mirror of
https://github.com/TrentSPalmer/trentpalmerdotorg.git
synced 2025-08-22 21:43:57 -07:00
add registration
This commit is contained in:
@@ -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,
|
||||
|
@@ -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'),
|
||||
|
@@ -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')
|
||||
|
Reference in New Issue
Block a user