Esempio n. 1
0
    def form_valid(self, form):
        """
        Runs after form validation. Creates the ``Target``, and creates any ``TargetName`` or ``TargetExtra`` objects,
        then runs the ``target_post_save`` hook and redirects to the success URL.

        :param form: Form data for target creation
        :type form: subclass of TargetCreateForm
        """
        super().form_valid(form)
        extra = TargetExtraFormset(self.request.POST)
        names = TargetNamesFormset(self.request.POST)
        if extra.is_valid() and names.is_valid():
            extra.instance = self.object
            extra.save()
            names.instance = self.object
            names.save()
        else:
            form.add_error(None, extra.errors)
            form.add_error(None, extra.non_form_errors())
            form.add_error(None, names.errors)
            form.add_error(None, names.non_form_errors())
            return super().form_invalid(form)
        logger.info('Target post save hook: %s created: %s', self.object, True)
        run_hook('target_post_save', target=self.object, created=True)
        return redirect(self.get_success_url())
Esempio n. 2
0
 def get_context_data(self, **kwargs):
     extra_field_names = [extra['name'] for extra in settings.EXTRA_FIELDS]
     context = super().get_context_data(**kwargs)
     context['names_form'] = TargetNamesFormset(instance=self.object)
     context['extra_form'] = TargetExtraFormset(
         instance=self.object,
         queryset=self.object.targetextra_set.exclude(
             key__in=extra_field_names))
     return context
Esempio n. 3
0
    def get_context_data(self, **kwargs):
        """
        Adds formset for ``TargetName`` and ``TargetExtra`` to the context.

        :returns: context object
        :rtype: dict
        """
        extra_field_names = [extra['name'] for extra in settings.EXTRA_FIELDS]
        context = super().get_context_data(**kwargs)
        context['names_form'] = TargetNamesFormset(instance=self.object)
        context['extra_form'] = TargetExtraFormset(
            instance=self.object,
            queryset=self.object.targetextra_set.exclude(key__in=extra_field_names)
        )
        return context
Esempio n. 4
0
    def get_context_data(self, **kwargs):
        """
        Inserts certain form data into the context dict.

        :returns: Dictionary with the following keys:

                  `type_choices`: ``tuple``: Tuple of 2-tuples of strings containing available target types in the TOM

                  `extra_form`: ``FormSet``: Django formset with fields for arbitrary key/value pairs
        """
        context = super(TargetCreateView, self).get_context_data(**kwargs)
        context['type_choices'] = Target.TARGET_TYPES
        context['names_form'] = TargetNamesFormset(initial=[{
            'name': new_name
        } for new_name in self.request.GET.get('names', '').split(',')])
        context['extra_form'] = TargetExtraFormset()
        return context
Esempio n. 5
0
 def form_valid(self, form):
     super().form_valid(form)
     extra = TargetExtraFormset(self.request.POST)
     names = TargetNamesFormset(self.request.POST)
     if extra.is_valid() and names.is_valid():
         extra.instance = self.object
         extra.save()
         names.instance = self.object
         names.save()
     else:
         form.add_error(None, extra.errors)
         form.add_error(None, extra.non_form_errors())
         form.add_error(None, names.errors)
         form.add_error(None, names.non_form_errors())
         return super().form_invalid(form)
     return redirect(self.get_success_url())
Esempio n. 6
0
    def form_valid(self, form):
        """
        Runs after form validation. Validates and saves the ``TargetExtra`` and ``TargetName`` formsets, then calls the
        superclass implementation of ``form_valid``, which saves the ``Target``. If any forms are invalid, rolls back
        the changes.

        Saving is done in this order to ensure that new names/extras are available in the ``target_post_save`` hook.

        :param form: Form data for target update
        :type form: subclass of TargetCreateForm
        """
        extra = TargetExtraFormset(self.request.POST, instance=self.object)
        names = TargetNamesFormset(self.request.POST, instance=self.object)
        if extra.is_valid() and names.is_valid():
            extra.save()
            names.save()
        else:
            form.add_error(None, extra.errors)
            form.add_error(None, extra.non_form_errors())
            form.add_error(None, names.errors)
            form.add_error(None, names.non_form_errors())
            return super().form_invalid(form)
        super().form_valid(form)
        return redirect(self.get_success_url())