Ejemplo n.º 1
0
    def _run_additional_validation(self, data):
        """
        Run additional data validation on API data and transition instances.

        Raise ValidationError (DRF) if any error occurs.
        """
        errors = _transition_data_validation(self.objects, self.transition,
                                             data)
        if errors:
            api_errors = {}
            for action_name, action_errors in errors.items():
                for field_name, field_errors in action_errors.items():
                    api_errors[field_name] = [
                        exc.message for exc in field_errors
                    ]
            raise DRFValidationError(api_errors)
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        not_valid = self._is_valid()
        if not_valid:
            return not_valid

        form = self.get_form()
        if form.is_valid():
            # additional data validation
            form_errors = _transition_data_validation(self.objects,
                                                      self.transition,
                                                      form.cleaned_data)
            if form_errors:
                for action, action_errors in form_errors.items():
                    for field_name, field_errors in action_errors.items():
                        form.add_error(action + '__' + field_name,
                                       field_errors)
            else:
                return self.form_valid(form)
        return self.form_invalid(form)