mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-16 22:07:03 +00:00
old user, first registration. cleaner
This commit is contained in:
@@ -162,30 +162,33 @@ def register(request, url_username=None):
|
|||||||
else:
|
else:
|
||||||
form = register_form(initial=initial_values)
|
form = register_form(initial=initial_values)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
form = register_form(request.POST)
|
form = register_form(request.POST)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
print("POST VALID")
|
print("POST VALID") # so now username and email fields are readonly
|
||||||
un = form.cleaned_data["username"]
|
un = form.cleaned_data["username"]
|
||||||
pw= form.cleaned_data["password1"]
|
pw= form.cleaned_data["password1"]
|
||||||
email = form.cleaned_data["email"]
|
email = form.cleaned_data["email"]
|
||||||
expoers = User.objects.filter(username=un)
|
expoers = User.objects.filter(username=un)
|
||||||
if len(expoers) != 0:
|
# if this is LOGONABLE user and we are not logged on
|
||||||
# this is a password re-set, not a new registration. So we need to check it is the same person.
|
# NOT just save the data ! Anyone could do that..
|
||||||
form_user = expoers[0]
|
# we are now in a state where password should only be re-set by email token
|
||||||
if request.user != form_user:
|
# but rather than redirect (off-putting) we just make the password fields read-only
|
||||||
print(f"## UNAUTHORIZED Password reset ## {request.user} {form_user}")
|
if len(expoers) > 0:
|
||||||
# return render(request, "login/register.html", {"form": form, "unauthorized": True})
|
form.fields["password1"].widget.attrs["readonly"]="readonly"
|
||||||
|
form.fields["password2"].widget.attrs["readonly"]="readonly"
|
||||||
|
|
||||||
# create User in the system and refresh stored encrypted user list and git commit it:
|
# create User in the system and refresh stored encrypted user list and git commit it:
|
||||||
updated_user = register_user(un, email, password=pw, pwhash=None)
|
updated_user = register_user(un, email, password=pw, pwhash=None)
|
||||||
save_users(request, updated_user, email)
|
save_users(request, updated_user, email)
|
||||||
# to do, login automatically, and redirect to control panel ?
|
# to do, login automatically, and redirect to control panel ?
|
||||||
return HttpResponseRedirect("/accounts/login/")
|
form.fields["username"].widget.attrs["readonly"]="readonly"
|
||||||
|
form.fields["email"].widget.attrs["readonly"]="readonly"
|
||||||
|
return render(request, "login/register.html", {"form": form, "email_stored": True})
|
||||||
|
# return HttpResponseRedirect("/accounts/login/")
|
||||||
else: # GET
|
else: # GET
|
||||||
pass
|
pass
|
||||||
return render(request, "login/register.html", {"form": form, "warning": warning, "logged_in": logged_in})
|
return render(request, "login/register.html", {"form": form})
|
||||||
|
|
||||||
|
|
||||||
def save_users(request, updated_user, email="troggle@exposerver.expo"):
|
def save_users(request, updated_user, email="troggle@exposerver.expo"):
|
||||||
@@ -350,7 +353,13 @@ class register_form(forms.Form): # not a model-form, just a form-form
|
|||||||
)
|
)
|
||||||
email = cleaned_data.get("email")
|
email = cleaned_data.get("email")
|
||||||
users = User.objects.filter(email=email)
|
users = User.objects.filter(email=email)
|
||||||
if len(users) != 0:
|
if len(users) > 1:
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
"Duplicate email address. Another registered user is already using this email address. Email addresses must be unique as that is how we reset forgotten passwords."
|
f"Duplicate email address. Another registered user {users} is already using this email address. Email addresses must be unique as that is how we reset forgotten passwords."
|
||||||
)
|
)
|
||||||
|
if len(users) == 1:
|
||||||
|
if users[0].username != un:
|
||||||
|
raise ValidationError(
|
||||||
|
f"Duplicate email address. Another registered user '{users[0]}' is already using this email address. Email addresses must be unique as that is how we reset forgotten passwords."
|
||||||
|
)
|
||||||
|
|
||||||
@@ -68,11 +68,19 @@ So type in the same email address that you use there if you have already signed
|
|||||||
<div style='width: 700px; font-family: monospace; font-weight: bold; font-size: 150%; text-align: right; '>
|
<div style='width: 700px; font-family: monospace; font-weight: bold; font-size: 150%; text-align: right; '>
|
||||||
<form method="post" accept-charset="utf-8">{% csrf_token %}
|
<form method="post" accept-charset="utf-8">{% csrf_token %}
|
||||||
<p>
|
<p>
|
||||||
|
{% if email_stored %}
|
||||||
|
<label for="id_username">Username <span style="color:blue">(checked)</span>:</label>
|
||||||
|
{% else %}
|
||||||
<label for="id_username">Username:</label>
|
<label for="id_username">Username:</label>
|
||||||
|
{% endif %}
|
||||||
{{form.username}}
|
{{form.username}}
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
|
{% if email_stored %}
|
||||||
|
<label for="id_email">email <span style="color:blue">(stored)</span>:</label>
|
||||||
|
{% else %}
|
||||||
<label for="id_email">email:</label>
|
<label for="id_email">email:</label>
|
||||||
|
{% endif %}
|
||||||
{{form.email}}
|
{{form.email}}
|
||||||
</p>
|
</p>
|
||||||
{% if logged_in %}<!-- one we have initially logged in,
|
{% if logged_in %}<!-- one we have initially logged in,
|
||||||
@@ -104,26 +112,39 @@ all later password chnages are done ONLY via email token password re-set-->
|
|||||||
Get login token by email →
|
Get login token by email →
|
||||||
</button>
|
</button>
|
||||||
{% else %}
|
{% else %}
|
||||||
<button class="fancybutton"
|
<button class="fancybutton" type="button"
|
||||||
{% if logged_in %}
|
{% if logged_in or email_stored %}
|
||||||
style="padding: 0.5em 25px; font-size: 100%;"
|
style="padding: 0.5em 25px; font-size: 100%;"
|
||||||
{% else %}
|
{% else %}
|
||||||
style="padding: 0.5em 25px; font-size: 100%; background: silver;"
|
style="padding: 0.5em 25px; font-size: 100%; background: silver;"
|
||||||
disabled
|
disabled
|
||||||
{% endif %}
|
{% endif %}
|
||||||
onclick="window.location.href='/accounts/password_reset/'" value = "Go to" >
|
onclick="window.location.href='/accounts/password_reset/'" value = "Go to" >
|
||||||
Reset password
|
Confirm email
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
|
||||||
<button class="fancybutton" style="padding: 0.5em 25px; font-size: 100%;" type = "submit" >
|
|
||||||
{% if logged_in %}
|
{% if logged_in %}
|
||||||
|
<button class="fancybutton" style="padding: 0.5em 25px; font-size: 100%;" type = "submit" >
|
||||||
Change or confirm email →
|
Change or confirm email →
|
||||||
{% else %}
|
|
||||||
Register →
|
|
||||||
{% endif %}
|
|
||||||
</button>
|
</button>
|
||||||
|
{% else %}
|
||||||
|
{% if email_stored %}
|
||||||
|
{% else %}
|
||||||
|
<button class="fancybutton" style="padding: 0.5em 25px; font-size: 100%;" type = "submit" >
|
||||||
|
Register →
|
||||||
|
</button>
|
||||||
|
{% endif %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{%endif %}
|
{%endif %}
|
||||||
|
{% if email_stored %}
|
||||||
|
<button class="fancybutton" type="button"
|
||||||
|
style="padding: 0.5em 25px; font-size: 100%;"
|
||||||
|
onclick="window.location.href='/accounts/login/'" value = "Go to" >
|
||||||
|
Login →
|
||||||
|
</button>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -24,6 +24,10 @@ Email change - for a personal login to Troggle
|
|||||||
<!--using template login/register_email.html -->
|
<!--using template login/register_email.html -->
|
||||||
</div>
|
</div>
|
||||||
<!--ONLY for an expo-valid logged-on User
|
<!--ONLY for an expo-valid logged-on User
|
||||||
|
|
||||||
|
The reason for separating this template from the other one (register.html) is that the logic
|
||||||
|
gets very confused. So despite the partial duplication, it is easier to debug and maintain by
|
||||||
|
having two separate templates.
|
||||||
-->
|
-->
|
||||||
<h3>Register your email address</h3>
|
<h3>Register your email address</h3>
|
||||||
|
|
||||||
|
|||||||
3
urls.py
3
urls.py
@@ -1,7 +1,7 @@
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.conf.urls.static import static
|
from django.conf.urls.static import static
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.contrib.auth.views import PasswordResetView # class-based view
|
from django.contrib.auth.views import PasswordResetView, PasswordResetConfirmView # class-based views
|
||||||
|
|
||||||
from django.urls import include, path, re_path
|
from django.urls import include, path, re_path
|
||||||
|
|
||||||
@@ -177,6 +177,7 @@ trogglepatterns = [
|
|||||||
path("accounts/newregister/", newregister, name="newregister"),
|
path("accounts/newregister/", newregister, name="newregister"),
|
||||||
path("accounts/reset/done/", reset_done, name="password_reset_done"), # overriding django.contrib.auth.urls
|
path("accounts/reset/done/", reset_done, name="password_reset_done"), # overriding django.contrib.auth.urls
|
||||||
path('accounts/password_reset/', PasswordResetView.as_view(form_class=ExpoPasswordResetForm), name='password_reset'),
|
path('accounts/password_reset/', PasswordResetView.as_view(form_class=ExpoPasswordResetForm), name='password_reset'),
|
||||||
|
path('accounts/reset/<uidb64>/<token>/', PasswordResetConfirmView.as_view(), name="password_reset_confirm"),
|
||||||
path('accounts/', include('django.contrib.auth.urls')), # see line 109 in this file NB initial "/accounts/" in URL
|
path('accounts/', include('django.contrib.auth.urls')), # see line 109 in this file NB initial "/accounts/" in URL
|
||||||
|
|
||||||
path('person/<slug:slug>', person, name="person"),
|
path('person/<slug:slug>', person, name="person"),
|
||||||
|
|||||||
Reference in New Issue
Block a user