Skip to content

Commit

Permalink
Update checkboxes in admin formsets
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Aug 14, 2024
1 parent fc02db3 commit 2a45783
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 179 deletions.
42 changes: 5 additions & 37 deletions studioadmin/forms/event_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,12 @@ class EventBaseFormSet(BaseModelFormSet):
def add_fields(self, form, index):
super(EventBaseFormSet, self).add_fields(form, index)

if form.instance:
form.fields['visible_on_site'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': "form-check-input position-static studioadmin-list",
}),
required=False,
label="Visible"
)

form.fields['booking_open'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': "form-check-input position-static studioadmin-list",
}),
required=False
)

form.fields['payment_open'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': "form-check-input position-static studioadmin-list",
}),
required=False
)

form.fields['advance_payment_required'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': "form-check-input position-static studioadmin-list",
}),
required=False
)
form.fields['visible_on_site'] = forms.BooleanField(
label="Visible"
)

form.fields['DELETE'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': 'form-check-input position-static studioadmin-list',
'id': 'DELETE_{}'.format(index)
}),
required=False
)
form.DELETE_id = 'DELETE_{}'.format(index)
for field in form.fields.values():
field.required = False


EventFormSet = modelformset_factory(
Expand Down
51 changes: 9 additions & 42 deletions studioadmin/forms/ticketed_events_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,16 @@ class TicketedEventBaseFormSet(BaseModelFormSet):

def add_fields(self, form, index):
super(TicketedEventBaseFormSet, self).add_fields(form, index)

confirmed_ticket_bookings = form.instance.ticket_bookings.filter(
purchase_confirmed=True
)
if confirmed_ticket_bookings:
form.cannot_delete = True

for field in form.fields.values():
field.required = False

if form.instance:
form.fields['show_on_site'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': "regular-checkbox studioadmin-list",
'id': 'show_on_site_{}'.format(index)
}),
required=False
)
form.show_on_site_id = 'show_on_site_{}'.format(index)

form.fields['payment_open'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': "regular-checkbox studioadmin-list",
'id': 'payment_open_{}'.format(index)
}),
required=False
)
form.payment_open_id = 'payment_open_{}'.format(index)

form.fields['advance_payment_required'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': "regular-checkbox studioadmin-list",
'id': 'advance_payment_required_{}'.format(index)
}),
required=False
)
form.advance_payment_required_id = 'advance_payment_required_{}'.format(index)

confirmed_ticket_bookings = form.instance.ticket_bookings.filter(
purchase_confirmed=True
)
if confirmed_ticket_bookings:
form.cannot_delete = True

form.fields['DELETE'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': 'delete-checkbox studioadmin-list',
'id': 'DELETE_{}'.format(index)
}),
required=False
)
form.DELETE_id = 'DELETE_{}'.format(index)

TicketedEventFormSet = modelformset_factory(
TicketedEvent,
Expand Down
30 changes: 5 additions & 25 deletions studioadmin/forms/timetable_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,18 @@ class SessionBaseFormSet(BaseModelFormSet):
def add_fields(self, form, index):
super(SessionBaseFormSet, self).add_fields(form, index)

if form.instance:
form.formatted_day = DAY_CHOICES[form.instance.day]
form.formatted_day = DAY_CHOICES[form.instance.day]

form.fields['booking_open'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={'class': "form-check-input position-static"}),
required=False
)

form.fields['payment_open'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={'class': "form-check-input position-static"}),
initial=form.instance.payment_open,
required=False
)

form.fields['advance_payment_required'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={'class': "form-check-input position-static"}),
required=False
)

form.fields['DELETE'] = forms.BooleanField(
widget=forms.CheckboxInput(attrs={
'class': 'form-check-input position-static studioadmin-list',
}),
required=False
)
for field in form.fields.values():
field.required = False


TimetableSessionFormSet = modelformset_factory(
Session,
fields=(
'booking_open',
'payment_open', 'advance_payment_required'
'payment_open',
'advance_payment_required'
),
formset=SessionBaseFormSet,
extra=0,
Expand Down
15 changes: 1 addition & 14 deletions studioadmin/tests/test_forms/test_event_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,6 @@ def test_event_formset_delete_with_bookings(self):
deleted_form_with_bookings.cleaned_data['id'], self.event1
)

delete_no_bookings_widget = deleted_form_no_bookings.\
fields['DELETE'].widget
delete_with_bookings_widget = deleted_form_with_bookings.\
fields['DELETE'].widget
self.assertEqual(
delete_no_bookings_widget.attrs['class'],
'form-check-input position-static studioadmin-list'
)
self.assertEqual(
delete_with_bookings_widget.attrs['class'],
'form-check-input position-static studioadmin-list'
)


class EventAdminFormTests(TestCase):

Expand Down Expand Up @@ -629,4 +616,4 @@ def assert_non_cost(form_data):

# non-cost values
for cost in [None, -1, 0]:
assert_non_cost(data)
assert_non_cost(data)
11 changes: 0 additions & 11 deletions studioadmin/tests/test_forms/test_ticketed_event_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,17 +422,6 @@ def test_event_formset_valid(self):
formset = TicketedEventFormSet(data=self.formset_data())
self.assertTrue(formset.is_valid())

def test_additional_form_data(self):
formset = TicketedEventFormSet(
data=self.formset_data(), queryset=TicketedEvent.objects.all()
)
form = formset.forms[0]
self.assertEqual(form.payment_open_id, 'payment_open_0')
self.assertEqual(
form.advance_payment_required_id, 'advance_payment_required_0'
)
self.assertEqual(form.DELETE_id, 'DELETE_0')

def test_can_only_delete_if_no_confirmed_ticket_purchases(self):

