2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-17 16:17:12 +00:00

New User registration form

This commit is contained in:
2025-01-23 23:38:06 +00:00
parent f842dab12a
commit a5d0ad3e4f
5 changed files with 173 additions and 38 deletions

View File

@@ -6,9 +6,11 @@ from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
from django.contrib.auth.forms import PasswordResetForm
from troggle.core.models.troggle import DataIssue, Person
from troggle.parsers.users import register_user, get_encryptor, ENCRYPTED_DIR, USERS_FILE
from troggle.parsers.people import troggle_slugify
from troggle.core.utils import (
add_commit,
)
@@ -23,6 +25,23 @@ todo = """
- login automatically, and redirect to control panel ?
"""
class ExpoPasswordResetForm(PasswordResetForm):
"""Because we are using the Django-standard django.contrib.auth mechanisms, the way Django wants us to
modify them is to subclass their stuff and insert our extras in teh subclasses. This is completely
unlike how the rest of troggle works because we avoid Class-based views.
We avoid them because they are very hard to debug for newcomer programmers who don't know Django:
the logic is spread out up a tree of preceding ancestor classes.
This is where we would override the template so make the form look visually like the rest of troggle
"""
def clean_email(self):
email = self.cleaned_data.get('email')
# Add custom validation logic etc.
print(f" * ExpoPasswordResetForm PASSWORD reset email posted '{email=}'")
# method_list = [attribute for attribute in dir(PasswordResetForm) if callable(getattr(PasswordResetForm, attribute)) and attribute.startswith('__') is False]
# print(method_list)
return email
def reset_done(request):
"""This page is called when a password reset has successively occured
Unfortunately by this point, we do not know the name of the user who initiated the
@@ -38,8 +57,43 @@ def reset_done(request):
"""
current_user = request.user
save_users(request, current_user)
return HttpResponseRedirect("/accounts/login/")
if updated_user.is_anonymous:
# What we expect, for a completely new user
return HttpResponseRedirect("/accounts/login/?next=/handbook/troggle/training/trogbegin.html")
else:
# This would be for someone already looged in "expo" for example
return HttpResponseRedirect("/handbook/troggle/training/trogbegin.html")
def newregister(request, username=None):
"""To register a COMPLETELY new user on the troggle system,
WITHOUT any previous expo attendance.
"""
current_user = request.user # if not logged in, this is 'AnonymousUser'
warning = ""
if request.method == "POST":
form = newregister_form(request.POST)
if form.is_valid():
fullname = form.cleaned_data["fullname"]
email = form.cleaned_data["email"]
nameslug = troggle_slugify(fullname)
print(f"NEW user slug {nameslug}")
expoers = User.objects.filter(username=nameslug)
if len(expoers) != 0:
# Disallow a name which already exists, use the other form.
return HttpResponseRedirect(f"/accounts/register/{nameslug}")
# create User in the system and refresh stored encrypted user list and git commit it:
updated_user = register_user(nameslug, email, password=None, pwhash=None, fullname=fullname)
save_users(request, updated_user, email)
return HttpResponseRedirect("/accounts/password_reset/")
else: # GET
form = newregister_form(initial={"visible": "True"})
return render(request, "login/register.html", {"form": form, "warning": warning, "newuser": True})
def register(request, username=None):
"""To register a new user on the troggle system, similar to the "expo" user
(with cavey:beery password) but specific to an individual
@@ -65,8 +119,8 @@ def register(request, username=None):
save_users(request, updated_user, email)
# to do, login automatically, and redirect to control panel ?
return HttpResponseRedirect("/accounts/login/")
else:
if username:
else: # GET
if username: # if provided in URL
if not current_user.is_anonymous:
warning = f"WARNING - you are logged-in as someone else '{current_user}'. You must logout and login again as '{username}' "
print(f"REGISTER: {warning}")
@@ -126,6 +180,34 @@ def write_users(registered_users, encryptedfile, git_string):
raise
return True
class newregister_form(forms.Form): # not a model-form, just a form-form
fullname = forms.CharField(strip=True, required=True,
label="Forename Surname",
widget=forms.TextInput(
attrs={"size": 35, "placeholder": "e.g. Anathema Device",
"style": "vertical-align: text-top;"}
))
email = forms.CharField(strip=True, required=True,
label="email",
widget=forms.TextInput(
attrs={"size": 35, "placeholder": "e.g. anathema@tackle_store.expo",
"style": "vertical-align: text-top;"}
))
def clean(self):
cleaned_data = super().clean()
un = cleaned_data.get("fullname")
# expoers = Person.objects.filter(slug=un)
# if len(expoers) == 0:
# raise ValidationError(
# "Sorry, we are not registering new people yet. Try again next week. We are still getting the bugs out of this.."
# )
# if len(expoers) != 1:
# raise ValidationError(
# "Sorry, that troggle identifier has duplicates. Contact a nerd on the Nerd email list, or (better) the Matrix website chat."
# )
class register_form(forms.Form): # not a model-form, just a form-form
username = forms.CharField(strip=True, required=True,
label="Username",
@@ -170,7 +252,7 @@ class register_form(forms.Form): # not a model-form, just a form-form
expoers = Person.objects.filter(slug=un)
if len(expoers) == 0:
raise ValidationError(
"Sorry, we are not registering new people yet. Try again next week. We are still getting the bugs out of this.."
"Sorry, this is the form for people who have already been to expo. Use the New User registration form (link above)."
)
if len(expoers) != 1:
raise ValidationError(