Skip to content

Commit

Permalink
Merge pull request #26 from PETCH-KR/feature/#22/review-api
Browse files Browse the repository at this point in the history
Feature/#22/review api
  • Loading branch information
Zih0 authored Aug 18, 2021
2 parents 88c6cdd + df79d1b commit 8a03863
Show file tree
Hide file tree
Showing 25 changed files with 402 additions and 38 deletions.
7 changes: 3 additions & 4 deletions config/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ def authenticate(self, request):
jwt_token, os.getenv("JWT_SECRET_KEY"), algorithms=["HS256"]
)
id = decoded.get("id")
if token_type is "Bearer":
if token_type == "Bearer":
user = User.objects.get(id=id)
# else:
# TODO : organization BigAutoField id 추가
# user = Organization.objects.get(id=id)
else:
user = Organization.objects.get(id=id)
return (user, None)
except jwt.exceptions.DecodeError:
raise exceptions.AuthenticationFailed(
Expand Down
11 changes: 11 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,14 @@

DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
MEDIA_URL = f"https://storage.googleapis.com/{GS_BUCKET_NAME}/static/"


# Swagger

SWAGGER_SETTINGS = {
"SHOW_REQUEST_HEADERS": True,
"SECURITY_DEFINITIONS": {
"Bearer": {"type": "apiKey", "name": "Authorization", "in": "header"}
},
"JSON_EDITOR": True,
}
11 changes: 7 additions & 4 deletions server/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from .airport import *
from .user import *
from .dog import *
from .organization import *
from .airport_model import *
from .user_model import *
from .dog_model import *
from .organization_model import *

from .user_review_model import *
from .organization_review_model import *
File renamed without changes.
5 changes: 2 additions & 3 deletions server/models/dog.py → server/models/dog_model.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# from django.db import models
from djongo import models
from .organization import Organization, OrganizationForm
from .organization_model import AbstractOrganization, OrganizationForm
from django.conf import settings
from djongo.storage import GridFSStorage



class Dog(models.Model):
_id = models.ObjectIdField(primary_key=True)
name = models.CharField(max_length=100)
Expand All @@ -17,7 +16,7 @@ class Dog(models.Model):
image = models.ImageField(upload_to="dogs", null=True)

organization = models.EmbeddedField(
model_container=Organization, model_form_class=OrganizationForm
model_container=AbstractOrganization, model_form_class=OrganizationForm
)

class Meta:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class Image(models.Model):
url = models.URLField(max_length=200)
url = models.URLField(max_length=1000)

class Meta:
abstract = True
Expand All @@ -29,7 +29,8 @@ class Meta:
fields = ("sns", "name")


class Organization(models.Model):
class AbstractOrganization(models.Model):
_id = models.ObjectIdField()
name = models.CharField(max_length=100)
ceo = models.CharField(max_length=100)
description = models.TextField()
Expand All @@ -44,6 +45,10 @@ class Meta:
abstract = True


class Organization(AbstractOrganization):
objects = models.DjongoManager()


class OrganizationForm(forms.ModelForm):
class Meta:
model = Organization
Expand Down
11 changes: 11 additions & 0 deletions server/models/organization_review_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from djongo import models
from server.models import BaseUser, Organization


class OrganizationReview(models.Model):
_id = models.ObjectIdField(primary_key=True)
comment = models.CharField(max_length=500)
image = models.CharField(max_length=500, default="")
createdAt = models.DateField(auto_now_add=True)
organization = models.EmbeddedField(model_container=Organization)
user = models.EmbeddedField(model_container=BaseUser)
26 changes: 22 additions & 4 deletions server/models/user.py → server/models/user_model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from django.contrib.auth.models import AbstractBaseUser
from django.db import models
from djongo import models
from django.contrib.auth.models import UserManager
import uuid


class User(AbstractBaseUser):

class BaseUser(AbstractBaseUser):
id = models.ObjectIdField(primary_key=True)
email = models.EmailField(
max_length=100, unique=True, verbose_name="Email", help_text="이메일"
max_length=100,
unique=True,
verbose_name="Email",
help_text="이메일",
)
password = models.CharField(
max_length=128, null=True, verbose_name="password", help_text="비밀번호"
Expand All @@ -28,7 +32,21 @@ class User(AbstractBaseUser):
max_length=255, null=True, verbose_name="token", help_text="Refresh Token"
)

class Meta:
abstract = True


class User(BaseUser):
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []

objects = UserManager()

def __str__(self):
return self.email

def has_perm(self, perm, obj=None):
return True

def has_module_perms(self, app_label):
return True
13 changes: 13 additions & 0 deletions server/models/user_review_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from djongo import models
from server.models import BaseUser, AbstractOrganization


class UserReview(models.Model):
_id = models.ObjectIdField(primary_key=True)
comment = models.CharField(max_length=500)
image = models.CharField(max_length=1000, default="")
createdAt = models.DateField(auto_now_add=True)
organization = models.EmbeddedField(model_container=AbstractOrganization)
user = models.EmbeddedField(model_container=BaseUser)

objects = models.DjongoManager()
8 changes: 5 additions & 3 deletions server/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .airport import *
from .user import *
from .dog import *
from .airport_serializer import *
from .user_serializer import *
from .organization_serializer import *
from .dog_serializer import *
from .review_serializer import *
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions server/serializers/organization_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import serializers
from server.models import Organization


class OrganizationSerializer(serializers.ModelSerializer):
class Meta:
model = Organization
fields = "__all__"
15 changes: 15 additions & 0 deletions server/serializers/review_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from rest_framework import serializers
from server.models import UserReview, OrganizationReview
from server.serializers import *


class UserReviewSerializer(serializers.ModelSerializer):
class Meta:
model = UserReview
fields = "__all__"


class OrganizationReviewSerializer(serializers.ModelSerializer):
class Meta:
model = OrganizationReview
fields = "__all__"
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ class UserSerializer(serializers.ModelSerializer):

class Meta:
model = User
fields = [
"password",
"email",
"phone",
"passport",
"provider",
]
fields = "__all__"

def create(self, validated_data):
if validated_data.get("password"):
Expand Down
7 changes: 4 additions & 3 deletions server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
path("user/kakao", views.kakao),
path("refresh", views.refresh),
path("airport/<str:name>", views.AirportAPIView.as_view()),
path('dog/list/', views.DogAPIView.as_view()),
path('dog/description/', views.DogDescriptionAPIView.as_view()),
path('dog/image/', views.DogImageAPIView.as_view()),
path("dog/list/", views.DogAPIView.as_view()),
path("dog/description/", views.DogDescriptionAPIView.as_view()),
path("dog/image/", views.DogImageAPIView.as_view()),
path("upload/test", views.upload_test),
path("review/user", views.UserReviewAPIView.as_view()),
]
12 changes: 12 additions & 0 deletions server/utils/error_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@ def as_md(self):
status=status.HTTP_403_FORBIDDEN,
message="refresh_token이 만료되었습니다.",
)