tb = baker.make(
Expand Down
4 changes: 2 additions & 2 deletions studioadmin/tests/test_views/test_event_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ def test_cancel_button_shown_for_events_with_bookings(self):
assert event.bookings.all().count() == 1
assert self.event.bookings.all().count() == 0
resp = self.client.get(reverse("studioadmin:events"))
assert 'id="DELETE_0"' in resp.rendered_content
assert 'id="DELETE_1"' not in resp.rendered_content
assert 'form-0-DELETE' in resp.rendered_content
assert 'form-1-DELETE' not in resp.rendered_content
assert 'cancel_button' in resp.rendered_content

def test_can_delete(self):
Expand Down
4 changes: 2 additions & 2 deletions studioadmin/tests/test_views/test_ticketed_events_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ def test_cancel_button_shown_for_events_with_bookings(self):

resp = self.client.get(self.url)
self.assertIn(
'id="DELETE_0"', resp.rendered_content
'form-0-DELETE', resp.rendered_content
)
self.assertNotIn(
'id="DELETE_1"', resp.rendered_content
'form-1-DELETE', resp.rendered_content
)
self.assertIn('cancel_button', resp.rendered_content)

Expand Down
37 changes: 27 additions & 10 deletions studioadmin/views/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,41 @@ def event_admin_list(request, ev_type):
)
)
else:
event = form.save()
changed_fields = []
unchanged_fields = []
for field in form.changed_data:
if getattr(event, field) == form.cleaned_data[field]:
changed_fields.append(field)
else:
unchanged_fields.append(field)

if changed_fields:
fields_str = ", ".join(
[field.title().replace("_", " ") for field in changed_fields]
)
messages.success(
request, mark_safe(
"<strong>{}</strong> updated for "
"<strong>{}</strong>".format(
field.title().replace("_", " "),
form.instance))
f"<strong>{fields_str}</strong> updated for "
f"<strong>{event}</strong>"
)
)

ActivityLog.objects.create(
log='{} {} (id {}) updated by admin user {}: field_changed: {}'.format(
ev_type_text.title(),
form.instance, form.instance.id,
request.user.username, field.title().replace("_", " ")
log=f"{ev_type_text.title()} {event} (id {event.id}) updated by "
f"admin user {request.user.username}: fields changed: {fields_str}"
)

if unchanged_fields:
fields_str = ", ".join(
[field.title().replace("_", " ") for field in unchanged_fields]
)
messages.error(
request, mark_safe(
f"<strong>{fields_str}</strong> could not be updated for "
f"<strong>{event}</strong>"
)
)

form.save()
eventformset.save()
return HttpResponseRedirect(
reverse('studioadmin:{}'.format(ev_type))
Expand Down
39 changes: 27 additions & 12 deletions studioadmin/views/ticketed_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,40 @@ def post(self, request, *args, **kwargs):
)
)
else:
ticketed_event = form.save()
changed_fields = []
unchanged_fields = []
for field in form.changed_data:
if getattr(ticketed_event, field) == form.cleaned_data[field]:
changed_fields.append(field)
else:
unchanged_fields.append(field)

if changed_fields:
fields_str = ", ".join(
[field.title().replace("_", " ") for field in changed_fields]
)
messages.success(
request, mark_safe(
"<strong>{}</strong> updated for "
"<strong>{}</strong>".format(
field.title().replace("_", " "),
form.instance))
f"<strong>{fields_str}</strong> updated for "
f"<strong>{ticketed_event}</strong>"
)
)

ActivityLog.objects.create(
log='Ticketed Event {} (id {}) updated '
'by admin user {}: field_'
'changed: {}'.format(
form.instance, form.instance.id,
request.user.username,
field.title().replace("_", " ")
log=f"Ticketed Event {ticketed_event} (id {ticketed_event.id}) updated by "
f"admin user {request.user.username}: fields changed: {fields_str}"
)

if unchanged_fields:
fields_str = ", ".join(
[field.title().replace("_", " ") for field in unchanged_fields]
)
messages.error(
request, mark_safe(
f"<strong>{fields_str}</strong> could not be updated for "
f"<strong>{ticketed_event}</strong>"
)
)
form.save()

ticketed_event_formset.save()
return HttpResponseRedirect(
Expand Down
37 changes: 26 additions & 11 deletions studioadmin/views/timetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,40 @@ def timetable_admin_list(request):
)
)
else:
session = form.save()
changed_fields = []
unchanged_fields = []
for field in form.changed_data:
if getattr(session, field) == form.cleaned_data[field]:
changed_fields.append(field)
else:
unchanged_fields.append(field)

if changed_fields:
fields_str = ", ".join(
[field.title().replace("_", " ") for field in changed_fields]
)
messages.success(
request, mark_safe(
"<strong>{}</strong> updated for "
"<strong>{}</strong>".format(
field.title().replace("_", " "),
form.instance
)
f"<strong>{fields_str}</strong> updated for "
f"<strong>{session}</strong>"
)
)
ActivityLog.objects.create(
log='Session {} (id {}) updated by admin '
'user {}'.format(
form.instance, form.instance.id,
request.user.username
log=f'Session {session} (id {session.id}) updated by admin '
f'user {request.user.username}: fields changed: {fields_str}'
)
if unchanged_fields:
fields_str = ", ".join(
[field.title().replace("_", " ") for field in unchanged_fields]
)
messages.error(
request, mark_safe(
f"<strong>{fields_str}</strong> could not be updated for "
f"<strong>{session}</strong>"
)
)
form.save()


sessionformset.save()
return HttpResponseRedirect(
reverse('studioadmin:timetable')
Expand Down
Loading

0 comments on commit 2a45783

Please sign in to comment.