마이리얼트립 클론②API-Users

박민하·2022년 7월 18일
0

PROJECT

목록 보기
15/17
post-thumbnail

<Fake_trip>
국내 숙소 카테고리를 한정으로 검색, 조건별 필터링, 예약 기능 구현

✅ Token_validators

#core>token_validators
import jwt

from django.http	   import JsonResponse

from users.models      import User
from faketrip.settings import SECRET_KEY , ALGORITHM

def token_validator(func):
    def wrapper(self,request,*args,**kwargs):
        try:
            access_token = request.headers.get("Authorization",None)
            payload      = jwt.decode(access_token, SECRET_KEY, ALGORITHM)
            user         = User.objects.get(id=payload['user_id'])
            request.user = user  
            
            return func(self, request, *args, **kwargs)

        except jwt.exceptions.DecodeError:
            return JsonResponse({'message' : 'Invalid Token'}, status=401)

        except User.DoesNotExist:
            return JsonResponse({'message' : 'Invalid User'}, status=401)
            
    return wrapper

✅ View

✔ import

#users>views
import boto3

from django.views           import View
from django.http            import JsonResponse, HttpResponse
from django.core.exceptions import ValidationError

from users.models            import User, Review
from faketrip.settings       import AWS_S3_ACCESS_KEY_ID, AWS_S3_SECRET_ACCESS_KEY
from core.token_validators   import token_validator
from core.file_upload_module import FileHandler, AwsUploader
from core.validators         import validate_phone_number

✔ 회원 정보

class UserView(View):
    @token_validator
    def patch(self, request):
        try:
            phone_number = request.body.get(phone_number)

            validate_phone_number(phone_number)

            request.user.phone_number = phone_number
            request.user.save()

            return JsonResponse({'message' : 'SUCCES'}, status = 200)

        except ValidationError as e:
            return JsonResponse({'message' : e.message}, status = 400) 

✔ 리뷰 기능

aws_keys = {'aws_access_key_id'     : AWS_S3_ACCESS_KEY_ID,
            'aws_secret_access_key' : AWS_S3_SECRET_ACCESS_KEY}
config   = {'bucket_name' : "ding-s3-bucket"}

file_uploader = AwsUploader(boto3.client('s3',**aws_keys), config)
file_handler  = FileHandler(file_uploader)

class ReviewView(View):
    @token_validator
    def post(self, request):
        product_id   = request.POST.get('product_id')
        room_id      = request.POST.get('room_id')
        content      = request.POST.get('content')
        rating       = request.POST.get('rating')
        product_id   = request.POST.get('product_id')
        review_image = request.FILES.get('image')

        if not rating:
            return JsonResponse({'message' : 'Choice Rating'}, status = 400)

        if not content:
            return JsonResponse({'message' : 'Insert Content'}, status = 400)

        image_url      = file_handler.upload(review_image)

        Review.objects.create(
            user       = request.user,
            product_id = product_id,
            room_id    = room_id,
            content    = content,
            rating     = rating,
            image_url  = image_url
        )

        return JsonResponse({'message' : 'Create Review'}, status = 201)
    
    @token_validator
    def post(self, request):
        try:
            review_id = request.GET.get('review_id')
            file_url  = Review.objects.get(id = review_id).image_url

            file_handler.delete(file_url)

            return HttpResponse(status = 204)

        except Review.DoesNotExist:
            return JsonResponse({'message' : 'Invalid Review'}, status = 400)
profile
backend developer 🐌

0개의 댓글