diff --git a/django/didgeridoo/webshop/forms.py b/django/didgeridoo/webshop/forms.py new file mode 100644 index 0000000..c87ec6c --- /dev/null +++ b/django/didgeridoo/webshop/forms.py @@ -0,0 +1,24 @@ +from django import forms +from webshop.models import Salutation, City + + +class RegistrationForm(forms.Form): + email = forms.EmailField() + salutation = forms.ModelChoiceField(queryset=Salutation.objects.all()) + first_name = forms.CharField() + last_name = forms.CharField() + street_name = forms.CharField() + street_number = forms.CharField() + zip_code = forms.IntegerField(min_value=1000, max_value=9999) + city = forms.CharField() + + def clean_city(self): + # Check that the two password entries match + city = self.cleaned_data['city'] + zip_code = self.cleaned_data['zip_code'] + try: + City.objects.get(name=city, zip_code=zip_code) + except City.DoesNotExist: + raise forms.ValidationError( + "The zip code and the city don't match.") + return city diff --git a/django/didgeridoo/webshop/templates/registration/login.html b/django/didgeridoo/webshop/templates/registration/login.html index 1d51a55..4ff603e 100644 --- a/django/didgeridoo/webshop/templates/registration/login.html +++ b/django/didgeridoo/webshop/templates/registration/login.html @@ -7,5 +7,6 @@ {% csrf_token %} {{ form.as_p }} +

Go to registration.

{% endblock %} diff --git a/django/didgeridoo/webshop/templates/registration/register.html b/django/didgeridoo/webshop/templates/registration/register.html new file mode 100644 index 0000000..032758f --- /dev/null +++ b/django/didgeridoo/webshop/templates/registration/register.html @@ -0,0 +1,19 @@ +{% extends 'webshop/base.html' %} + +{% block section_title %}Registration{% endblock %} + +{% block content %} + {% if profile_form.errors %} +

+ Please correct the error{{ form.errors|pluralize }} below. +

+ {% endif %} +
+ + {{ user_form.as_table }} + {{ profile_form.as_table }} +
+ {% csrf_token %} + +
+{% endblock %} diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py index bcaddec..dc79c1d 100644 --- a/django/didgeridoo/webshop/urls.py +++ b/django/didgeridoo/webshop/urls.py @@ -11,7 +11,6 @@ urlpatterns = [ views.articles_in_category, name='category'), url('^', include('django.contrib.auth.urls')), - url(r'^accounts/profile/$', - views.profile, - name='profile'), + url(r'^profile/$', views.profile, name='profile'), + url(r'^registration/$', views.registration, name='registration'), ] diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py index 0b17186..499c261 100644 --- a/django/didgeridoo/webshop/views.py +++ b/django/didgeridoo/webshop/views.py @@ -1,6 +1,10 @@ +from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.contrib.auth.decorators import login_required -from webshop.models import Article, Category, ArticleStatus, Person +from django.contrib.auth.models import User +from django.contrib.auth.forms import UserCreationForm +from webshop.models import Article, Category, ArticleStatus, Person, City +from webshop.forms import RegistrationForm # Create your views here. @@ -41,3 +45,32 @@ def profile(request): person = Person.objects.get(user=request.user) return render(request, 'registration/profile.html', {'person': person}) + + +def registration(request): + if request.method == 'POST': + profile_form = RegistrationForm(request.POST) + user_form = UserCreationForm(request.POST) + if (profile_form.is_valid() and user_form.is_valid()): + pf = profile_form.cleaned_data + uf = user_form.cleaned_data + user = User.objects.create_user(uf['username'], + pf['email'], + uf['password2']) + user.last_name = pf['last_name'] + user.first_name = pf['first_name'] + user.save() + person = Person.objects.create( + salutation=pf['salutation'], + city=City.objects.get(zip_code=pf['zip_code'], + name=pf['city']), + street_name=pf['street_name'], + street_number=pf['street_number'], + user=user) + return HttpResponseRedirect('/login/') + else: + profile_form = RegistrationForm + user_form = UserCreationForm + return render(request, 'registration/register.html', + {'profile_form': profile_form, + 'user_form': user_form})