Beispiel #1
0
    def create_item(self, path):
        """Creates a new Location object for the site.

        The location path should be canonical and should not contain
        parts that are not used for access control (query, fragment,
        parameters). Location should not contain non-ascii characters.

        Raises:
            ValidationError if the path is invalid or if a site
            already has a location with such path.
            LimitExceeded if the site defines a maximum number of
            locations and adding a new one would exceed this number.
        """

        locations_limit = self.site.locations_limit
        if (locations_limit is not None and self.count() >= locations_limit):
            raise LimitExceeded('Locations limit exceeded')

        if not url_utils.is_canonical(path):
            raise ValidationError(
                'Path should be absolute and normalized (starting with / '\
                    'without /../ or /./ or //).')
        if len(path) > self.PATH_LEN_LIMIT:
            raise ValidationError('Path too long')
        if url_utils.contains_fragment(path):
            raise ValidationError(
                "Path should not contain fragment ('#' part).")
        if url_utils.contains_query(path):
            raise ValidationError(
                "Path should not contain query ('?' part).")
        if url_utils.contains_params(path):
            raise ValidationError(
                "Path should not contain parameters (';' part).")
        try:
            path.encode('ascii')
        except UnicodeError:
            raise ValidationError(
                'Path should contain only ascii characters.')

        if self.get_unique(lambda item: item.path == path) is not None:
            raise ValidationError('Location already exists.')

        return self._do_create_item(path=path)
    def create_item(self, path):
        """Creates a new Location object for the site.

        The location path should be canonical and should not contain
        parts that are not used for access control (query, fragment,
        parameters). Location should not contain non-ascii characters.

        Raises:
            ValidationError if the path is invalid or if a site
            already has a location with such path.
            LimitExceeded if the site defines a maximum number of
            locations and adding a new one would exceed this number.
        """

        locations_limit = self.site.locations_limit
        if (locations_limit is not None and self.count() >= locations_limit):
            raise LimitExceeded('Locations limit exceeded')

        if not url_utils.is_canonical(path):
            raise ValidationError(
                'Path should be absolute and normalized (starting with / '\
                    'without /../ or /./ or //).')
        if len(path) > self.PATH_LEN_LIMIT:
            raise ValidationError('Path too long')
        if url_utils.contains_fragment(path):
            raise ValidationError(
                "Path should not contain fragment ('#' part).")
        if url_utils.contains_query(path):
            raise ValidationError("Path should not contain query ('?' part).")
        if url_utils.contains_params(path):
            raise ValidationError(
                "Path should not contain parameters (';' part).")
        try:
            path.encode('ascii')
        except UnicodeError:
            raise ValidationError('Path should contain only ascii characters.')

        if self.get_unique(lambda item: item.path == path) is not None:
            raise ValidationError('Location already exists.')

        return self._do_create_item(path=path)
Beispiel #3
0
 def test_contains_fragment(self):
     self.assertTrue(contains_params('/foo;'))
     self.assertFalse(contains_params('/foo'))