<Watcha_classic>
클래식 영화 모음 사이트.
영화 정보를 확인거나, 영화를 유저의 프로필 페이지에 삭제/등록 할 수 있다.
from django.db import models
from core.models import TimeStampModel
class User(TimeStampModel):
email = models.CharField(max_length=80, unique=True)
password = models.CharField(max_length=200)
username = models.CharField(max_length=80, unique=True)
date_of_birth = models.DateField()
class Meta:
db_table = 'users'
import re
from django.http import JsonResponse
from django.core.exceptions import ValidationError
#USERNAME_REGEX: 한글/영어, 숫자x,기호x
USERNAME_REGEX = '^([A-Za-z0-9가-힣]{2,})+'
#EMAIL_REGEX: @와 .필수
EMAIL_REGEX = '^[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$'
#PASSWORD_REGEX: 10자 이상, 영,숫자,특수기호 중 2개 이상 포함
PASSWORD_REGEX = '^((?=.*[A-Za-z])(?=.*\d)|(?=.*[A-Za-z])(?=.*[\^@$!%*#?&])|(?=.*\d)(?=.*[\^@$!%*#?&])).{10,}$'
#BIRTH_REGEX: 1900~2099년생까지
BIRTH_REGEX = '^(19[0-9][0-9]|20[0-9][0-9])*-(0[1-9]|1[0-2])*-(0[1-9]|[1-2][0-9]|3[0-1])$'
def validate_username(value):
if not re.match(USERNAME_REGEX,value):
raise ValidationError('INVALID_USERNAME')
def validate_email(value):
if not re.match(EMAIL_REGEX,value):
raise ValidationError('INVALID_EMAIL')
def validate_password(value):
if not re.match(PASSWORD_REGEX,value):
raise ValidationError('INVALID_PASSWORD')
def validate_birth(value):
if not re.match(BIRTH_REGEX,value):
raise ValidationError('INVALID_BIRTH')
import json
import bcrypt
import jwt
from django.http import JsonResponse
from django.views import View
from django.core.exceptions import ValidationError
from django.conf import settings
from users.models import User
from users.validation import (
validate_username,
validate_email,
validate_password,
validate_birth
)
from core.utils import token_decorator
class SignUpView(View):
def post(self, request):
try:
data = json.loads(request.body)
email = data['email']
password = data['password']
username = data['username']
date_of_birth = data['date_of_birth']
if User.objects.filter(email=email).exists():
return JsonResponse({'message' : 'EMAIL_ALREADY_EXISTS'}, status=409)
validate_username(username)
validate_email(email)
validate_password(password)
validate_birth(date_of_birth)
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
User.objects.create(
username = username,
email = email,
password = hashed_password.decode('utf-8'),
date_of_birth = date_of_birth,
)
return JsonResponse({'message' :'SUCCESS'}, status = 201)
except KeyError:
return JsonResponse({'message' :'KEY_ERROR'}, status = 400)
except ValidationError as error:
return JsonResponse({'message' : error.message}, status = 400)
class SignInView(View):
def post(self, request):
try:
data = json.loads(request.body)
user = User.objects.get(email=data['email'])
if not bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')):
return JsonResponse({'message' : 'INVALID_USER'}, status = 401)
access_token = jwt.encode({"id" : user.id}, settings.SECRET_KEY, algorithm = settings.ALGORITHM)
return JsonResponse({'access_token' : access_token}, status = 200)
except KeyError:
return JsonResponse({'message' :'KEY_ERROR'}, status = 400)
except User.DoesNotExist:
return JsonResponse({'message' : 'INVALID_USER'}, status = 401)
from django.urls import path
from users.views import SignUpView, SignInView
urlpatterns = [
path('/signup', SignUpView.as_view()),
path('/signin', SignInView.as_view())
]