From 186af5f73c67f6f14348fab3c979567dacaff45f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Kol=C3=A1=C5=99?= Date: Fri, 10 Nov 2023 22:08:30 +0100 Subject: [PATCH] fix(buddy-request): fix creating request without faculty in profile --- fiesta/apps/buddy_system/forms.py | 11 ++++++++++- fiesta/apps/buddy_system/views/request.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/fiesta/apps/buddy_system/forms.py b/fiesta/apps/buddy_system/forms.py index 445c407c..be83bb00 100644 --- a/fiesta/apps/buddy_system/forms.py +++ b/fiesta/apps/buddy_system/forms.py @@ -9,7 +9,7 @@ from apps.buddy_system.models import BuddyRequest, BuddyRequestMatch from apps.fiestaforms.fields.array import ChoicedArrayField from apps.fiestaforms.forms import BaseModelForm -from apps.fiestaforms.widgets.models import ActiveLocalMembersFromSectionWidget, UserWidget +from apps.fiestaforms.widgets.models import ActiveLocalMembersFromSectionWidget, FacultyWidget, UserWidget USER_PROFILE_CONTACT_FIELDS = fields_for_model( UserProfile, @@ -32,6 +32,7 @@ class Meta: "interests", "responsible_section", "issuer", + "issuer_faculty", ) field_classes = { "interests": ChoicedArrayField, @@ -39,10 +40,12 @@ class Meta: widgets = { "responsible_section": HiddenInput, "issuer": HiddenInput, + "issuer_faculty": FacultyWidget, } labels = { "note": _("Tell us about yourself"), "interests": _("What are you into?"), + "issuer_faculty": _("Your faculty"), } help_texts = { "note": lazy( @@ -51,6 +54,12 @@ class Meta: ) } + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + if self.initial.get("issuer_faculty"): + self.fields["issuer_faculty"].disabled = True + # TODO: add save/load of contacts to/from user_profile diff --git a/fiesta/apps/buddy_system/views/request.py b/fiesta/apps/buddy_system/views/request.py index a7fc5e38..5f535a45 100644 --- a/fiesta/apps/buddy_system/views/request.py +++ b/fiesta/apps/buddy_system/views/request.py @@ -11,6 +11,7 @@ from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, TemplateView +from apps.accounts.models import UserProfile from apps.buddy_system.forms import NewBuddyRequestForm from apps.plugins.views import PluginConfigurationViewMixin from apps.sections.models import SectionMembership, SectionsConfiguration @@ -103,13 +104,25 @@ class NewRequestView( success_url = reverse_lazy("buddy_system:index") def get_initial(self): + p: UserProfile = self.request.user.profile_or_none return { "responsible_section": self.request.in_space_of_section, "issuer": self.request.user, + "issuer_faculty": p.faculty if p else None, } def form_valid(self, form): # override to be sure form.instance.responsible_section = self.request.in_space_of_section form.instance.issuer = self.request.user + + profile: UserProfile = self.request.user.profile_or_none + if not profile.faculty: + profile.faculty = form.instance.issuer_faculty + profile.save(update_fields=("faculty",)) + + if not profile.university: + profile.university = form.instance.issuer_faculty.university + profile.save(update_fields=("university",)) + return super().form_valid(form)