2 from .base import MetaValue
5 class WLURI(MetaValue):
6 """Represents a WL URI. Extracts slug from it."""
9 example = 'http://wolnelektury.pl/katalog/lektura/template/'
10 _re_wl_uri = re.compile(
11 r'http://(www\.)?wolnelektury.pl/katalog/lektur[ay]/'
12 '(?P<slug>[-a-z0-9]+)/?$'
15 def __init__(self, slug, uri=None):
16 """Contructs an URI from slug.
18 >>> print(WLURI.from_slug('a-slug').uri)
19 http://wolnelektury.pl/katalog/lektura/a-slug/
23 uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
25 return super().__init__(slug)
29 def from_text(cls, uri):
30 slug = uri.rstrip('/').rsplit('/', 1)[-1]
34 match = self._re_wl_uri.match(self.uri)
36 raise ValidationError('Invalid URI (%s). Should match: %s' % (
37 self.uri, self._re_wl_uri.pattern))
43 def __eq__(self, other):
44 return self.slug == other.slug