Пример #1
0
 def get_context_data(self, **kwargs):
     context = super(RuleUpdate, self).get_context_data(**kwargs)
     context.setdefault("formset_labels", forms.LabelFormset(instance=self.object))
     context.setdefault("formset_annotations", forms.AnnotationFormset(instance=self.object))
     context["macro"] = macro.EXCLUSION_MACRO
     context["rules"] = [self.object.parent] if self.object.parent else [self.object]
     return context
Пример #2
0
    def post(self, request, *args, **kwargs):
        self.object = self.get_object()

        # Save a copy of our forms into a context var that we can use
        # to re-render our form properly in case of errors
        context = {}
        context["form"] = form = self.get_form()
        context["formset_labels"] = form_labels = forms.LabelFormset(
            request.POST, request.FILES, instance=self.object)
        context[
            "formset_annotations"] = form_annotations = forms.AnnotationFormset(
                request.POST, request.FILES, instance=self.object)

        # Check validity of our labels and annotations in Django before we try to render
        if not all([form_labels.is_valid(), form_annotations.is_valid()]):
            return self.form_invalid(**context)

        # Populate our cached_properties so we can render a test
        # populate only rows with a 'value' so that we skip fields we're deleting
        # see Django docs on cached_property and promgen.forms.RuleForm.clean()
        form.instance.labels = {
            l["name"]: l["value"]
            for l in form_labels.cleaned_data if "value" in l
        }
        form.instance.annotations = {
            a["name"]: a["value"]
            for a in form_annotations.cleaned_data if "value" in a
        }

        # With our labels+annotations manually cached we can test
        if not form.is_valid():
            return self.form_invalid(**context)

        # Save our labels
        for instance in form_labels.save():
            messages.info(request,
                          "Added {} to {}".format(instance.name, self.object))

        # Save our annotations
        for instance in form_annotations.save():
            messages.info(request,
                          "Added {} to {}".format(instance.name, self.object))

        return self.form_valid(form)