Пример #1
0
    def validate(self, value):
        super(DecimalField, self).validate(value)
        if value in validators.EMPTY_VALUES:
            return
        # Check for NaN, Inf and -Inf values. We can't compare directly for NaN,
        # since it is never equal to itself. However, NaN is the only value that
        # isn't equal to itself, so we can use this to identify NaN
        if value != value or value == Decimal("Inf") or value == Decimal(
                "-Inf"):
            raise ValidationError(self.error_messages["invalid"])
        sign, digittuple, exponent = value.as_tuple()
        decimals = abs(exponent)
        # digittuple doesn't include any leading zeros.
        digits = len(digittuple)
        if decimals > digits:
            # We have leading zeros up to or past the decimal point.  Count
            # everything past the decimal point as a digit.  We do not count
            # 0 before the decimal point as a digit since that would mean
            # we would not allow max_digits = decimal_places.
            digits = decimals
        whole_digits = digits - decimals

        if self.max_digits is not None and digits > self.max_digits:
            raise ValidationError(self.error_messages["max_digits"] %
                                  self.max_digits)
        if self.decimal_places is not None and decimals > self.decimal_places:
            raise ValidationError(self.error_messages["max_decimal_places"] %
                                  self.decimal_places)
        if self.max_digits is not None and self.decimal_places is not None and whole_digits > (
                self.max_digits - self.decimal_places):
            raise ValidationError(self.error_messages["max_whole_digits"] %
                                  (self.max_digits - self.decimal_places))
        return value
Пример #2
0
    def validate_attributes_values(self, attrs, source):
        # values must be a dict
        data_values = attrs.get("attributes_values", None)
        if self.object:
            data_values = (data_values or self.object.attributes_values)

        if type(data_values) is not dict:
            raise ValidationError(
                _("Invalid content. It must be {\"key\": \"value\",...}"))

        # Values keys must be in the container object project
        data_container = attrs.get(self._container_field, None)
        if data_container:
            project_id = data_container.project_id
        elif self.object:
            project_id = getattr(self.object, self._container_field).project_id
        else:
            project_id = None

        values_ids = list(data_values.keys())
        qs = self._custom_attribute_model.objects.filter(project=project_id,
                                                         id__in=values_ids)
        if qs.count() != len(values_ids):
            raise ValidationError(_("It contains invalid custom fields."))

        return attrs
Пример #3
0
    def validate_before_userstory_id(self, attrs, source):
        before_userstory_id = attrs.get(source, None)
        after_userstory_id = attrs.get("after_userstory_id", None)

        if after_userstory_id and before_userstory_id:
            raise ValidationError(
                _("You can't use after and before at the same time."))
        elif before_userstory_id is not None:
            filters = {
                "project__id": attrs["project_id"],
                "status__id": attrs["status_id"],
                "id": attrs[source]
            }
            swimlane_id = attrs.get("swimlane_id", None)
            if swimlane_id:
                filters["swimlane__id"] = swimlane_id
            else:
                filters["swimlane__isnull"] = True

            if not UserStory.objects.filter(**filters).exists():
                raise ValidationError(
                    _("Invalid user story id to move before. The user story must belong "
                      "to the same project, status and swimlane."))

        return attrs
Пример #4
0
    def from_native(self, value):
        # Convert URL -> model instance pk
        # TODO: Use values_list
        queryset = self.queryset
        if queryset is None:
            raise Exception(
                "Writable related fields must include a `queryset` argument")

        try:
            http_prefix = value.startswith(("http:", "https:"))
        except AttributeError:
            msg = self.error_messages["incorrect_type"]
            raise ValidationError(msg % type(value).__name__)

        if http_prefix:
            # If needed convert absolute URLs to relative path
            value = urlparse.urlparse(value).path
            prefix = get_script_prefix()
            if value.startswith(prefix):
                value = "/" + value[len(prefix):]

        try:
            match = resolve(value)
        except Exception:
            raise ValidationError(self.error_messages["no_match"])

        if match.view_name != self.view_name:
            raise ValidationError(self.error_messages["incorrect_match"])

        try:
            return self.get_object(queryset, match.view_name, match.args,
                                   match.kwargs)
        except (ObjectDoesNotExist, TypeError, ValueError):
            raise ValidationError(self.error_messages["does_not_exist"])
