Skip to content

Commit

Permalink
feat(user-profile): support for fields taken from user
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 12, 2023
1 parent bf94ab9 commit f6e1e22
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
43 changes: 41 additions & 2 deletions fiesta/apps/accounts/forms/profile.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -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",
Expand All @@ -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")
16 changes: 8 additions & 8 deletions fiesta/apps/accounts/templates/accounts/user_profile/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h1 class="card-title pb-1 mb-3 border-b-2">{% trans "Basic information" %}</h1>
</tr>
<tr>
<td class="font-semibold pr-12">Email:</td>
<td>{{ user.email_user }}</td>
<td>{{ user.primary_email }}</td>
</tr>
<tr>
<td class="font-semibold pr-12">Gender:</td>
Expand Down Expand Up @@ -82,8 +82,10 @@ <h1 class="card-title pb-1 mb-3 border-b-2">{% trans "Socials and contact" %}</h
<tr>
<td class="font-semibold pr-12 flex-shrink-0">Phone number:</td>
<td class="flex-grow">
<a class="underline text-blue-600 hover:text-blue-800 visited:text-purple-600 block"
href="tel:{{ profile.phone_number }}">{{ profile.phone_number }}</a>
{% if profile.phone_number %}
<a class="underline text-blue-600 hover:text-blue-800 visited:text-purple-600 block"
href="tel:{{ profile.phone_number }}">{{ profile.phone_number }}</a>
{% endif %}
</td>
</tr>
<tr>
Expand All @@ -109,11 +111,9 @@ <h1 class="card-title pb-1 mb-3 border-b-2">{% trans "Socials and contact" %}</h
</tr>
<tr>
<td class="font-semibold pr-12 flex-shrink-0">Whatsapp:</td>
<td class="flex-grow">{{ profile.whatsapp }}</td>
</tr>
<tr>
<td class="font-semibold pr-12 flex-shrink-0">Email:</td>
<td class="flex-grow">{{ user.primary_email }}</td>
<td class="flex-grow">
{% if profile.whatsapp %}{{ profile.whatsapp }}{% endif %}
</td>
</tr>
{% endwith %}
</table>
Expand Down

0 comments on commit f6e1e22

Please sign in to comment.