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_attachments = False save_poll_answers = False self.object = form.save(commit=False) if perms.may_attach_files(self.request.user): aformset = AttachmentFormSet(self.request.POST, self.request.FILES, instance=self.object) if aformset.is_valid(): save_attachments = True else: success = False else: aformset = None if perms.may_create_poll(self.request.user): pollformset = self.get_poll_answer_formset_class()() if getattr(self, 'forum', None) or self.object.topic.head == self.object: if self.object.topic.poll_type != Topic.POLL_TYPE_NONE: pollformset = self.get_poll_answer_formset_class()( 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() else: pollformset = None 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 get_context_data(self, **kwargs): ctx = super(TopicView, self).get_context_data(**kwargs) if self.request.user.is_authenticated(): self.request.user.is_moderator = perms.may_moderate_topic( self.request.user, self.topic) self.request.user.is_subscribed = self.request.user in self.topic.subscribers.all( ) if perms.may_post_as_admin(self.request.user): ctx['form'] = AdminPostForm(initial={ 'login': getattr(self.request.user, username_field) }, topic=self.topic) else: ctx['form'] = PostForm(topic=self.topic) self.mark_read(self.request, self.topic) elif defaults.PYBB_ENABLE_ANONYMOUS_POST: ctx['form'] = PostForm(topic=self.topic) else: ctx['form'] = None if defaults.PYBB_ATTACHMENT_ENABLE: aformset = AttachmentFormSet() ctx['aformset'] = aformset if defaults.PYBB_FREEZE_FIRST_POST: ctx['first_post'] = self.topic.head else: ctx['first_post'] = None ctx['topic'] = self.topic if self.request.user.is_authenticated() and self.topic.poll_type != Topic.POLL_TYPE_NONE and \ pybb_topic_poll_not_voted(self.topic, self.request.user): ctx['poll_form'] = PollForm(self.topic) return ctx
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 perms.may_attach_files(self.request.user): aformset = AttachmentFormSet( self.request.POST, self.request.FILES, instance=self.object) if aformset.is_valid(): save_attachments = True else: success = False else: aformset = None if perms.may_create_poll(self.request.user): 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() else: pollformset = None 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 get_context_data(self, **kwargs): ctx = super(PostEditMixin, self).get_context_data(**kwargs) if perms.may_attach_files(self.request.user) and (not 'aformset' in kwargs): ctx['aformset'] = AttachmentFormSet(instance=self.object if getattr(self, 'object') else None) if perms.may_create_poll(self.request.user) and ('pollformset' not in kwargs): ctx['pollformset'] = self.get_poll_answer_formset_class()( 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