From 35e12176d97df8a738e288b4466555afc4679c63 Mon Sep 17 00:00:00 2001 From: petrceska Date: Thu, 5 Oct 2023 23:08:38 +0200 Subject: [PATCH] forms --- fiesta/apps/events/forms/event.py | 36 ++++++++++++++++++++++--------- fiesta/apps/events/views/event.py | 8 ++++++- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/fiesta/apps/events/forms/event.py b/fiesta/apps/events/forms/event.py index 4ebeb580..ccbc201c 100644 --- a/fiesta/apps/events/forms/event.py +++ b/fiesta/apps/events/forms/event.py @@ -1,6 +1,7 @@ +from apps.events.models.organizer import OrganizerRole from apps.fiestaforms.forms import BaseModelForm from apps.events.models import Event, Organizer -from django.forms import Field as FormField, modelform_factory, CharField, HiddenInput, ChoiceField +from django.forms import CharField, HiddenInput, MultipleChoiceField, ModelMultipleChoiceField from django.utils.translation import gettext_lazy as _ from apps.sections.models import SectionMembership @@ -41,24 +42,37 @@ class Meta: "author": HiddenInput, } + class UpdateEventForm(BaseModelForm): # section_name = CharField(label=_("ESN section"), disabled=True) # author_name = CharField(label=_("Author of the event"), disabled=True) - memberships_queryset = SectionMembership.objects.all() - membership_choices = [(membership.user.id, membership.user.full_name_official) for membership in memberships_queryset] - add_organizer = ChoiceField(choices=membership_choices, label=_("Add organizer"), required=False) + add_organizer = MultipleChoiceField(label=_("Add organizer"), required=False, queryset=SectionMembership.objects.none()) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + memberships_queryset = self.initial["section"].memberships.all() + membership_choices = [(str(membership.user.id), membership.user.full_name_official) for membership in + memberships_queryset] + + self.fields["add_organizer"].queryset = membership_choices + + def save(self, commit=True): + event = super().save(commit=commit) - # self.fields["section"].disabled = True - # self.fields["author"].disabled = True + # Get the selected organizer user IDs from cleaned_data + organizer_user_ids = self.cleaned_data.get("add_organizer", []) - # self.initial["section_name"] = self.initial["section"].name - # self.initial["author_name"] = self.initial["author"].full_name + # Create organizers for the selected users + for user_id in organizer_user_ids: + Organizer.objects.create( + user_id=user_id, + event=event, + state=OrganizerRole.OC, + ) + + return event - class Meta: model = Event fields = ( @@ -73,9 +87,11 @@ class Meta: "landscape_cover", "portrait_cover", "section", - "author" + "author", + "add_organizer" ) widgets = { "section": HiddenInput, "author": HiddenInput, } + diff --git a/fiesta/apps/events/views/event.py b/fiesta/apps/events/views/event.py index 42ba3a82..e52e61ce 100644 --- a/fiesta/apps/events/views/event.py +++ b/fiesta/apps/events/views/event.py @@ -67,13 +67,19 @@ class UpdateEventView( request: HttpRequest object: Event + form_class = UpdateEventForm template_name = 'events/update_event.html' ajax_template_name = 'events/parts/update_event_form.html' - form_class = UpdateEventForm success_message = _("Event updated") + def get_initial(self): + return dict( + section=self.request.in_space_of_section, + ) + + def get_object(self, queryset=None): event_id = self.kwargs.get('pk') return self.request.in_space_of_section.events.get(id=event_id)