Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#22/review api #26

Merged
merged 13 commits into from
Aug 18, 2021
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 @@ -30,6 +30,21 @@ class Meta:


class Organization(models.Model):
_id = models.ObjectIdField()
name = models.CharField(max_length=100)
ceo = models.CharField(max_length=100)
description = models.TextField()
phone = models.CharField(max_length=100)
images = models.ArrayField(model_container=Image, model_form_class=ImageForm)
donation = models.CharField(max_length=100)
fax = models.CharField(max_length=100)
email = models.CharField(max_length=100)
sns = models.ArrayField(model_container=SNS, model_form_class=SNSForm)
objects = models.DjongoManager()


class AbstractOrganization(models.Model):
_id = models.ObjectIdField()
Zih0 marked this conversation as resolved.
Show resolved Hide resolved
name = models.CharField(max_length=100)
ceo = models.CharField(max_length=100)
description = models.TextField()
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()),
]
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))
Zih0 marked this conversation as resolved.
Show resolved Hide resolved
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
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 *
3 changes: 1 addition & 2 deletions server/views/airport.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from server.serializers.airport import *
from server.serializers.airport_serializer import *
from rest_framework.response import Response
from rest_framework.views import APIView
from drf_yasg import openapi
Expand Down Expand Up @@ -31,7 +31,6 @@ class AirportAPIView(APIView):

def get_object(self, name):
queryset = Airport.objects.all()

name_match = queryset.filter(name__contains=name)
country_match = queryset.filter(country__contains=name)
IATA_match = queryset.filter(IATA__contains=name)
Expand Down
9 changes: 5 additions & 4 deletions server/views/dog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from server.models.dog import Dog
from server.serializers.dog import *
from server.models.dog_model import Dog
from server.serializers.dog_serializer import *
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
Expand Down Expand Up @@ -47,12 +47,13 @@ def get(self, request):
}
return Response(response_object)


class DogImageAPIView(APIView):
def post(self, request):
dog_serializer = DogImageSerializer(data = request.data)
dog_serializer = DogImageSerializer(data=request.data)
print(dog_serializer)
if dog_serializer.is_valid():
dog_serializer.save()
return Response({"message": "success"})
else:
return Response ({"message": "fail"})
return Response({"message": "fail"})
Loading