REVIEW_400_NULL_REQUEST_DATA = ErrorCollection(
code="REVIEW_400_NULL_REQUEST_DATA",
status=status.HTTP_400_BAD_REQUEST,
message="누락된 정보(이미지, 코멘트, 기관ID)가 있습니다. 확인해주세요.",
)

REVIEW_400_ADD_REVIEW_FAILED = ErrorCollection(
code="REVIEW_400_ADD_REVIEW_FAILED",
status=status.HTTP_400_BAD_REQUEST,
message="리뷰 저장 시 문제가 발생했습니다.",
)
13 changes: 13 additions & 0 deletions server/utils/json_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import json
from bson import ObjectId


class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, ObjectId):
return str(o)
return json.JSONEncoder.default(self, o)


def jsonify(data):
return json.loads(json.dumps(data, cls=JSONEncoder))
2 changes: 1 addition & 1 deletion server/utils/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def generate_access_token(user):

access_token_payload = {
"id": user.id,
"exp": datetime.datetime.utcnow() + datetime.timedelta(days=0, minutes=5),
"exp": datetime.datetime.utcnow() + datetime.timedelta(days=1),
"iat": datetime.datetime.utcnow(),
}
access_token = jwt.encode(
Expand Down
119 changes: 119 additions & 0 deletions server/utils/success_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from drf_yasg import openapi
from drf_yasg.openapi import Items

from server.models import *


class Success(object):
Expand Down Expand Up @@ -70,3 +73,119 @@ def as_obj(self):
"email": openapi.Schema(type=openapi.TYPE_STRING, description="이메일"),
},
)

