From b0226abbff3ecdcf2baed3e711c5957d779c1cb6 Mon Sep 17 00:00:00 2001 From: laykusero04 Date: Mon, 1 Dec 2025 19:58:44 +0800 Subject: [PATCH] Add occupation field to EditUserForm and update profile template; implement EmailBackend for authentication --- accounts_app/backends.py | 27 +++++++ accounts_app/forms/edit_user_form.py | 2 +- .../templates/accounts_app/profile.html | 72 ++++++++++++++++++- accounts_app/views/invite_user.py | 35 +++++++-- django_htmx_coding_challenge/settings.py | 4 ++ 5 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 accounts_app/backends.py diff --git a/accounts_app/backends.py b/accounts_app/backends.py new file mode 100644 index 0000000..7cf0fe5 --- /dev/null +++ b/accounts_app/backends.py @@ -0,0 +1,27 @@ +from django.contrib.auth.backends import ModelBackend +from django.contrib.auth import get_user_model + +User = get_user_model() + + +class EmailBackend(ModelBackend): + """ + Custom authentication backend that authenticates users using email instead of username. + """ + + def authenticate(self, request, email=None, password=None, **kwargs): + if email is None or password is None: + return None + + try: + user = User.objects.get(email=email) + except User.DoesNotExist: + # Run the default password hasher once to reduce timing attacks + User().set_password(password) + return None + + if user.check_password(password) and self.user_can_authenticate(user): + return user + + return None + diff --git a/accounts_app/forms/edit_user_form.py b/accounts_app/forms/edit_user_form.py index 53fbcef..4469eac 100644 --- a/accounts_app/forms/edit_user_form.py +++ b/accounts_app/forms/edit_user_form.py @@ -6,4 +6,4 @@ class EditUserForm(forms.ModelForm): class Meta: model = User - fields = ["first_name", "last_name"] \ No newline at end of file + fields = ["first_name", "last_name", "occupation"] \ No newline at end of file diff --git a/accounts_app/templates/accounts_app/profile.html b/accounts_app/templates/accounts_app/profile.html index 31b5137..ea59233 100644 --- a/accounts_app/templates/accounts_app/profile.html +++ b/accounts_app/templates/accounts_app/profile.html @@ -10,9 +10,10 @@

Welcome back, {{ user.full_na id="edit_profile_form" hx-post="{% url 'home' %}" hx-trigger="submit" - hx-swap="multi:#edit_profile_form:outerHTML,h1:outerHTML" + hx-swap="multi:#edit_profile_form:outerHTML,#edit_profile_header:outerHTML" hx-ext="multi" > + {% csrf_token %}
Welcome back, {{ user.full_na required >
+
+ + +
+ + + + + + +