From d2213562bb67c4e1ec51c3b51d662f85e4f2c716 Mon Sep 17 00:00:00 2001 From: Calum Andrew Morrell Date: Sun, 9 Nov 2025 11:55:03 +0000 Subject: [PATCH] Added basic code and templates to handle user accounts without user initiated registration. --- accounts/admin.py | 9 ++++++- accounts/models.py | 17 ++++++++++++- accounts/templates/registration/login.html | 17 +++++++++++++ .../registration/password_change_done.html | 9 +++++++ .../registration/password_change_form.html | 14 +++++++++++ .../registration/password_reset_complete.html | 11 ++++++++ .../registration/password_reset_confirm.html | 25 +++++++++++++++++++ .../registration/password_reset_done.html | 13 ++++++++++ .../registration/password_reset_email.html | 10 ++++++++ .../registration/password_reset_form.html | 14 +++++++++++ accounts/urls.py | 5 ++++ 11 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 accounts/templates/registration/login.html create mode 100644 accounts/templates/registration/password_change_done.html create mode 100644 accounts/templates/registration/password_change_form.html create mode 100644 accounts/templates/registration/password_reset_complete.html create mode 100644 accounts/templates/registration/password_reset_confirm.html create mode 100644 accounts/templates/registration/password_reset_done.html create mode 100644 accounts/templates/registration/password_reset_email.html create mode 100644 accounts/templates/registration/password_reset_form.html create mode 100644 accounts/urls.py diff --git a/accounts/admin.py b/accounts/admin.py index 8c38f3f..5b80366 100644 --- a/accounts/admin.py +++ b/accounts/admin.py @@ -1,3 +1,10 @@ from django.contrib import admin +from django.contrib.auth.admin import UserAdmin -# Register your models here. +from .models import User + + +@admin.register(User) +class UserAdmin(UserAdmin): + list_display = ["username", "email", "first_name", "last_name", "display_name"] + list_filter = ["is_active"] diff --git a/accounts/models.py b/accounts/models.py index 71a8362..bece7cc 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -1,3 +1,18 @@ +from django.contrib.auth.models import AbstractUser from django.db import models -# Create your models here. + +class User(AbstractUser): + display_name = models.CharField( + max_length=200, help_text="Display name", blank=True, null=True + ) + + def __str__(self): + if self.display_name: + return self.display_name + elif self.first_name and self.last_name: + return f"{self.first_name} {self.last_name}" + elif self.first_name: + return self.first_name + else: + return self.username diff --git a/accounts/templates/registration/login.html b/accounts/templates/registration/login.html new file mode 100644 index 0000000..31f1bda --- /dev/null +++ b/accounts/templates/registration/login.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Login

+ {% if form.errors %} +

The username and password combination entered did not match an active account.

+ {% endif %} +
+ {% csrf_token %} + {{ form.as_p }} + + Cancel +
+
+{% endblock %} + diff --git a/accounts/templates/registration/password_change_done.html b/accounts/templates/registration/password_change_done.html new file mode 100644 index 0000000..18d49f0 --- /dev/null +++ b/accounts/templates/registration/password_change_done.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Password change complete

+

Your password has been successfully changed. Don't forget your new password!

+
+{% endblock %} + diff --git a/accounts/templates/registration/password_change_form.html b/accounts/templates/registration/password_change_form.html new file mode 100644 index 0000000..845c766 --- /dev/null +++ b/accounts/templates/registration/password_change_form.html @@ -0,0 +1,14 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Change your password

+
+ {% csrf_token %} {{ form.as_p }} + + + Cancel +
+
+{% endblock %} + diff --git a/accounts/templates/registration/password_reset_complete.html b/accounts/templates/registration/password_reset_complete.html new file mode 100644 index 0000000..f790e13 --- /dev/null +++ b/accounts/templates/registration/password_reset_complete.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Password reset

+

+ Your password has been reset successfully. You can login now. +

+
+{% endblock %} + diff --git a/accounts/templates/registration/password_reset_confirm.html b/accounts/templates/registration/password_reset_confirm.html new file mode 100644 index 0000000..14e077f --- /dev/null +++ b/accounts/templates/registration/password_reset_confirm.html @@ -0,0 +1,25 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Reset your password

+ {% if validlink %} +

Enter your new password twice.

+
+ {% csrf_token %} {{ form.as_p }} + + Cancel +
+ {% else %} +

+ The password reset link you clicked was invalid. This may mean it has already been used or too much time + has passed since you requested it. +

+

+ If you have not already reset your password successfully, you may request a new password reset. +

+ {% endif %} +
+{% endblock %} + diff --git a/accounts/templates/registration/password_reset_done.html b/accounts/templates/registration/password_reset_done.html new file mode 100644 index 0000000..fb89d6b --- /dev/null +++ b/accounts/templates/registration/password_reset_done.html @@ -0,0 +1,13 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Password reset requested

+

An email has been sent with a link which will allow you to reset your password.

+

+ If you do not receive it, check your spam or junk folders and ensure you typed the email address connected + with your account. +

+
+{% endblock %} + diff --git a/accounts/templates/registration/password_reset_email.html b/accounts/templates/registration/password_reset_email.html new file mode 100644 index 0000000..ded821b --- /dev/null +++ b/accounts/templates/registration/password_reset_email.html @@ -0,0 +1,10 @@ +A request to reset the password associated with this account has been made on our health website. + +If this was you, please click the following link to reset your password. + +{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %} + +If you did not make this request, we would suggest you log into your account to confirm there has been no malicious activity. You may also want to consider changing your password as a precaution. + +-- +The Dazed Gerbil diff --git a/accounts/templates/registration/password_reset_form.html b/accounts/templates/registration/password_reset_form.html new file mode 100644 index 0000000..63513c2 --- /dev/null +++ b/accounts/templates/registration/password_reset_form.html @@ -0,0 +1,14 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Reset your password

+

Have you forgotten your password? Enter your email address to request a password reset.

+
+ {% csrf_token %} {{ form.as_p }} + + Cancel +
+
+{% endblock %} + diff --git a/accounts/urls.py b/accounts/urls.py new file mode 100644 index 0000000..da28d3c --- /dev/null +++ b/accounts/urls.py @@ -0,0 +1,5 @@ +from django.urls import include, path + +urlpatterns = [ + path("", include("django.contrib.auth.urls")), +]