예제 #1
0
파일: views.py 프로젝트: drewman/pybbm
    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_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))
예제 #3
0
 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
예제 #4
0
파일: views.py 프로젝트: brycenesbitt/pybbm
    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))
예제 #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