Exemplo n.º 1
0
    def add_error(self, field, error):
        '''
        Override add_error method in Form,
        so that form doesn't need to be validated to add error.
        '''

        if not isinstance(error, ValidationError):
            error = ValidationError(error)

        if hasattr(error, 'error_dict'):
            if field is not None:
                raise TypeError(
                    "The argument `field` must be `None` when the `error` "
                    "argument contains errors for multiple fields.")
            else:
                error = error.error_dict
        else:
            error = {field or NON_FIELD_ERRORS: error.error_list}

        for field, error_list in error.items():
            if field not in self.errors:
                if field != NON_FIELD_ERRORS and field not in self.fields:
                    raise ValueError("'%s' has no field named '%s'." %
                                     (self.__class__.__name__, field))

                if field == NON_FIELD_ERRORS:
                    self._errors[field] = self.error_class(
                        error_class='nonfield')
                else:
                    self._errors[field] = self.error_class()

            self._errors[field].extend(error_list)
            if hasattr(self, 'cleaned_data') and field in self.cleaned_data:
                del self.cleaned_data[field]
Exemplo n.º 2
0
    def add_error(self, field, error):
        """
        Update the content of `self._errors`.

        The `field` argument is the name of the field to which the errors
        should be added. If it's None, treat the errors as NON_FIELD_ERRORS.

        The `error` argument can be a single error, a list of errors, or a
        dictionary that maps field names to lists of errors. An "error" can be
        either a simple string or an instance of ValidationError with its
        message attribute set and a "list or dictionary" can be an actual
        `list` or `dict` or an instance of ValidationError with its
        `error_list` or `error_dict` attribute set.

        If `error` is a dictionary, the `field` argument *must* be None and
        errors will be added to the fields that correspond to the keys of the
        dictionary.
        """
        if not isinstance(error, ValidationError):
            # Normalize to ValidationError and let its constructor
            # do the hard work of making sense of the input.
            error = ValidationError(error)

        if hasattr(error, "error_dict"):
            if field is not None:
                raise TypeError(
                    "The argument `field` must be `None` when the `error` "
                    "argument contains errors for multiple fields."
                )
            else:
                error = error.error_dict
        else:
            error = {field or NON_FIELD_ERRORS: error.error_list}

        for field, error_list in error.items():
            if field not in self.errors:
                if field != NON_FIELD_ERRORS and field not in self.fields:
                    raise ValueError(
                        "'%s' has no field named '%s'."
                        % (self.__class__.__name__, field)
                    )
                if field == NON_FIELD_ERRORS:
                    self._errors[field] = self.error_class(
                        error_class="nonfield", renderer=self.renderer
                    )
                else:
                    self._errors[field] = self.error_class(renderer=self.renderer)
            self._errors[field].extend(error_list)
            if field in self.cleaned_data:
                del self.cleaned_data[field]
Exemplo n.º 3
0
    def add_error(self, field, error):
        """
        Update the content of `self._errors`.

        The `field` argument is the name of the field to which the errors
        should be added. If its value is None the errors will be treated as
        NON_FIELD_ERRORS.

        The `error` argument can be a single error, a list of errors, or a
        dictionary that maps field names to lists of errors. What we define as
        an "error" can be either a simple string or an instance of
        ValidationError with its message attribute set and what we define as
        list or dictionary can be an actual `list` or `dict` or an instance
        of ValidationError with its `error_list` or `error_dict` attribute set.

        If `error` is a dictionary, the `field` argument *must* be None and
        errors will be added to the fields that correspond to the keys of the
        dictionary.
        """
        if not isinstance(error, ValidationError):
            # Normalize to ValidationError and let its constructor
            # do the hard work of making sense of the input.
            error = ValidationError(error)

        if hasattr(error, 'error_dict'):
            if field is not None:
                raise TypeError(
                    "The argument `field` must be `None` when the `error` "
                    "argument contains errors for multiple fields."
                )
            else:
                error = error.error_dict
        else:
            error = {field or NON_FIELD_ERRORS: error.error_list}

        for field, error_list in error.items():
            if field not in self.errors:
                if field != NON_FIELD_ERRORS and field not in self.fields:
                    raise ValueError(
                        "'%s' has no field named '%s'." % (self.__class__.__name__, field))
                if field == NON_FIELD_ERRORS:
                    self._errors[field] = self.error_class(error_class='nonfield')
                else:
                    self._errors[field] = self.error_class()
            self._errors[field].extend(error_list)
            if field in self.cleaned_data:
                del self.cleaned_data[field]
