From 513166847c62900cdc7cc036e6f5fa58547fd95f Mon Sep 17 00:00:00 2001 From: Sergey G Date: Thu, 11 Jan 2018 17:12:17 +0500 Subject: [PATCH] LIL-68 Added registration page --- apps/__init__.py | 0 apps/auth/__init__.py | 0 apps/auth/admin.py | 3 ++ apps/auth/apps.py | 6 +++ apps/auth/forms.py | 8 ++++ apps/auth/migrations/__init__.py | 0 apps/auth/models.py | 3 ++ .../templates/auth/registration-learner.html | 10 ++++ apps/auth/tests/__init__.py | 0 apps/auth/tests/test_registration.py | 47 +++++++++++++++++++ apps/auth/urls.py | 7 +++ apps/auth/views.py | 26 ++++++++++ project/settings.py | 2 + project/urls.py | 3 +- 14 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 apps/__init__.py create mode 100644 apps/auth/__init__.py create mode 100644 apps/auth/admin.py create mode 100644 apps/auth/apps.py create mode 100644 apps/auth/forms.py create mode 100644 apps/auth/migrations/__init__.py create mode 100644 apps/auth/models.py create mode 100644 apps/auth/templates/auth/registration-learner.html create mode 100644 apps/auth/tests/__init__.py create mode 100644 apps/auth/tests/test_registration.py create mode 100644 apps/auth/urls.py create mode 100644 apps/auth/views.py diff --git a/apps/__init__.py b/apps/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/auth/__init__.py b/apps/auth/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/auth/admin.py b/apps/auth/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/apps/auth/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/apps/auth/apps.py b/apps/auth/apps.py new file mode 100644 index 00000000..ea157644 --- /dev/null +++ b/apps/auth/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AuthConfig(AppConfig): + name = 'apps.auth' + label = 'lilcity_auth' diff --git a/apps/auth/forms.py b/apps/auth/forms.py new file mode 100644 index 00000000..e369a8f9 --- /dev/null +++ b/apps/auth/forms.py @@ -0,0 +1,8 @@ +from django import forms + + +class LearnerRegistrationForm(forms.Form): + first_name = forms.CharField() + last_name = forms.CharField() + email = forms.EmailField() + password = forms.CharField() diff --git a/apps/auth/migrations/__init__.py b/apps/auth/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/auth/models.py b/apps/auth/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/apps/auth/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/apps/auth/templates/auth/registration-learner.html b/apps/auth/templates/auth/registration-learner.html new file mode 100644 index 00000000..de155ea5 --- /dev/null +++ b/apps/auth/templates/auth/registration-learner.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + \ No newline at end of file diff --git a/apps/auth/tests/__init__.py b/apps/auth/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/auth/tests/test_registration.py b/apps/auth/tests/test_registration.py new file mode 100644 index 00000000..f0824fec --- /dev/null +++ b/apps/auth/tests/test_registration.py @@ -0,0 +1,47 @@ +from django.test import TestCase, Client +from django.urls import reverse +from django.contrib.auth import get_user_model + + +User = get_user_model() + + +class LearnerRegistrationTest(TestCase): + def setUp(self): + self.client = Client() + self.url = reverse("lilcity:registration-learner") + self.user_data = { + "email": "test@example.com", + "first_name": "Alice", + "last_name": "T", + "password": "12345" + } + + def test_get_html_form(self): + response = self.client.get(self.url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.templates[0].name, 'auth/registration-learner.html') + + def test_should_create_user(self): + self.assertEqual(User.objects.count(), 0) + + response = self.client.post(self.url, self.user_data) + + self.assertEqual(response.status_code, 201) + self.assertTrue(response.json()['success']) + + user = User.objects.get(email=self.user_data['email']) + self.assertEqual(user.username, self.user_data['email']) + self.assertEqual(user.first_name, self.user_data['first_name']) + self.assertEqual(user.last_name, self.user_data['last_name']) + + def test_invalid_data(self): + del self.user_data['email'] + + response = self.client.post(self.url, self.user_data) + + self.assertEqual(response.status_code, 400) + self.assertIn('email', response.json()) + + def test_should_redirect_main_page_when_user_not_anonymous(self): # todo + pass diff --git a/apps/auth/urls.py b/apps/auth/urls.py new file mode 100644 index 00000000..ccf2d689 --- /dev/null +++ b/apps/auth/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path('registration/learner/', views.LearnerRegistrationView.as_view(), name="registration-learner") +] diff --git a/apps/auth/views.py b/apps/auth/views.py new file mode 100644 index 00000000..5e6d9287 --- /dev/null +++ b/apps/auth/views.py @@ -0,0 +1,26 @@ +from django.views.generic import FormView +from django.http import JsonResponse +from django.contrib.auth import get_user_model + +from .forms import LearnerRegistrationForm + + +User = get_user_model() + + +class LearnerRegistrationView(FormView): + form_class = LearnerRegistrationForm + template_name = "auth/registration-learner.html" + + def form_valid(self, form): + first_name = form.cleaned_data['first_name'] + last_name = form.cleaned_data['last_name'] + email = form.cleaned_data['email'] + password = form.cleaned_data['password'] + + User.objects.create(username=email, email=email, first_name=first_name, last_name=last_name, password=password) + + return JsonResponse({"success": True}, status=201) + + def form_invalid(self, form): + return JsonResponse(form.errors.get_json_data(escape_html=True), status=400) diff --git a/project/settings.py b/project/settings.py index 51fedf87..1379a18e 100644 --- a/project/settings.py +++ b/project/settings.py @@ -37,6 +37,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', +] + [ + 'apps.auth.apps' ] MIDDLEWARE = [ diff --git a/project/urls.py b/project/urls.py index 37a462cb..6c8ff891 100644 --- a/project/urls.py +++ b/project/urls.py @@ -14,8 +14,9 @@ Including another URLconf 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 path, include urlpatterns = [ path('admin/', admin.site.urls), + path('auth/', include(('apps.auth.urls', 'lilcity'))), ]