def post(self, request, *args, **kwargs): if not self.event.is_accepting_requests(): raise Http404(u'Страница не найдена') formset = self.ModelFormSet(request.POST, prefix=self.formset_prefix) if formset.is_valid(): if formset.total_form_count() <= len(formset.deleted_forms): self.object.delete() return HttpResponseRedirect( reverse('event-detail', args=(self.event.id,))) instances = formset.save(commit=False) self.object.is_accepted = False self.object.save() for instance in instances: # the name of fk attribute should be lower case of fk model setattr(instance, self.model_name, self.object) instance.is_accepted = False instance.save() if instance.nomination not in instance.nominations.all(): instance.nominations.clear() vn = VideoNomination( nomination=instance.nomination, participant_video=instance) vn.save() return HttpResponseRedirect(self.get_success_url()) return self.render_to_response(self.get_context_data(formset=formset))
def post(self, request, *args, **kwargs): if not self.event.is_accepting_requests(): raise Http404(u'Страница не найдена') self.object = None participant_form = ParticipantForm(request.POST, prefix='accept') formset = self.ModelFormSet(request.POST, prefix='participantvideo_set') if participant_form.is_valid(): if (formset.is_valid() and (formset.total_form_count() > len(formset.deleted_forms))): self.object = self.model(owner=request.user, event=self.event) try: self.object.save() except IntegrityError: try: # in case of already exist participant self.object = self.model.objects.get( event=self.event, owner=request.user) return HttpResponseRedirect(self.get_success_url()) except request.user.DoesNotExist: return self.render_to_response( self.get_context_data(formset=formset)) instances = formset.save(commit=False) for instance in instances: instance.participant = self.object instance.save() if instance.nomination not in instance.nominations.all(): instance.nominations.clear() vn = VideoNomination( nomination=instance.nomination, participant_video=instance) vn.save() return HttpResponseRedirect(self.get_success_url()) else: return self.render_to_response(self.get_context_data( formset=formset, participant_form=participant_form)) return self.render_to_response(self.get_context_data(formset=formset))