From 4e23d4547221e25f98dec15b09f00810accd8b75 Mon Sep 17 00:00:00 2001 From: krek0 Date: Sun, 17 May 2026 07:08:29 +0200 Subject: [PATCH] Add display name (first_name) shown instead of username on user-facing UI --- allauth_oauth/signals.py | 5 +++++ photo21/forms.py | 12 ++++++++++++ photo21/templates/base.html | 2 +- photologue/templates/photologue/gallery_detail.html | 4 ++-- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/allauth_oauth/signals.py b/allauth_oauth/signals.py index 30ebf4e..eb8ae56 100644 --- a/allauth_oauth/signals.py +++ b/allauth_oauth/signals.py @@ -29,6 +29,11 @@ def sync_user_fields(sender, request, sociallogin, **kwargs): user.username = username changed = True + display_name = data.get("name") or data.get("preferred_username") + if display_name and user.first_name != display_name: + user.first_name = display_name + changed = True + staff_groups = settings.OAUTH_STAFF_GROUPS if staff_groups: oauth_groups = set(data.get("groups", [])) diff --git a/photo21/forms.py b/photo21/forms.py index 253fab3..cd47e6a 100644 --- a/photo21/forms.py +++ b/photo21/forms.py @@ -8,6 +8,12 @@ from django.utils.translation import gettext_lazy as _ class CustomSignupForm(SignupForm): + first_name = forms.CharField( + label=_("Display name"), + max_length=150, + required=True, + ) + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -16,6 +22,12 @@ class CustomSignupForm(SignupForm): "Please enter a valid email address ending with `@ens-rennes.fr`" ) + def save(self, request): + user = super().save(request) + user.first_name = self.cleaned_data["first_name"] + user.save() + return user + def clean_email(self): """ Check that the email address ends with a trusted domain. diff --git a/photo21/templates/base.html b/photo21/templates/base.html index cf80e1b..ecccd2c 100644 --- a/photo21/templates/base.html +++ b/photo21/templates/base.html @@ -132,7 +132,7 @@ SPDX-License-Identifier: GPL-3.0-or-later