From f6e1e22165c00cd2058639348cf63f48447415ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Kol=C3=A1=C5=99?= Date: Sun, 12 Nov 2023 16:07:02 +0100 Subject: [PATCH] feat(user-profile): support for fields taken from user --- fiesta/apps/accounts/forms/profile.py | 43 ++++++++++++++++++- .../accounts/user_profile/detail.html | 16 +++---- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/fiesta/apps/accounts/forms/profile.py b/fiesta/apps/accounts/forms/profile.py index 3b962bf0..4bf90986 100644 --- a/fiesta/apps/accounts/forms/profile.py +++ b/fiesta/apps/accounts/forms/profile.py @@ -1,7 +1,7 @@ from __future__ import annotations from django.db.models import Field -from django.forms import Field as FormField, modelform_factory +from django.forms import Field as FormField, fields_for_model, modelform_factory from django.utils.translation import gettext_lazy as _ from apps.accounts.models import User, UserProfile @@ -10,6 +10,22 @@ from apps.fiestaforms.widgets.models import FacultyWidget, UniversityWidget from apps.sections.models import SectionMembership, SectionsConfiguration +FIELDS_FROM_USER = ("first_name", "last_name") +REQUIRED_FIELDS_FROM_USER = FIELDS_FROM_USER + + +def _create_user_fields(): + fields = fields_for_model( + User, + fields=FIELDS_FROM_USER, + ) + for f in REQUIRED_FIELDS_FROM_USER: + fields[f].required = True + return fields + + +USER_FIELDS = _create_user_fields() + class UserProfileForm(BaseModelForm): FIELDS_TO_CONFIGURATION = { @@ -33,7 +49,9 @@ def get_form_fields(cls, user: User): for field_name, conf_field in cls._FIELD_NAMES_TO_CONFIGURATION.items() if any(conf_field.__get__(c) is not None for c in confs) ) - return fields_to_include + cls.Meta.fields + all_fields = fields_to_include + cls.Meta.fields + # first all required fields, after them all the rest in original order + return sorted(set(all_fields), key=lambda f: (not UserProfileForm.base_fields[f].required, all_fields.index(f))) @classmethod def get_user_configuration(cls, user: User): @@ -75,6 +93,9 @@ def callback(f: Field, **kwargs) -> FormField: formfield_callback=callback, ) + # include pre-generated field from User + locals().update(USER_FIELDS) + class Meta: model = UserProfile @@ -83,7 +104,9 @@ class Meta: } fields = ( + *USER_FIELDS.keys(), # TODO: think about limiting the choices by country of section, in which is current membership + "nationality", "university", "faculty", "picture", @@ -99,6 +122,22 @@ class Meta: "faculty": FacultyWidget, } + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + if up := self.instance: # type: UserProfile + for f in USER_FIELDS: + self.initial[f] = getattr(up.user, f, None) + + def save(self, commit=True): + instance: UserProfile = super().save(commit=commit) + + for f in USER_FIELDS: + setattr(instance.user, f, self.cleaned_data.get(f)) + instance.user.save(update_fields=USER_FIELDS.keys()) + + return instance + class UserProfileFinishForm(UserProfileForm): submit_text = _("Finish Profile") diff --git a/fiesta/apps/accounts/templates/accounts/user_profile/detail.html b/fiesta/apps/accounts/templates/accounts/user_profile/detail.html index 74add5c0..c711e44c 100644 --- a/fiesta/apps/accounts/templates/accounts/user_profile/detail.html +++ b/fiesta/apps/accounts/templates/accounts/user_profile/detail.html @@ -44,7 +44,7 @@

{% trans "Basic information" %}

Email: - {{ user.email_user }} + {{ user.primary_email }} Gender: @@ -82,8 +82,10 @@

{% trans "Socials and contact" %} Phone number: - {{ profile.phone_number }} + {% if profile.phone_number %} + {{ profile.phone_number }} + {% endif %} @@ -109,11 +111,9 @@

{% trans "Socials and contact" %} Whatsapp: - {{ profile.whatsapp }} - - - Email: - {{ user.primary_email }} + + {% if profile.whatsapp %}{{ profile.whatsapp }}{% endif %} + {% endwith %}