Exemplo n.º 4
0
    def validate_manifest(self, manifest_str):
        try:
            manifest_data = json.loads(manifest_str)
        except json.decoder.JSONDecodeError as exc:
            raise ValidationError(f"Unable to parse manifest.json: {exc}")

        serializer = ManifestV1Serializer(
            user=self.user,
            uploader=self.identity,
            data=manifest_data,
        )
        if serializer.is_valid():
            self.manifest = serializer.validated_data
        else:
            errors = unpack_serializer_errors("manifest.json",
                                              serializer.errors)
            errors = ValidationError(
                [f"{key}: {value}" for key, value in errors.items()])
            self.add_error(None, errors)
Exemplo n.º 5
0
    def add_error(self, field, error):
        """
        Standard django form does not support more structured errors
        (for example error dict that contains another error dict).
        For this purpose pyston creates RESTError class and we must rewrite this method to allow these
        complex error messages.
        """
        if isinstance(error, RESTError):
            if not field:
                raise ValueError('Field must be set for RESTError')
            self._errors[field] = error
        else:
            if not isinstance(error, ValidationError):
                # Normalize to ValidationError and let its constructor
                # do the hard work of making sense of the input.
                error = ValidationError(error)

            if hasattr(error, 'error_dict'):
                if field is not None:
                    raise TypeError(
                        "The argument `field` must be `None` when the `error` "
                        "argument contains errors for multiple fields.")
                else:
                    error = error.error_dict
            else:
                error = {field or NON_FIELD_ERRORS: error.error_list}

            for field, error_list in error.items():
                if field not in self.errors:
                    if field == NON_FIELD_ERRORS:
                        self._errors[field] = self.error_class(
                            error_class='nonfield')
                    else:
                        self._errors[field] = self.error_class()
                self._errors[field].extend(error_list)
                if field in self.cleaned_data:
                    del self.cleaned_data[field]
Exemplo n.º 6
0
    def add_error(self, field, error):
        """
        Standard django form does not support more structured errors
        (for example error dict that contains another error dict).
        For this purpose pyston creates RESTError class and we must rewrite this method to allow these
        complex error messages.
        """
        if isinstance(error, RESTError):
            if not field:
                raise ValueError('Field must be set for RESTError')
            self._errors[field] = error
        else:
            if not isinstance(error, ValidationError):
                # Normalize to ValidationError and let its constructor
                # do the hard work of making sense of the input.
                error = ValidationError(error)

            if hasattr(error, 'error_dict'):
                if field is not None:
                    raise TypeError(
                        "The argument `field` must be `None` when the `error` "
                        "argument contains errors for multiple fields."
                    )
                else:
                    error = error.error_dict
            else:
                error = {field or NON_FIELD_ERRORS: error.error_list}

            for field, error_list in error.items():
                if field not in self.errors:
                    if field == NON_FIELD_ERRORS:
                        self._errors[field] = self.error_class(error_class='nonfield')
                    else:
                        self._errors[field] = self.error_class()
                self._errors[field].extend(error_list)
                if field in self.cleaned_data:
                    del self.cleaned_data[field]
Exemplo n.º 7
0
    def validate_manifest(self, manifest_str):
        try:
            manifest_data = json.loads(manifest_str)
        except UnicodeDecodeError as exc:
            raise ValidationError([
                f"Unable to parse manifest.json: {exc}\n",
                "Make sure the manifest.json is UTF-8 compatible",
            ])
        except json.decoder.JSONDecodeError as exc:
            raise ValidationError(f"Unable to parse manifest.json: {exc}")

        serializer = ManifestV1Serializer(
            user=self.user,
            uploader=self.cleaned_data.get("team"),
            data=manifest_data,
        )
        if serializer.is_valid():
            self.manifest = serializer.validated_data
        else:
            errors = unpack_serializer_errors("manifest.json",
                                              serializer.errors)
            errors = ValidationError(
                [f"{key}: {value}" for key, value in errors.items()])
            self.add_error(None, errors)
Exemplo n.º 8
0
"""