def test_event_formset_delete_with_bookings(self): """ Test delete widget is formatted if bookings made against event (will be hidden in template and Cancel button will be displayed instead) """ extra_data = { 'form-TOTAL_FORMS': 2, 'form-INITIAL_FORMS': 2, 'form-0-DELETE': 'on', 'form-1-id': str(self.event1.id), 'form-1-cost': '7', 'form-1-max-participants': '10', 'form-1-booking_open': 'on', 'form-1-payment_open': 'on', 'form-1-DELETE': 'on', } formset = EventFormSet(data=self.formset_data(extra_data)) deleted_form_no_bookings = formset.deleted_forms[0] deleted_form_with_bookings = formset.deleted_forms[1] self.assertEqual(deleted_form_no_bookings.cleaned_data['id'], self.event) self.assertEqual(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'], 'delete-checkbox studioadmin-list') self.assertEqual(delete_with_bookings_widget.attrs['class'], 'delete-checkbox studioadmin-list')
def test_event_formset_delete(self): extra_data = { 'form-TOTAL_FORMS': 2, 'form-INITIAL_FORMS': 2, 'form-0-DELETE': 'on', 'form-1-id': self.event1.id, 'form-1-cost': '7', 'form-1-max-participants': '10', 'form-1-booking_open': 'on', 'form-1-payment_open': 'on', } formset = EventFormSet(data=self.formset_data(extra_data)) self.assertEqual(len(formset.deleted_forms), 1) deleted_form = formset.deleted_forms[0] self.assertEqual(deleted_form.cleaned_data['id'], self.event)
def _get_past_events(ev_type, request): if ev_type == 'events': nonpag_events = Event.objects.filter( event_type__event_type='EV', date__lte=timezone.now() ).order_by('-date') else: nonpag_events = Event.objects.filter( date__lte=timezone.now() ).exclude(event_type__event_type='EV').order_by('-date') paginator = Paginator(nonpag_events, 20) page = request.GET.get('page') try: events = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. events = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. events = paginator.page(paginator.num_pages) page_query = nonpag_events.filter(id__in=[obj.id for obj in events]) eventformset = EventFormSet(queryset=page_query) return events, eventformset
def test_event_formset_valid(self): formset = EventFormSet(data=self.formset_data()) self.assertTrue(formset.is_valid())
def event_admin_list(request, ev_type): if ev_type == 'events': ev_type_text = 'event' events = Event.objects.select_related('event_type').filter( event_type__event_type='EV', date__gte=timezone.now() - timedelta(hours=1) ).order_by('date') else: ev_type_text = 'class' events = Event.objects.select_related('event_type').filter( date__gte=timezone.now() - timedelta(hours=1) ).exclude(event_type__event_type='EV').order_by('date') show_past = False if request.method == 'POST': if "past" in request.POST: show_past = True events, eventformset = _get_past_events(ev_type, request) elif "upcoming" in request.POST: show_past = False eventformset = EventFormSet(queryset=events) else: eventformset = EventFormSet(request.POST) if eventformset.is_valid(): if not eventformset.has_changed(): messages.info(request, "No changes were made") else: for form in eventformset: if form.has_changed(): if 'DELETE' in form.changed_data: messages.success( request, mark_safe( '{} <strong>{}</strong> has been deleted!'.format( ev_type_text.title(), form.instance, ) ) ) ActivityLog.objects.create( log='{} {} (id {}) deleted by admin user {}'.format( ev_type_text.title(), 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='{} {} (id {}) updated by admin user {}: field_changed: {}'.format( ev_type_text.title(), form.instance, form.instance.id, request.user.username, field.title().replace("_", " ") ) ) form.save() eventformset.save() return HttpResponseRedirect( reverse('studioadmin:{}'.format(ev_type),) ) else: # pragma: no cover # currently only boolean fields, this is left here in case of # future additional fields messages.error( request, mark_safe( "There were errors in the following fields:\n{}".format( '\n'.join( [ "{}".format(error) for error in eventformset.errors ] ) ) ) ) else: page = request.GET.get('page') # only past is paginated if page: show_past = True events, eventformset = _get_past_events(ev_type, request) else: eventformset = EventFormSet(queryset=events) non_deletable_events = Booking.objects.select_related('event').filter(event__in=events).distinct().values_list('event__id', flat=True) return TemplateResponse( request, 'studioadmin/admin_events.html', { 'eventformset': eventformset, 'type': ev_type, 'events': events, 'sidenav_selection': ev_type, 'show_past': show_past, 'non_deletable_events': non_deletable_events } )
def event_admin_list(request, ev_type): ev_type_abbreviation = 'EV' if ev_type == 'events' else 'CL' ev_type_text = 'workshop' if ev_type == 'events' else 'class' queryset = Event.objects.filter( event_type__event_type=ev_type_abbreviation, date__gte=timezone.now() ).order_by('date') events = True if queryset.count() > 0 else False show_past = False if request.method == 'POST': if "past" in request.POST: queryset = Event.objects.filter( event_type__event_type=ev_type_abbreviation, date__lte=timezone.now() ).order_by('date') events = True if queryset.count() > 0 else False show_past = True eventformset = EventFormSet(queryset=queryset) elif "upcoming" in request.POST: queryset = queryset show_past = False eventformset = EventFormSet(queryset=queryset) else: eventformset = EventFormSet(request.POST) if eventformset.is_valid(): if not eventformset.has_changed(): messages.info(request, "No changes were made") else: for form in eventformset: if form.has_changed(): if 'DELETE' in form.changed_data: messages.success( request, mark_safe( '{} <strong>{}</strong> has been deleted!'.format( ev_type_text.title(), form.instance, ) ) ) ActivityLog.objects.create( log='{} {} (id {}) deleted by admin user {}'.format( ev_type_text.title(), 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='{} {} (id {}) updated by admin ' 'user {}: field_changed: ' '{}'.format( ev_type_text.title(), form.instance, form.instance.id, request.user.username, field.title().replace("_", " ") ) ) form.save() for error in form.errors: messages.error(request, mark_safe("{}".format(error))) eventformset.save() return HttpResponseRedirect( reverse('studioadmin:{}'.format(ev_type),) ) else: messages.error( request, mark_safe( "There were errors in the following fields:\n{}".format( '\n'.join( ["{}".format(error) for error in eventformset.errors] ) ) ) ) else: eventformset = EventFormSet(queryset=queryset) return render( request, 'studioadmin/admin_events.html', { 'eventformset': eventformset, 'type': ev_type, 'events': events, 'sidenav_selection': ev_type, 'show_past': show_past, } )