Example #1
0
    def form_valid(self, form):
        # This implementation is an amalgamation of
        # * django.views.generic.edit.ModelFormMixin#form_valid
        # * django.views.generic.edit.FormMixin#form_valid

        # trigger signal for extra form validations
        try:
            view_form_valid.send(sender=type(self),
                                 view=self,
                                 form=form,
                                 request=self.request)
        except ValidationError:
            return self.form_invalid(form)

        is_new = (not self.object.pk)
        self.save_form(form)
        if is_new:
            object_created.send(sender=type(self.object),
                                object=self.object,
                                request=self.request)

        object_saved.send(sender=type(self.object),
                          object=self.object,
                          request=self.request)
        self._add_create_or_change_message(self.request,
                                           self.object,
                                           is_new=is_new)
        return HttpResponseRedirect(self.get_success_url())
Example #2
0
    def save_form_parts(self, form):
        # trigger signal for extra form validations
        try:
            view_form_valid.send(sender=type(self), view=self, form=form, request=self.request)
        except ValidationError:
            return self.form_invalid(form)

        is_new = (not self.object.pk)
        form_parts = self.get_form_parts(self.object)
        for form_part in form_parts:
            retval = form_part.form_valid(form)

            if retval is not None:  # Allow a form part to change the identity of the object
                self.object = retval
                for form_part in form_parts:
                    form_part.object = self.object
        if is_new:
            object_created.send(sender=type(self.object), object=self.object, request=self.request)

        object_saved.send(sender=type(self.object), object=self.object, request=self.request)
        self._add_create_or_change_message(self.request, self.object, is_new)

        if self.request.GET.get("redirect") and not self.request.POST.get("__next"):
            return HttpResponseRedirect(self.request.GET.get("redirect"))

        if hasattr(self, "get_success_url"):
            return HttpResponseRedirect(self.get_success_url())

        if is_new:
            return HttpResponseRedirect(get_model_url(self.object, shop=self.request.shop))
        else:
            return HttpResponseRedirect(self.request.path)