SUCCESS_ADD_USER_REVIEW = Success(
message="리뷰가 성공적으로 작성되었습니다.",
data={
"_id": openapi.Schema(type=openapi.TYPE_STRING, description="리뷰 id"),
"comment": openapi.Schema(type=openapi.TYPE_STRING, description="리뷰 내용"),
"image": openapi.Schema(
type=openapi.TYPE_STRING,
description="리뷰 내용",
),
"organization": openapi.Schema(
type=openapi.TYPE_OBJECT,
description="기관 정보",
properties={
"_id": openapi.Schema(type=openapi.TYPE_STRING, description="기관 id"),
"name": openapi.Schema(type=openapi.TYPE_STRING, description="기관명"),
"ceo": openapi.Schema(type=openapi.TYPE_STRING, description="CEO"),
"description": openapi.Schema(
type=openapi.TYPE_STRING, description="기관 설명"
),
"phone": openapi.Schema(
type=openapi.TYPE_STRING, description="기관 전화번호"
),
"images": openapi.Schema(
type=openapi.TYPE_ARRAY,
description="이미지",
items=Items(
type=openapi.TYPE_STRING,
),
),
"donation": openapi.Schema(
type=openapi.TYPE_STRING, description="후원계좌"
),
"fax": openapi.Schema(type=openapi.TYPE_STRING, description="팩스"),
"email": openapi.Schema(type=openapi.TYPE_STRING, description="이메일"),
"sns": openapi.Schema(
type=openapi.TYPE_ARRAY,
description="SNS",
items=Items(
type=openapi.TYPE_OBJECT,
),
),
},
),
"user": openapi.Schema(
type=openapi.TYPE_OBJECT,
description="유저 정보",
properties={
"id": openapi.Schema(type=openapi.TYPE_INTEGER, description="유저 id"),
"email": openapi.Schema(type=openapi.TYPE_STRING, description="이메일"),
"phone": openapi.Schema(type=openapi.TYPE_STRING, description="전화번호"),
"passport": openapi.Schema(
type=openapi.TYPE_BOOLEAN, description="여권 인증유무", default=False
),
},
),
},
)

SUCCESS_GET_USER_REVIEW = Success(
message="리뷰를 성공적으로 불러왔습니다.",
data={
"_id": openapi.Schema(type=openapi.TYPE_STRING, description="리뷰 id"),
"comment": openapi.Schema(type=openapi.TYPE_STRING, description="리뷰 내용"),
"image": openapi.Schema(
type=openapi.TYPE_STRING,
description="리뷰 내용",
),
"organization": openapi.Schema(
type=openapi.TYPE_OBJECT,
description="기관 정보",
properties={
"_id": openapi.Schema(type=openapi.TYPE_STRING, description="기관 id"),
"name": openapi.Schema(type=openapi.TYPE_STRING, description="기관명"),
"ceo": openapi.Schema(type=openapi.TYPE_STRING, description="CEO"),
"description": openapi.Schema(
type=openapi.TYPE_STRING, description="기관 설명"
),
"phone": openapi.Schema(
type=openapi.TYPE_STRING, description="기관 전화번호"
),
"images": openapi.Schema(
type=openapi.TYPE_ARRAY,
description="이미지",
items=Items(
type=openapi.TYPE_STRING,
),
),
"donation": openapi.Schema(
type=openapi.TYPE_STRING, description="후원계좌"
),
"fax": openapi.Schema(type=openapi.TYPE_STRING, description="팩스"),
"email": openapi.Schema(type=openapi.TYPE_STRING, description="이메일"),
"sns": openapi.Schema(
type=openapi.TYPE_ARRAY,
description="SNS",
items=Items(
type=openapi.TYPE_OBJECT,
),
),
},
),
"user": openapi.Schema(
type=openapi.TYPE_OBJECT,
description="유저 정보",
properties={
"id": openapi.Schema(type=openapi.TYPE_INTEGER, description="유저 id"),
"email": openapi.Schema(type=openapi.TYPE_STRING, description="이메일"),
"phone": openapi.Schema(type=openapi.TYPE_STRING, description="전화번호"),
"passport": openapi.Schema(
type=openapi.TYPE_BOOLEAN, description="여권 인증유무", default=False
),
},
),
},
)
1 change: 1 addition & 0 deletions server/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .airport import *
from .user import *
from .dog import *
from .review import *
Loading

0 comments on commit 8a03863

Please sign in to comment.