Пример #5
0
        def _validate_tag_field(value):
            # Valid field:
            #    - ["tag1", "tag2", "tag3"...]
            #    - ["tag1", ["tag2", None], ["tag3", "#ccc"], [tag4, #cccccc]...]
            for tag in value:
                if isinstance(tag, str):
                    continue

                if isinstance(tag, (list, tuple)) and len(tag) == 2:
                    name = tag[0]
                    color = tag[1]

                    if isinstance(name, str):
                        if color is None or color == "":
                            continue

                        if isinstance(color, str) and re.match(
                                '^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$', color):
                            continue

                        raise ValidationError(
                            _("Invalid tag '{value}'. The color is not a "
                              "valid HEX color or null.").format(value=tag))

                raise ValidationError(
                    _("Invalid tag '{value}'. it must be the name or a pair "
                      "'[\"name\", \"hex color/\" | null]'.").format(
                          value=tag))
Пример #6
0
    def validate_full_name(self, attrs, source):
        value = attrs[source]
        if value != bleach.clean(value):
            raise ValidationError(_("Invalid full name"))

        if re.search(r"http[s]?:", value):
            raise ValidationError(_("Invalid full name"))

        return attrs
Пример #7
0
    def validate(self, attrs):
        if "ref" in attrs:
            if "epic" in attrs:
                raise ValidationError("'epic' param is incompatible with 'ref' in the same request")
            if "us" in attrs:
                raise ValidationError("'us' param is incompatible with 'ref' in the same request")
            if "task" in attrs:
                raise ValidationError("'task' param is incompatible with 'ref' in the same request")
            if "issue" in attrs:
                raise ValidationError("'issue' param is incompatible with 'ref' in the same request")

        return attrs
Пример #8
0
    def from_native(self, data):
        if self.queryset is None:
            raise Exception("Writable related fields must include a `queryset` argument")

        try:
            return self.queryset.get(**{self.slug_field: data})
        except ObjectDoesNotExist:
            raise ValidationError(self.error_messages["does_not_exist"] %
                                  (self.slug_field, smart_text(data)))
        except (TypeError, ValueError):
            msg = self.error_messages["invalid"]
            raise ValidationError(msg)
Пример #9
0
    def from_native(self, data):
        if self.queryset is None:
            raise Exception("Writable related fields must include a `queryset` argument")

        try:
            return self.queryset.get(pk=data)
        except ObjectDoesNotExist:
            msg = self.error_messages["does_not_exist"] % smart_text(data)
            raise ValidationError(msg)
        except (TypeError, ValueError):
            received = type(data).__name__
            msg = self.error_messages["incorrect_type"] % received
            raise ValidationError(msg)
Пример #10
0
    def validate_is_admin(self, attrs, source):
        project = attrs.get("project", None if self.object is None else self.object.project)
        if project is None:
            return attrs

        if (self.object and self.object.user):
            if self.object.user.id == project.owner_id and not attrs[source]:
                raise ValidationError(_("The project owner must be admin."))

            if not services.project_has_valid_admins(project, exclude_user=self.object.user):
                raise ValidationError(
                    _("At least one user must be an active admin for this project.")
                )

        return attrs
Пример #11
0
    def validate_status_id(self, attrs, source):
        filters = {"project__id": attrs["project_id"], "id": attrs[source]}

        if not TaskStatus.objects.filter(**filters).exists():
            raise ValidationError(_("Invalid task status id."))

        return attrs
Пример #12
0
    def validate_milestone_id(self, attrs, source):
        filters = {"project__id": attrs["project_id"], "id": attrs[source]}

        if not Milestone.objects.filter(**filters).exists():
            raise ValidationError(_("Invalid milestone id."))

        return attrs
Пример #13
0
    def validate_read_new_terms(self, attrs, source):
        value = attrs[source]
        if not value:
            raise ValidationError(
                _("Read new terms has to be true'"))

        return attrs
    def validate_from_tags(self, attrs, source):
        tags = attrs.get(source, None)
        for tag in tags:
            if not services.tag_exist_for_project_elements(self.project, tag):
                raise ValidationError(_("The tag doesn't exist."))

        return attrs
