Пример #1
0
    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))
Пример #2
0
    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))
Пример #3
0
    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))
Пример #4
0
    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))
Пример #5
0
    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