Exemplo n.º 1
0
 def condition_type(self, value):
     if value not in CONDITION_CHOICES:
         raise ValidationError(
             "condition_type attribute must be a value from a list: "
             "{list}".format(list=", ".join(CONDITION_CHOICES))
         )
     self._condition_type = value
Exemplo n.º 2
0
    def group_id(self, value):
        value = self._is_valid_int(value, "group_id", True)

        # Validate group id and raise an error if it's not valid
        if len(str(value)) > 9:
            raise ValidationError(
                "group_id must be an integer, maximum 9 characters.")

        self._group_id = str(value) if value else None
Exemplo n.º 3
0
    def rate(self, value):
        if value not in RATE_CHOICES:
            try:
                float(value)
            except (TypeError, ValueError):
                raise ValidationError(
                    ("The rate parameter can have the following values: "
                     "number (int or float), (rate_choices)".format(
                         rate_choices=', '.join(RATE_CHOICES))))

        self._rate = str(value)
Exemplo n.º 4
0
    def value(self, v):
        try:
            v = int(v)

            # Check valid choices for specified unit
            not_valid, choices = False, None
            if self.unit == "year":
                if v not in YEAR_CHOICES:
                    not_valid, choices = True, YEAR_CHOICES
            else:
                if v not in MONTH_CHOICES:
                    not_valid, choices = True, MONTH_CHOICES

            # Raise ValidationError if choice is not valid
            if not_valid:
                raise ValidationError(
                    "value for unit 'year' must be a valid choice: "
                    "{c}".format(c=", ".join(str(c) for c in choices)))

            self._value = str(v)
        except (TypeError, ValueError):
            raise ValidationError("value must be a valid int")
 def _is_valid_datetime(
     dt,
     dt_format,
     attr: str,
     allow_none: bool = False,
 ) -> Optional[Union[datetime, str]]:
     """
     A helper method for checking if a value is a valid datetime and
     returning a value if the check succeeds or raising an error.
     """
     if isinstance(dt, datetime):
         return dt.strftime(dt_format)
     elif isinstance(dt, str):
         try:
             datetime.strptime(dt, dt_format)
         except ValueError as e:
             raise ValidationError(e)
         return dt
     elif dt is None and allow_none:
         return None
     else:
         raise ValidationError(
             "{a} must be a valid datetime".format(a=attr)
         )
 def _is_valid_int(
     value,
     attr: str,
     allow_none: bool = False,
     convert_to_str: bool = True
 ) -> Optional[Union[int, str]]:
     """
     A helper method for checking if a value is a valid number and returning
     a value if the check succeeds or raising an error.
     """
     try:
         value = int(value)
         return str(value) if convert_to_str else value
     except (TypeError, ValueError):
         if value is None and allow_none:
             return None
         raise ValidationError("{a} must be a valid int".format(a=attr))
 def enable_auto_discounts(self, value):
     if value in ["yes", "true", "1", "no", "false", "0"]:
         self._enable_auto_discounts = value
     elif value is True:
         self._enable_auto_discounts = "true"
     elif value is False:
         self._enable_auto_discounts = "false"
     elif value is None:
         self._enable_auto_discounts = value
     else:
         raise ValidationError(
             (
                 "enable_auto_discounts should be True, False "
                 "or str from available values: {values}".format(
                     values=", ".join(ENABLE_AUTO_DISCOUNTS_CHOICES)
                 )
             )
         )
 def _is_valid_bool(
     value,
     attr: str,
     allow_none: bool = False
 ) -> Optional[str]:
     """
     A helper method for checking if a value is a valid bool and returning
     a value if the check succeeds or raising an error.
     """
     if value in ["true", "false"]:
         return value
     elif value is True:
         return "true"
     elif value is False:
         return "false"
     elif value is None and allow_none:
         return None
     else:
         raise ValidationError(
             "The {attr} parameter should be boolean. "
             "Got {t} instead.".format(attr=attr, t=type(value))
         )
Exemplo n.º 9
0
 def page_extent(self, value):
     value = self._is_valid_int(value, "page_extent", True, False)
     if value <= 0:
         raise ValidationError("page_extent must be positive int")
     self._page_extent = str(value)
Exemplo n.º 10
0
 def currency(self, value):
     if value not in CURRENCY_CHOICES:
         raise ValidationError(
             "Price data is accepted only in: (formatted_choices)".format(
                 formatted_choices=", ".join(CURRENCY_CHOICES)))
     self._currency = value
Exemplo n.º 11
0
 def unit(self, value):
     if value and value not in UNIT_CHOICES:
         raise ValidationError("unit must be a valid choice: {c}".format(
             c=", ".join(UNIT_CHOICES)))
     self._unit = value
Exemplo n.º 12
0
 def url(self, value: str):
     if len(value) > 512:
         raise ValidationError("The maximum url length is 512 characters.")
     self._url = value
Exemplo n.º 13
0
 def value(self, v):
     try:
         self._value = str(v)
     except (TypeError, ValueError):
         raise ValidationError("value must be a string")