示例#1
0
文件: models.py 项目: viciu/wwwhisper
    def create_item(self, path):
        """Creates a new Location object.

        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.

        Args:
            path: A canonical path to the location.

        Raises:
            CreationException if the path is invalid or if a location
            with such path already exists.
        """

        if not url_path.is_canonical(path):
            raise CreationException(
                'Path should be absolute and normalized (starting with / '\
                    'without /../ or /./ or //).')
        if url_path.contains_fragment(path):
            raise CreationException(
                "Path should not contain fragment ('#' part).")
        if url_path.contains_query(path):
            raise CreationException(
                "Path should not contain query ('?' part).")
        if url_path.contains_params(path):
            raise CreationException(
                "Path should not contain parameters (';' part).")
        try:
            if path.encode('utf-8', 'strict') != path:
                raise CreationException(
                    'Path should contain only ascii characters.')
        except UnicodeError:
            raise CreationException('Invalid path encoding')

        if _find(Location, path=path) is not None:
            raise CreationException('Location already exists.')
        location = Location.objects.create(path=path)
        location.save()
        return location
示例#2
0
 def test_contains_query(self):
     self.assertTrue(contains_query("/foo?"))
     self.assertFalse(contains_query("/foo"))