Skip to content

Commit

Permalink
fix(buddy-request): fix creating request without faculty in profile
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 10, 2023
1 parent dc5d66e commit 186af5f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion fiesta/apps/buddy_system/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,17 +32,20 @@ class Meta:
"interests",
"responsible_section",
"issuer",
"issuer_faculty",
)
field_classes = {
"interests": ChoicedArrayField,
}
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(
Expand All @@ -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

Expand Down
13 changes: 13 additions & 0 deletions fiesta/apps/buddy_system/views/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 186af5f

Please sign in to comment.