def test_additional_form_data(self): formset = TimetableSessionFormSet(data=self.formset_data(), queryset=Session.objects.all()) form = formset.forms[0] self.assertEquals(form.formatted_day, DAY_CHOICES[self.session.day]) self.assertEquals(form.booking_open_id, 'booking_open_0') self.assertEquals(form.payment_open_id, 'payment_open_0')
def test_can_delete(self): session_to_delete = mommy.make(Session) extra_data = { 'form-TOTAL_FORMS': 2, 'form-INITIAL_FORMS': 2, 'form-1-DELETE': 'on', 'form-1-id': session_to_delete.id, 'form-1-cost': '7', 'form-1-max-participants': '10', 'form-1-booking_open': 'on', 'form-1-payment_open': 'on', } formset = TimetableSessionFormSet(data=self.formset_data(extra_data), queryset=Session.objects.all()) self.assertEqual(len(formset.deleted_forms), 1) deleted_form = formset.deleted_forms[0] self.assertEqual(deleted_form.cleaned_data['id'], session_to_delete)
def test_event_formset_valid(self): formset = TimetableSessionFormSet(data=self.formset_data()) self.assertTrue(formset.is_valid())
def timetable_admin_list(request): if request.method == 'POST': sessionformset = TimetableSessionFormSet(request.POST) if sessionformset.is_valid(): if not sessionformset.has_changed(): messages.info(request, "No changes were made") else: for form in sessionformset: if form.has_changed(): if 'DELETE' in form.changed_data: messages.success( request, mark_safe( 'Session <strong>{} {} {}</strong> has been deleted!' .format( form.instance.name, DAY_CHOICES[form.instance.day], form.instance.time.strftime('%H:%M')))) ActivityLog.objects.create( log='Session {} (id {}) deleted by admin ' 'user {}'.format(form.instance, form.instance. id, request.user.username)) else: for field in form.changed_data: messages.success( request, mark_safe( "<strong>{}</strong> updated for " "<strong>{}</strong>".format( field.title().replace("_", " "), form.instance))) ActivityLog.objects.create( log='Session {} (id {}) updated by admin ' 'user {}'.format(form.instance, form.instance.id, request.user.username)) form.save() sessionformset.save() return HttpResponseRedirect(reverse('studioadmin:timetable')) else: # pragma: no cover # all fields are booleans; no errors will be thrown, but keep this # code in case we change the fields in future messages.error( request, mark_safe( "There were errors in the following fields:\n{}".format( '\n'.join([ "{}".format(error) for error in sessionformset.errors ])))) else: sessionformset = TimetableSessionFormSet( queryset=Session.objects.all().order_by('day', 'time')) return render(request, 'studioadmin/timetable_list.html', { 'sessionformset': sessionformset, 'sidenav_selection': 'timetable' })
def timetable_admin_list(request): if request.method == 'POST': sessionformset = TimetableSessionFormSet(request.POST) if sessionformset.is_valid(): if not sessionformset.has_changed(): messages.info(request, "No changes were made") else: for form in sessionformset: if form.has_changed(): if 'DELETE' in form.changed_data: messages.success( request, mark_safe( 'Session <strong>{} {} {}</strong> has been deleted!'.format( form.instance.name, DAY_CHOICES[form.instance.day], form.instance.time.strftime('%H:%M') )) ) ActivityLog.objects.create( log='Session {} (id {}) deleted by admin ' 'user {}'.format( form.instance, form.instance.id, request.user.username ) ) else: for field in form.changed_data: messages.success( request, mark_safe( "<strong>{}</strong> updated for " "<strong>{}</strong>".format( field.title().replace("_", " "), form.instance ) ) ) ActivityLog.objects.create( log='Session {} (id {}) updated by admin ' 'user {}'.format( form.instance, form.instance.id, request.user.username ) ) form.save() sessionformset.save() return HttpResponseRedirect( reverse('studioadmin:timetable') ) else: # pragma: no cover # all fields are booleans; no errors will be thrown, but keep this # code in case we change the fields in future messages.error( request, mark_safe( "There were errors in the following fields:\n{}".format( '\n'.join( ["{}".format(error) for error in sessionformset.errors] ) ) ) ) else: sessionformset = TimetableSessionFormSet( queryset=Session.objects.all().order_by('day', 'time') ) return render( request, 'studioadmin/timetable_list.html', { 'sessionformset': sessionformset, 'sidenav_selection': 'timetable' } )