def create_event(request): event_form = EventForm(request.POST or None, prefix='event') ContactsFormSet = modelformset_factory(Contact, form=ContactForm, can_delete=True) contacts_formset = ContactsFormSet(request.POST or None, prefix='contacts-form', queryset=Contact.objects.none()) EventDateFormset = modelformset_factory(EventDate, form=EventDateForm, formset=EventDateModelFormset, can_delete=True) event_date_formset = EventDateFormset(request.POST or None, prefix='event-date-form', queryset=EventDate.objects.none()) if request.POST: if event_form.is_valid() and contacts_formset.is_valid() and event_date_formset.is_valid(): organizer = None event_user = None the_event = None contacts = None event_dates = None try: the_event = event_form.save() event_user = EventUser.objects.create(user=request.user, event=the_event) organizer = create_organizer(event_user) contacts = contacts_formset.save(commit=False) event_dates = event_date_formset.save(commit=False) for a_contact in contacts: a_contact.event = the_event a_contact.save() for event_date in event_dates: event_date.event = the_event event_date.save() return HttpResponseRedirect(reverse("index", args=(the_event.slug,))) except Exception as e: logger.error(e) try: if organizer is not None: Organizer.delete(organizer) if event_user is not None: EventUser.delete(event_user) if the_event is not None: Event.delete(the_event) if contacts is not None: for a_contact in contacts: Contact.objects.delete(a_contact) if event_dates is not None: for event_date in event_dates: EventDate.objects.delete(event_date) except Exception: pass messages.error(request, _("There is a problem with your event. Please check the form for errors.")) return render(request, 'event/create.html', {'form': event_form, 'domain': request.get_host(), 'protocol': request.scheme, 'contacts_formset': contacts_formset, 'event_date_formset': event_date_formset})
def create_event(request): event_form = EventForm(request.POST or None, prefix='event') ContactsFormSet = modelformset_factory(Contact, form=ContactForm, can_delete=True) contacts_formset = ContactsFormSet(request.POST or None, prefix='contacts-form', queryset=Contact.objects.none()) if request.POST: if event_form.is_valid() and contacts_formset.is_valid(): organizer = None eventUser = None the_event = None contacts = None try: the_event = event_form.save() eventUser = EventUser.objects.create(user=request.user, event=the_event) organizer = create_organizer(eventUser) contacts = contacts_formset.save(commit=False) for a_contact in contacts: a_contact.event = the_event a_contact.save() return HttpResponseRedirect('/event/' + the_event.slug) except Exception: if organizer is not None: Organizer.delete(organizer) if eventUser is not None: EventUser.delete(eventUser) if the_event is not None: Event.delete(the_event) if contacts is not None: for a_contact in contacts: Contact.objects.delete(a_contact) messages.error(request, _("There is a problem with your event. Please check the form for errors.")) return render(request, 'event/create.html', {'form': event_form, 'domain': request.get_host(), 'protocol': request.scheme, 'contacts_formset': contacts_formset}, context_instance=RequestContext(request))
def create_event(request): event_form = EventForm(request.POST or None, prefix='event') ContactsFormSet = modelformset_factory(Contact, form=ContactForm, can_delete=True) contacts_formset = ContactsFormSet(request.POST or None, prefix='contacts-form', queryset=Contact.objects.none()) if request.POST: if event_form.is_valid() and contacts_formset.is_valid(): organizer = None eventUser = None the_event = None contacts = None try: the_event = event_form.save() eventUser = EventUser.objects.create(user=request.user, event=the_event) organizer = create_organizer(eventUser) contacts = contacts_formset.save(commit=False) for a_contact in contacts: a_contact.event = the_event a_contact.save() return HttpResponseRedirect('/event/' + the_event.slug) except Exception: if organizer is not None: Organizer.delete(organizer) if eventUser is not None: EventUser.delete(eventUser) if the_event is not None: Event.delete(the_event) if contacts is not None: for a_contact in contacts: Contact.objects.delete(a_contact) messages.error(request, _("There is a problem with your event. Please check the form for errors.")) return render(request, 'event/create.html', {'form': event_form, 'domain': request.get_host(), 'protocol': request.scheme, 'contacts_formset': contacts_formset}, context_instance=RequestContext(request))