Пример #15
0
    def validate_watchers(self, attrs, source):
        users = attrs.get(source, [])

        # Try obtain a valid project
        if self.object is None and "project" in attrs:
            project = attrs["project"]
        elif self.object:
            project = self.object.project
        else:
            project = None

        # If project is empty in all conditions, continue
        # without errors, because other validator should
        # validate the empty project field.
        if not project:
            return attrs

        # Check if incoming watchers are contained
        # in project members list
        member_ids = project.members.values_list("id", flat=True)
        existing_watcher_ids = project.get_watchers().values_list("id",
                                                                  flat=True)
        result = set(users).difference(member_ids).difference(
            existing_watcher_ids)
        if result:
            raise ValidationError(_("Watchers contains invalid users"))

        return attrs
Пример #16
0
    def validate(self, data):
        """
        All the userstories and the milestone are from the same project
        """
        user_story_ids = [us["us_id"] for us in data["bulk_stories"]]
        project = get_object_or_404(Project, pk=data["project_id"])

        if project.user_stories.filter(
                id__in=user_story_ids).count() != len(user_story_ids):
            raise ValidationError(
                "all the user stories must be from the same project")

        if project.milestones.filter(id=data["milestone_id"]).count() != 1:
            raise ValidationError("the milestone isn't valid for the project")

        return data
Пример #17
0
 def validate(self, value):
     """
     Validates that the input is in self.choices.
     """
     super(ChoiceField, self).validate(value)
     if value and not self.valid_value(value):
         raise ValidationError(self.error_messages["invalid_choice"] % {"value": value})
Пример #18
0
    def validate_project(self, attrs, source):
        # Create only
        if self.object is not None and self.object.project != attrs.get(
                "project"):
            raise ValidationError(_("Invalid operation"))

        return attrs
Пример #19
0
def validate_user_email_allowed_domains(value):
    validators.validate_email(value)

    domain_name = value.split("@")[1]

    if settings.USER_EMAIL_ALLOWED_DOMAINS and domain_name not in settings.USER_EMAIL_ALLOWED_DOMAINS:
        raise ValidationError(_("You email domain is not allowed"))
Пример #20
0
    def validate_username(self, attrs, source):
        username = attrs.get(source, None)
        try:
            validate_user_email_allowed_domains(username)

        except ValidationError:
            # If the validation comes from a request let's check the user is a valid contact
            request = self.context.get("request", None)
            if request is not None and request.user.is_authenticated:
                valid_usernames = request.user.contacts_visible_by_user(
                    request.user).values_list("username", flat=True)
                if username not in valid_usernames:
                    raise ValidationError(
                        _("The user must be a valid contact"))

        user = User.objects.filter(Q(username=username)
                                   | Q(email=username)).first()
        if user is not None:
            email = user.email
            self.user = user

        else:
            email = username

        self.email = email
        self._validate_member_doesnt_exist(attrs, email)
        return attrs
Пример #21
0
 def _validate_tag_field(value):
     for tag in value:
         if isinstance(tag, str):
             continue
         raise ValidationError(
             _("Invalid tag '{value}'. It must be the tag name.").
             format(value=tag))
Пример #22
0
    def field_from_native(self, data, files, field_name, into):
        if self.read_only:
            return

        try:
            if self.many:
                try:
                    # Form data
                    value = data.getlist(field_name)
                    if value == [""] or value == []:
                        raise KeyError
                except AttributeError:
                    # Non-form data
                    value = data[field_name]
            else:
                value = data[field_name]
        except KeyError:
            if self.partial:
                return
            value = self.get_default_value()

        if value in self.null_values:
            if self.required:
                raise ValidationError(self.error_messages["required"])
            into[(self.source or field_name)] = None
        elif self.many:
            into[(self.source
                  or field_name)] = [self.from_native(item) for item in value]
        else:
            into[(self.source or field_name)] = self.from_native(value)
