1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
4 from django.contrib.sites.models import Site
5 from django.utils.functional import lazy
6 from catalogue.models import Book, Tag
10 lambda: 'https://' + Site.objects.get_current().domain, str)()
22 def read_tags(tags, request, allowed):
23 """ Reads a path of filtering tags.
25 :param str tags: a path of category and slug pairs, like: authors/an-author/...
26 :returns: list of Tag objects
27 :raises: ValueError when tags can't be found
30 def process(category, slug):
31 if category == 'book':
34 books.append(Book.objects.get(slug=slug))
35 except Book.DoesNotExist:
36 raise ValueError('Unknown book.')
38 real_tags.append(Tag.objects.get(category=category, slug=slug))
39 except Tag.DoesNotExist:
40 raise ValueError('Tag not found')
45 tags = tags.strip('/').split('/')
49 category = tags.pop(0)
53 category = category_singular[category]
55 raise ValueError('Unknown category.')
57 if category not in allowed:
58 raise ValueError('Category not allowed.')
59 process(category, slug)
61 for key in request.GET:
62 if key in category_singular:
63 category = category_singular[key]
64 if category in allowed:
65 for slug in request.GET.getlist(key):
66 process(category, slug)
67 return real_tags, books