def form_valid(self, form): success = True with transaction.commit_manually(): self.object = form.save() if defaults.PYBB_ATTACHMENT_ENABLE: aformset = AttachmentFormSet(self.request.POST, self.request.FILES, instance=self.object) if aformset.is_valid(): aformset.save() else: success = False else: aformset = AttachmentFormSet() if self.object.topic.poll_type != Topic.POLL_TYPE_NONE and self.object.topic.head == self.object: pollformset = PollAnswerFormSet(self.request.POST, instance=self.object.topic) if pollformset.is_valid(): pollformset.save() else: success = False else: self.object.topic.poll_question = None self.object.topic.save() self.object.topic.poll_answers.all().delete() pollformset = PollAnswerFormSet() if success: transaction.commit() return super(ModelFormMixin, self).form_valid(form) else: transaction.rollback() return self.render_to_response(self.get_context_data(form=form, aformset=aformset, pollformset=pollformset))
def form_valid(self, form): success = True save_poll_answers = False self.object = form.save(commit=False) pollformset = PollAnswerFormSet() if getattr(self, 'forum', None) or self.object.topic.head == self.object: if self.object.topic.poll_type != Topic.POLL_TYPE_NONE: pollformset = PollAnswerFormSet(self.request.POST, instance=self.object.topic) if pollformset.is_valid(): save_poll_answers = True else: success = False else: self.object.topic.poll_question = None self.object.topic.poll_answers.all().delete() if success: self.object.topic.save() self.object.topic = self.object.topic self.object.save() if save_poll_answers: pollformset.save() return super(ModelFormMixin, self).form_valid(form) else: return self.render_to_response(self.get_context_data(form=form, aformset=aformset, pollformset=pollformset))
def get_context_data(self, **kwargs): ctx = super(PostEditMixin, self).get_context_data(**kwargs) if defaults.PYBB_ATTACHMENT_ENABLE and (not 'aformset' in kwargs): ctx['aformset'] = AttachmentFormSet(instance=self.object if getattr(self, 'object') else None) if 'pollformset' not in kwargs: ctx['pollformset'] = PollAnswerFormSet(instance=self.object.topic if getattr(self, 'object') else None) return ctx
def form_valid(self, form): success = True save_attachments = False save_poll_answers = False self.object = form.save(commit=False) if defaults.PYBB_ATTACHMENT_ENABLE: aformset = AttachmentFormSet(self.request.POST, self.request.FILES, instance=self.object) if aformset.is_valid(): save_attachments = True else: success = False else: aformset = AttachmentFormSet() pollformset = PollAnswerFormSet() if getattr(self, 'forum', None) or self.object.topic.head == self.object: if self.object.topic.poll_type != Topic.POLL_TYPE_NONE: pollformset = PollAnswerFormSet(self.request.POST, instance=self.object.topic) if pollformset.is_valid(): save_poll_answers = True else: success = False else: self.object.topic.poll_question = None self.object.topic.poll_answers.all().delete() if success: self.object.topic.save() self.object.topic = self.object.topic self.object.save() if save_attachments: aformset.save() if save_poll_answers: pollformset.save() return super(ModelFormMixin, self).form_valid(form) else: return self.render_to_response( self.get_context_data(form=form, aformset=aformset, pollformset=pollformset))
def form_valid(self, form): success = True with transaction.commit_manually(): try: self.object = form.save() if defaults.PYBB_ATTACHMENT_ENABLE: aformset = AttachmentFormSet(self.request.POST, self.request.FILES, instance=self.object) if aformset.is_valid(): aformset.save() else: success = False else: aformset = AttachmentFormSet() if self.object.topic.poll_type != Topic.POLL_TYPE_NONE and self.object.topic.head == self.object: pollformset = PollAnswerFormSet(self.request.POST, instance=self.object.topic) if pollformset.is_valid(): pollformset.save() else: success = False else: self.object.topic.poll_question = None self.object.topic.save() self.object.topic.poll_answers.all().delete() pollformset = PollAnswerFormSet() if success: transaction.commit() return super(ModelFormMixin, self).form_valid(form) else: transaction.rollback() return self.render_to_response( self.get_context_data(form=form, aformset=aformset, pollformset=pollformset)) except Exception: transaction.rollback() raise