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/README.md b/README.md index 94f2edc..e680376 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Mercy Kids API ![CI Lint](https://github.com/mercy-project/mercy-kids-django/workflows/Lint/badge.svg) -![Python 3.8](https://img.shields.io/badge/Python-3.8-blue.svg) -![Django](https://img.shields.io/badge/Django-3.1.5-yellowgreen.svg) +![Python 3.8](https://img.shields.io/badge/Python-3.8-blue.svg?logo=python) +![Django](https://img.shields.io/badge/Django-3.1.5-yellowgreen.svg?logo=django) ## virtualenv ```bat 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