add registration

This commit is contained in:
Trent Palmer 2021-03-13 13:40:41 -08:00
parent 5a052c2a56
commit ffa103e26c
6 changed files with 59 additions and 2 deletions

View File

@ -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.contrib.auth.models import User
from django import forms from django import forms
from .models import Account from .models import Account, EmailWhiteList
class EnableTotpForm(forms.ModelForm): class EnableTotpForm(forms.ModelForm):
@ -13,6 +13,37 @@ class EnableTotpForm(forms.ModelForm):
fields = ("totp_code", ) 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): class EditProfileForm(forms.Form):
email = forms.EmailField( email = forms.EmailField(
required=True, required=True,

View File

@ -8,6 +8,7 @@ app_name = "accounts"
urlpatterns = [ urlpatterns = [
path('login/', log_in, name='login'), path('login/', log_in, name='login'),
path('register/', views.register, name='register'),
path('logout/', views.log_out, name='logout'), path('logout/', views.log_out, name='logout'),
path('edit-profile/', views.edit_profile, name='edit_profile'), path('edit-profile/', views.edit_profile, name='edit_profile'),
path('password-change/', views.password_change, name='password_change'), path('password-change/', views.password_change, name='password_change'),

View File

@ -1,5 +1,6 @@
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.forms import PasswordChangeForm
from .forms import OurUserCreationForm
from django.contrib import messages from django.contrib import messages
from django.contrib.auth import logout, update_session_auth_hash from django.contrib.auth import logout, update_session_auth_hash
from .forms import EditProfileForm from .forms import EditProfileForm
@ -20,6 +21,20 @@ def password_change(request):
return render(request, 'base_form.html', {'form': form}) 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): def log_out(request):
if not request.user.is_authenticated: if not request.user.is_authenticated:
return redirect('audio:home') return redirect('audio:home')

View File

@ -14,6 +14,7 @@
{% url 'accounts:disable_totp' as disable_totp_url %} {% url 'accounts:disable_totp' as disable_totp_url %}
{% url 'audio:new_feed' as new_feed_url %} {% url 'audio:new_feed' as new_feed_url %}
{% url 'audio:feeds' as feeds_url %} {% url 'audio:feeds' as feeds_url %}
{% url 'accounts:register' as register_url %}
{% if request.path == home_url %} {% if request.path == home_url %}
Home Home
@ -33,6 +34,8 @@
New Feed? New Feed?
{% elif request.path == feeds_url %} {% elif request.path == feeds_url %}
Feeds Feeds
{% elif request.path == register_url %}
Register
{% endif %} {% endif %}
{{ title }} {{ title }}

View File

@ -8,6 +8,7 @@
{% url 'accounts:password_change' as password_change_url %} {% url 'accounts:password_change' as password_change_url %}
{% url 'audio:new_feed' as new_feed_url %} {% url 'audio:new_feed' as new_feed_url %}
{% url 'accounts:password_reset' as password_reset_url %} {% url 'accounts:password_reset' as password_reset_url %}
{% url 'accounts:register' as register_url %}
{% if request.path == login_url %} {% if request.path == login_url %}
{% firstof 'Login' as submit %} {% firstof 'Login' as submit %}
@ -19,6 +20,8 @@
{% firstof 'Submit' as submit %} {% firstof 'Submit' as submit %}
{% elif request.path == password_reset_url %} {% elif request.path == password_reset_url %}
{% firstof 'Reset Password' as submit %} {% firstof 'Reset Password' as submit %}
{% elif request.path == register_url %}
{% firstof 'Register' as submit %}
{% endif %} {% endif %}
{% include "base_navbar.html" %} {% include "base_navbar.html" %}
@ -59,6 +62,7 @@
{% endif %} {% endif %}
{% if request.path == login_url %} {% if request.path == login_url %}
<p>Forgot Password? <a href="{% url 'accounts:password_reset' %}">Reset Password</a></p> <p>Forgot Password? <a href="{% url 'accounts:password_reset' %}">Reset Password</a></p>
<p>Need an account? <a href="{% url 'accounts:register' %}">Register</a></p>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View File

@ -12,6 +12,7 @@
{% url 'accounts:password_reset' as password_reset_url %} {% url 'accounts:password_reset' as password_reset_url %}
{% url 'accounts:password_reset_done' as password_reset_done_url %} {% url 'accounts:password_reset_done' as password_reset_done_url %}
{% url 'accounts:password_reset_complete' as password_reset_complete_url %} {% url 'accounts:password_reset_complete' as password_reset_complete_url %}
{% url 'accounts:register' as register_url %}
{% if request.path == login_url %} {% if request.path == login_url %}
Login? Login?
@ -35,6 +36,8 @@
Password Reset Sent Password Reset Sent
{% elif request.path == password_reset_complete_url %} {% elif request.path == password_reset_complete_url %}
Password Reset Complete Password Reset Complete
{% elif request.path == register_url %}
Register?
{% endif %} {% endif %}
{{ heading }} {{ heading }}