From ffa103e26c6a80645f8b8e8afbe6b9a33de8c5d8 Mon Sep 17 00:00:00 2001 From: Trent Palmer Date: Sat, 13 Mar 2021 13:40:41 -0800 Subject: [PATCH] add registration --- accounts/forms.py | 35 ++++++++++++++++++++++++++++++++-- accounts/urls.py | 1 + accounts/views.py | 15 +++++++++++++++ tp/templates/base.html | 3 +++ tp/templates/base_form.html | 4 ++++ tp/templates/base_heading.html | 3 +++ 6 files changed, 59 insertions(+), 2 deletions(-) diff --git a/accounts/forms.py b/accounts/forms.py index 5560241..1d57de1 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -1,7 +1,7 @@ -from django.contrib.auth.forms import ValidationError, UsernameField # , UserCreationForm +from django.contrib.auth.forms import ValidationError, UsernameField, UserCreationForm from django.contrib.auth.models import User from django import forms -from .models import Account +from .models import Account, EmailWhiteList class EnableTotpForm(forms.ModelForm): @@ -13,6 +13,37 @@ class EnableTotpForm(forms.ModelForm): fields = ("totp_code", ) +class OurUserCreationForm(UserCreationForm): + email = forms.EmailField( + required=True, + label='Email', + max_length=254, + widget=forms.EmailInput(attrs={'autocomplete': 'email'}) + ) + + class Meta: + model = User + fields = ("username", "email", "password1", "password2") + + def save(self, commit=True): + user = super(OurUserCreationForm, self).save(commit=False) + user.email = self.cleaned_data["email"] + if commit: + user.save() + return user + + def clean(self): + email = self.cleaned_data.get('email') + if not EmailWhiteList.objects.filter(email=email).exists(): + raise ValidationError("Email Not Authorized, try another.") + if User.objects.filter(email=email).exists(): + raise ValidationError("An account already exists with this email address.") + username = self.cleaned_data.get('username') + if User.objects.filter(username=username).exists(): + raise ValidationError("Try a different username, that one already exists.") + return self.cleaned_data + + class EditProfileForm(forms.Form): email = forms.EmailField( required=True, diff --git a/accounts/urls.py b/accounts/urls.py index 03cf8cb..8f9b054 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -8,6 +8,7 @@ app_name = "accounts" urlpatterns = [ path('login/', log_in, name='login'), + path('register/', views.register, name='register'), path('logout/', views.log_out, name='logout'), path('edit-profile/', views.edit_profile, name='edit_profile'), path('password-change/', views.password_change, name='password_change'), diff --git a/accounts/views.py b/accounts/views.py index bbb5032..bca72c8 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,5 +1,6 @@ from django.shortcuts import render, redirect from django.contrib.auth.forms import PasswordChangeForm +from .forms import OurUserCreationForm from django.contrib import messages from django.contrib.auth import logout, update_session_auth_hash from .forms import EditProfileForm @@ -20,6 +21,20 @@ def password_change(request): return render(request, 'base_form.html', {'form': form}) +def register(request): + if request.user.is_authenticated: + return redirect('audio:home') + if request.method == "POST": + form = OurUserCreationForm(request.POST) + if form.is_valid(): + form.save() + messages.success(request, 'Successfully Registered!', extra_tags="mb-0") + return redirect('accounts:login') + else: + form = OurUserCreationForm() + return render(request, 'base_form.html', {'form': form}) + + def log_out(request): if not request.user.is_authenticated: return redirect('audio:home') diff --git a/tp/templates/base.html b/tp/templates/base.html index ab881d6..b0224bb 100644 --- a/tp/templates/base.html +++ b/tp/templates/base.html @@ -14,6 +14,7 @@ {% url 'accounts:disable_totp' as disable_totp_url %} {% url 'audio:new_feed' as new_feed_url %} {% url 'audio:feeds' as feeds_url %} + {% url 'accounts:register' as register_url %} {% if request.path == home_url %} Home @@ -33,6 +34,8 @@ New Feed? {% elif request.path == feeds_url %} Feeds + {% elif request.path == register_url %} + Register {% endif %} {{ title }} diff --git a/tp/templates/base_form.html b/tp/templates/base_form.html index 42e6d01..33e74b2 100644 --- a/tp/templates/base_form.html +++ b/tp/templates/base_form.html @@ -8,6 +8,7 @@ {% url 'accounts:password_change' as password_change_url %} {% url 'audio:new_feed' as new_feed_url %} {% url 'accounts:password_reset' as password_reset_url %} + {% url 'accounts:register' as register_url %} {% if request.path == login_url %} {% firstof 'Login' as submit %} @@ -19,6 +20,8 @@ {% firstof 'Submit' as submit %} {% elif request.path == password_reset_url %} {% firstof 'Reset Password' as submit %} + {% elif request.path == register_url %} + {% firstof 'Register' as submit %} {% endif %} {% include "base_navbar.html" %} @@ -59,6 +62,7 @@ {% endif %} {% if request.path == login_url %}

Forgot Password? Reset Password

+

Need an account? Register

{% endif %} diff --git a/tp/templates/base_heading.html b/tp/templates/base_heading.html index 44b205a..8f7e13f 100644 --- a/tp/templates/base_heading.html +++ b/tp/templates/base_heading.html @@ -12,6 +12,7 @@ {% url 'accounts:password_reset' as password_reset_url %} {% url 'accounts:password_reset_done' as password_reset_done_url %} {% url 'accounts:password_reset_complete' as password_reset_complete_url %} + {% url 'accounts:register' as register_url %} {% if request.path == login_url %} Login? @@ -35,6 +36,8 @@ Password Reset Sent {% elif request.path == password_reset_complete_url %} Password Reset Complete + {% elif request.path == register_url %} + Register? {% endif %} {{ heading }}