2 from .base import MetaValue
5 class WLURI(MetaValue):
6 """Represents a WL URI. Extracts slug from it."""
7 example = 'http://wolnelektury.pl/katalog/lektura/template/'
8 _re_wl_uri = re.compile(
9 r'http://(www\.)?wolnelektury.pl/katalog/lektur[ay]/'
10 '(?P<slug>[-a-z0-9]+)/?$'
13 def __init__(self, slug, uri=None):
14 """Contructs an URI from slug.
16 >>> print(WLURI.from_slug('a-slug').uri)
17 http://wolnelektury.pl/katalog/lektura/a-slug/
21 uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
23 return super().__init__(slug)
26 def from_text(cls, uri):
27 slug = uri.rstrip('/').rsplit('/', 1)[-1]
31 match = self._re_wl_uri.match(self.uri)
33 raise ValidationError('Invalid URI (%s). Should match: %s' % (
34 self.uri, self._re_wl_uri.pattern))
39 def __eq__(self, other):
40 return self.value == other.value