From 636db45c16974e8b49292ca6b89a331356dbc0fe Mon Sep 17 00:00:00 2001 From: rapsealk Date: Thu, 11 Feb 2021 20:44:37 +0900 Subject: [PATCH] =?UTF-8?q?WIP-001:=20=EC=82=AC=EC=9A=A9=EC=9E=90(User)=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20=EA=B3=BC=EC=A0=95=20=EA=B0=80=EC=95=88=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=20(#1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 - mercy/mercy/urls.py | 3 +- mercy/mercykids/migrations/0001_initial.py | 26 +++++++ .../migrations/0002_auto_20210211_2040.py | 33 +++++++++ mercy/mercykids/urls.py | 9 +++ mercy/mercykids/views.py | 67 ++++++++++++++++++- 6 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 mercy/mercykids/migrations/0001_initial.py create mode 100644 mercy/mercykids/migrations/0002_auto_20210211_2040.py create mode 100644 mercy/mercykids/urls.py diff --git a/.gitignore b/.gitignore index 700a4d5..73a1a48 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,6 @@ __pycache__/ *.pyc -# Django -migrations/ - # Docker docker/*/data/* diff --git a/mercy/mercy/urls.py b/mercy/mercy/urls.py index 5570c9e..676b034 100644 --- a/mercy/mercy/urls.py +++ b/mercy/mercy/urls.py @@ -14,8 +14,9 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import include, path urlpatterns = [ + path('kids/', include('mercykids.urls')), path('admin/', admin.site.urls), ] diff --git a/mercy/mercykids/migrations/0001_initial.py b/mercy/mercykids/migrations/0001_initial.py new file mode 100644 index 0000000..c5a62cf --- /dev/null +++ b/mercy/mercykids/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 3.1.6 on 2021-02-04 11:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('uid', models.CharField(max_length=200)), + ('email', models.CharField(max_length=32)), + ('password', models.CharField(max_length=64)), + ('name', models.CharField(max_length=10)), + ('nickname', models.CharField(max_length=10)), + ('registered_at', models.DateTimeField()), + ], + ), + ] diff --git a/mercy/mercykids/migrations/0002_auto_20210211_2040.py b/mercy/mercykids/migrations/0002_auto_20210211_2040.py new file mode 100644 index 0000000..f463ca6 --- /dev/null +++ b/mercy/mercykids/migrations/0002_auto_20210211_2040.py @@ -0,0 +1,33 @@ +# Generated by Django 3.1.6 on 2021-02-11 11:40 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mercykids', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='user', + name='uid', + ), + migrations.AddField( + model_name='user', + name='birthdate', + field=models.DateTimeField(default=datetime.datetime(1970, 1, 1, 9, 0)), + ), + migrations.AlterField( + model_name='user', + name='id', + field=models.AutoField(primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='user', + name='registered_at', + field=models.DateTimeField(default=datetime.datetime(2021, 2, 11, 20, 40, 34, 953546)), + ), + ] diff --git a/mercy/mercykids/urls.py b/mercy/mercykids/urls.py new file mode 100644 index 0000000..544f10a --- /dev/null +++ b/mercy/mercykids/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path('', views.index, name='index'), + path('auth/register', views.register_user, name='register_user'), + path('auth/verify', views.auth_verify_email, name='auth_verify_email') +] diff --git a/mercy/mercykids/views.py b/mercy/mercykids/views.py index 91ea44a..94efdfb 100644 --- a/mercy/mercykids/views.py +++ b/mercy/mercykids/views.py @@ -1,3 +1,68 @@ -from django.shortcuts import render +import json +from datetime import datetime + +# from django.shortcuts import render +from django.http import JsonResponse + +from mercykids.models import User + +HTTP_OK = 200 +HTTP_BAD_REQUEST = 400 + + +class BooleanJsonResponse(JsonResponse): + def __init__(self, data: bool, status=HTTP_OK): + assert type(data) is bool + data = {"value": data} + super(BooleanJsonResponse, self).__init__(data, status=status) + # Create your views here. +def index(request): + # token = request.headers.get('Authorization', None) + # body = json.loads(request.body) + return JsonResponse({}) + + +def auth_verify_email(request): + email = request.POST.get('email', None) + if email is None: + return JsonResponse({ + "message": "이메일(email)이 입력되지 않았습니다." + }, + status=HTTP_BAD_REQUEST, + json_dumps_params={'ensure_ascii': True}) + is_email_registered = _is_email_registered(email) + return BooleanJsonResponse(is_email_registered) + + +def register_user(request): + # TODO: 1) 이메일 형태 검증 (regex) + # TODO: 2) 이메일 중복 확인 + # TODO: 3) 비밀번호 해싱 + email = request.POST.get('email', None) + password = request.POST.get('password', None) + name = request.POST.get('name', None) + nickname = request.POST.get('nickname', None) + birthdate = request.POST.get('birthdate', None) + user = User( + email=email, + password=password, + name=name, + nickname=nickname, + birthdate=datetime(*map(int, birthdate.split('-'))) + ) + user.save() + + return BooleanJsonResponse(True) + + +def _is_email_registered(email: str) -> bool: + query = User.objects.filter(email=email) + try: + _ = query.get() + return True + except User.DoesNotExist: + return False + except Exception: + return False