Пример #23
0
    def validate_color(self, attrs, source):
        color = attrs.get(source, None)
        if color and not re.match('^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$',
                                  color):
            raise ValidationError(_("The color is not a valid HEX color."))

        return attrs
Пример #24
0
    def validate_source_project_slug(self, attrs, source):
        if source in attrs and attrs[source] is not None and attrs[source] != "":
            msg = _("An Epic has a related story from an external project (%(project)s) and cannot be imported") % {"project": attrs[source]}
            raise ValidationError(msg)

        attrs.pop(source, None)
        return attrs
Пример #25
0
    def from_native(self, value):
        if value in validators.EMPTY_VALUES:
            return None

        if isinstance(value, datetime.time):
            return value

        for format in self.input_formats:
            if format.lower() == ISO_8601:
                try:
                    parsed = parse_time(value)
                except (ValueError, TypeError):
                    pass
                else:
                    if parsed is not None:
                        return parsed
            else:
                try:
                    parsed = datetime.datetime.strptime(value, format)
                except (ValueError, TypeError):
                    pass
                else:
                    return parsed.time()

        msg = self.error_messages["invalid"] % readable_time_formats(self.input_formats)
        raise ValidationError(msg)
Пример #26
0
    def from_native(self, value):
        if value in validators.EMPTY_VALUES:
            return None

        if isinstance(value, datetime.datetime):
            if timezone and settings.USE_TZ and timezone.is_aware(value):
                # Convert aware datetimes to the default time zone
                # before casting them to dates (#17742).
                default_timezone = timezone.get_default_timezone()
                value = timezone.make_naive(value, default_timezone)
            return value.date()
        if isinstance(value, datetime.date):
            return value

        for format in self.input_formats:
            if format.lower() == ISO_8601:
                try:
                    parsed = parse_date(value)
                except (ValueError, TypeError):
                    pass
                else:
                    if parsed is not None:
                        return parsed
            else:
                try:
                    parsed = datetime.datetime.strptime(value, format)
                except (ValueError, TypeError):
                    pass
                else:
                    return parsed.date()

        msg = self.error_messages["invalid"] % readable_date_formats(self.input_formats)
        raise ValidationError(msg)
Пример #27
0
    def field_from_native(self, data, files, field_name, into):
        """
        Given a dictionary and a field name, updates the dictionary `into`,
        with the field and it's deserialized value.
        """
        if self.read_only:
            return

        try:
            data = data or {}
            if self.use_files:
                files = files or {}
                try:
                    native = files[field_name]
                except KeyError:
                    native = data[field_name]
            else:
                native = data[field_name]
        except KeyError:
            if self.default is not None and not self.partial:
                # Note: partial updates shouldn't set defaults
                native = self.get_default_value()
            else:
                if self.required:
                    raise ValidationError(self.error_messages["required"])
                return

        value = self.from_native(native)
        if self.source == "*":
            if value:
                into.update(value)
        else:
            self.validate(value)
            self.run_validators(value)
            into[self.source or field_name] = value
Пример #28
0
    def validate_bulk_memberships(self, attrs, source):
        project_id = attrs["project_id"]
        role_ids = [r["role_id"] for r in attrs["bulk_memberships"]]

        if Role.objects.filter(project_id=project_id, id__in=role_ids).count() != len(set(role_ids)):
            raise ValidationError(_("Invalid role ids. All roles must belong to the same project."))

        return attrs
Пример #29
0
    def validate_username(self, attrs, source):
        value = attrs[source]
        validator = core_validators.RegexValidator(re.compile(r'^[\w.-]+$'), _("invalid username"),
                                                   _("invalid"))

        try:
            validator(value)
        except ValidationError:
            raise ValidationError(_("Required. 255 characters or fewer. Letters, "
                                    "numbers and /./-/_ characters'"))

        if (self.object and
                self.object.username != value and
                User.objects.filter(username=value).exists()):
            raise ValidationError(_("Invalid username. Try with a different one."))

        return attrs
Пример #30
0
    def validate(self, attrs):
        request = self.context.get("request", None)
        if request is not None and request.user.is_authenticated and not request.user.verified_email:
            raise ValidationError(
                _("To add members to a project, first you have to verify your email address"
                  ))

        return super().validate(attrs)