Ejemplo n.º 1
0
    def validate_input(self, value):
        """
        Validates if the given value is a valid input for the NumerField.
        Checks if a value of type number has been provided or if a value of
        type String has been provided that can be successfully converted to a
        Float value. Then checks if the value is between bounds of minval and
        maxval.

        Parameters
        ----------
        value : str
            The value to be validated

        Notes
        -----
        Raises InputError if an invalid value is provided
        """
        if isinstance(value, (str, unicode)) and len(value) == 0:
            value = None

        self.validate_required(value)

        if value is not None:
            if isinstance(value, (str, unicode)):
                try:
                    value = float(value) if '.' in value else int(value)
                except ValueError:
                    raise InputError(
                        'The value provided for field %s is not a number.' %
                        self.name)

            if isinstance(value, (int, long, float, complex)):
                if self.minval and self.maxval and (not (value >= self.minval)
                                                    and
                                                    (value <= self.maxval)):
                    raise InputError('The value provided for field %s must be '
                                     ' greater than %s and lower than %s.' %
                                     (self.name, self.minval, self.maxval))

                else:
                    if self.minval and (not (value >= self.minval)):
                        raise InputError('The value provided for field %s must'
                                         ' be greater than %s.' %
                                         (self.name, self.minval))

                    if self.maxval and (not (value <= self.maxval)):
                        raise InputError('The value provided for field %s must'
                                         ' be lower than %s.' %
                                         (self.name, self.maxval))
Ejemplo n.º 2
0
    def get_by_bbox(self, bbox):
        """
        Returns a subset of the queryset containing observations where the
        geometry of the location is inside the the passed bbox.

        Parameters
        ----------
        bbox : str
            Str that provides the xmin,ymin,xmax,ymax

        Return
        ------
        django.db.models.Queryset
            List of search results matching the query
        """

        if bbox:
            try:
                # created bbox to Polygon
                from django.contrib.gis.geos import Polygon
                bbox = bbox.split(',')  # Split by ','
                geom_bbox = Polygon.from_bbox(bbox)
                # Filtering observations where
                return self.filter(location__geometry__bboverlaps=geom_bbox)
            except Exception as e:
                raise InputError(
                    str(e) + '. Please, check the coordinates'
                    ' you attached to bbox parameters, they should follow'
                    'the OSGeo standards (e.g:bbox=xmin,ymin,xmax,ymax).')
Ejemplo n.º 3
0
    def validate_input(self, provided_vals):
        """
        Checks if the provided value matches the ID of one of the field's
        lookupvalues.

        Parameters
        ----------
        value : int
            The value to be validated

        Notes
        -----
        Raises InputError if an invalid value is provided
        """
        self.validate_required(provided_vals)

        valid = True

        if provided_vals is not None:
            if isinstance(provided_vals, (str, unicode)):
                provided_vals = json.loads(provided_vals)

            accepted_values = [value.id for value in self.lookupvalues.all()]
            intersection = [
                val for val in provided_vals if val in accepted_values
            ]

            valid = len(intersection) == len(provided_vals)

        if not valid:
            raise InputError('One or more values for the multiple select '
                             'field %s is not an accepted value for the '
                             'field.' % self.name)
Ejemplo n.º 4
0
    def validate_input(self, value):
        """
        Checks if the provided value matches the ID of one of the field's
        lookupvalues.

        Parameters
        ----------
        value : int
            The value to be validated

        Notes
        -----
        Raises InputError if an invalid value is provided
        """
        self.validate_required(value)

        valid = False

        if value is not None:
            try:
                value = int(value)
                for lookupvalue in self.lookupvalues.all():
                    if lookupvalue.id == value:
                        valid = True
            except ValueError:
                pass
        else:
            valid = True

        if not valid:
            raise InputError('The value for lookup field %s is not an '
                             'accepted value for the field.' % self.name)
Ejemplo n.º 5
0
 def validate_required(self, value):
     """
     Validates input value against required status.
     Raises an `InputError` if no value has been provided.
     """
     if self.status == STATUS.active and self.required and (value is None):
         raise InputError('The field %s is required.' % self.name)
Ejemplo n.º 6
0
    def validate_required(self, value):
        """
        Validate the given value agaist required status. Checks if value is
        not None and has at least one character.

        Parameters
        ----------
        value : str or None
            The value to be validated

        Notes
        -----
        Raises InputError if no value is provided
        """
        if (self.status == STATUS.active and self.required
                and (value is None or len(value) == 0)):
            raise InputError('The field %s is required.' % self.name)
Ejemplo n.º 7
0
    def validate_input(self, value):
        """
        Checks if the provided value is a matches the patterns HH:mm

        Parameters
        ----------
        value : str
            The value to be validated

        Notes
        -----
        Raises InputError if an invalid value is provided
        """
        self.validate_required(value)
        if value is not None:
            try:
                time.strptime(value, '%H:%M')
            except ValueError:
                raise InputError('The value for TimeField %s is not a '
                                 'valid time.' % self.name)
Ejemplo n.º 8
0
    def validate_input(self, value):
        """
        Validates if the given value is a valid input for the TextField.
        Checks if the value is required and if maxlength constraint is met.

        Parameters
        ----------
        value : str
            The value to be validated

        Notes
        -----
        Raises InputError if an invalid value is provided
        """
        self.validate_required(value)

        if value is not None:
            if self.maxlength is not None and len(value) > self.maxlength:
                raise InputError('The input provided for text field %s '
                                 'contains too many characters.' % self.name)
Ejemplo n.º 9
0
    def validate_required(self, value):
        """
        Validate the given value agaist required status. Checks if value is
        not None and has at least one character.

        Parameters
        ----------
        value : str
            The value to be validated

        Notes
        -----
        Raises InputError if no value is provided
        """
        if isinstance(value, str) or isinstance(value, unicode):
            value = value.encode('utf-8')

        if self.status == STATUS.active and self.required and (
                value is None or len(str(value)) == 0):
            raise InputError('The field %s is required.' % self.name)
Ejemplo n.º 10
0
    def validate_input(self, value):
        """
        Checks if the provided value is a valid and ISO8601 compliant date
        string.

        Parameters
        ----------
        value : str
            The value to be validated

        Notes
        -----
        Raises InputError if an invalid value is provided
        """
        self.validate_required(value)
        if value is not None:
            try:
                parse_date(value)
            except ParseError:
                raise InputError('The value for DateField %s is not a '
                                 'valid date.' % self.name)