1 from .base import MetaValue
4 class WLURI(MetaValue):
5 """Represents a WL URI. Extracts slug from it."""
8 example = 'http://wolnelektury.pl/katalog/lektura/template/'
9 _re_wl_uri = re.compile(
10 r'http://(www\.)?wolnelektury.pl/katalog/lektur[ay]/'
11 '(?P<slug>[-a-z0-9]+)/?$'
14 def __init__(self, slug, uri=None):
15 """Contructs an URI from slug.
17 >>> print(WLURI.from_slug('a-slug').uri)
18 http://wolnelektury.pl/katalog/lektura/a-slug/
22 uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
24 return super().__init__(slug)
28 def from_text(cls, uri):
29 slug = uri.rstrip('/').rsplit('/', 1)[-1]
33 match = self._re_wl_uri.match(self.uri)
35 raise ValidationError('Invalid URI (%s). Should match: %s' % (
36 self.uri, self._re_wl_uri.pattern))
42 def __eq__(self, other):
43 return self.slug == other.slug