Skip to content

Commit

Permalink
forms
Browse files Browse the repository at this point in the history
  • Loading branch information
petrceska committed Oct 5, 2023
1 parent 48df26f commit 35e1217
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
36 changes: 26 additions & 10 deletions fiesta/apps/events/forms/event.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = (
Expand All @@ -73,9 +87,11 @@ class Meta:
"landscape_cover",
"portrait_cover",
"section",
"author"
"author",
"add_organizer"
)
widgets = {
"section": HiddenInput,
"author": HiddenInput,
}

8 changes: 7 additions & 1 deletion fiesta/apps/events/views/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 35e1217

Please sign in to comment.