From 5e23c7359460abaf50273b0056eb65c5d236103b Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 12 Jan 2015 16:51:54 +0100 Subject: [PATCH] Add registration form --- accounts/templates/accounts/registration.html | 10 ++++ accounts/tests.py | 51 +++++++++++++++++++ accounts/urls.py | 12 +++++ accounts/views.py | 23 +++++++++ booking/templates/front_template.html | 3 ++ duckbook/settings.py | 1 + duckbook/urls.py | 3 +- requirements.txt | 2 + 8 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 accounts/templates/accounts/registration.html create mode 100644 accounts/tests.py create mode 100644 accounts/urls.py create mode 100644 accounts/views.py diff --git a/accounts/templates/accounts/registration.html b/accounts/templates/accounts/registration.html new file mode 100644 index 0000000..090190e --- /dev/null +++ b/accounts/templates/accounts/registration.html @@ -0,0 +1,10 @@ +{% extends 'front_template.html' %} + +{% block body %} +

For later convenience, please use your corporate ID as the username!

+
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} diff --git a/accounts/tests.py b/accounts/tests.py new file mode 100644 index 0000000..ca55c41 --- /dev/null +++ b/accounts/tests.py @@ -0,0 +1,51 @@ +from django.test import TestCase, Client +from django_webtest import WebTest +from django.contrib.auth.models import User +from django.contrib.auth.forms import UserCreationForm + +class FrontTest(TestCase): + def setUp(self): + self.client = Client() + + def test_registration_page(self): + response = self.client.get('/accounts/register') + self.assertEqual(response.status_code, 200) + +class RegFormTest(WebTest): + def test_valid_data(self): + form_data = { + 'username': 'test', + 'password1': 'password', + 'password2': 'password' + } + + form = UserCreationForm(form_data) + self.assertTrue(form.is_valid()) + + user = form.save() + self.assertEqual(user.username, 'test') + # The password must be encrypted by now + self.assertNotEqual(user.password, 'password') + + def test_empty(self): + form_data = {} + form = UserCreationForm(form_data) + self.assertFalse(form.is_valid()) + self.assertEqual(form.errors, { + 'username': ['This field is required.'], + 'password1': ['This field is required.'], + 'password2': ['This field is required.'], + }) + + def test_form_error(self): + page = self.app.get('/accounts/register') + page = page.form.submit() + self.assertContains(page, "This field is required.") + + def test_form_success(self): + page = self.app.get('/accounts/register') + page.form['username'] = 'test' + page.form['password1'] = 'password' + page.form['password2'] = 'password' + page = page.form.submit() + self.assertRedirects(page, '/') diff --git a/accounts/urls.py b/accounts/urls.py new file mode 100644 index 0000000..6323ee6 --- /dev/null +++ b/accounts/urls.py @@ -0,0 +1,12 @@ +from django.conf.urls import patterns, url + +from .views import RegistrationFormView + +urlpatterns = patterns( + '', + url( + r'^register$', + RegistrationFormView.as_view(), + name = 'register' + ), +) diff --git a/accounts/views.py b/accounts/views.py new file mode 100644 index 0000000..9a97f36 --- /dev/null +++ b/accounts/views.py @@ -0,0 +1,23 @@ +from django.shortcuts import render +from django.views import generic +from django.http import HttpResponseRedirect +from django.core.urlresolvers import reverse +from django.contrib.auth.forms import UserCreationForm + +class RegistrationFormView(generic.View): + form_class = UserCreationForm + template_name = 'accounts/registration.html' + + def get(self, request): + form = self.form_class() + return render(request, self.template_name, { 'form': form }) + + def post(self, request): + form = self.form_class(request.POST) + + if form.is_valid(): + form.save() + + return HttpResponseRedirect(reverse('booking:index')) + + return render(request, self.template_name, {'form': form}) diff --git a/booking/templates/front_template.html b/booking/templates/front_template.html index e3ab9f8..7a32326 100644 --- a/booking/templates/front_template.html +++ b/booking/templates/front_template.html @@ -5,6 +5,9 @@

Rubber Duck Booking Tool

+
+ Register +
Home Terms and Conditions Vocabulary diff --git a/duckbook/settings.py b/duckbook/settings.py index 2bc2007..16e50fd 100644 --- a/duckbook/settings.py +++ b/duckbook/settings.py @@ -36,6 +36,7 @@ INSTALLED_APPS = ( 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'accounts', 'booking', ) diff --git a/duckbook/urls.py b/duckbook/urls.py index 1a4007c..d5838b9 100644 --- a/duckbook/urls.py +++ b/duckbook/urls.py @@ -3,5 +3,6 @@ from django.views.generic import TemplateView urlpatterns = patterns( '', - url('', include('booking.urls', namespace = 'booking')), + url(r'^accounts/', include('accounts.urls', namespace = 'accounts')), + url('', include('booking.urls', namespace = 'booking')), ) diff --git a/requirements.txt b/requirements.txt index af9154e..d98884a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ Django>=1.7,<1.8 +WebTest==2.0.17 +django-webtest==1.7.7