1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.db.models import Q
6 from catalogue.models import Book, Tag
7 from catalogue import utils
8 from catalogue.tasks import touch_tag
9 from social.models import Cite
12 def likes(user, work):
13 return user.is_authenticated() and work.tags.filter(category='set', user=user).exists()
16 def get_set(user, name):
17 """Returns a tag for use by the user. Creates it, if necessary."""
19 tag = Tag.objects.get(category='set', user=user, name=name)
20 except Tag.DoesNotExist:
21 tag = Tag.objects.create(category='set', user=user, name=name,
22 slug=utils.get_random_hash(name), sort_key=name.lower())
26 def set_sets(user, work, sets):
27 """Set tags used for given work by a given user."""
29 old_sets = list(work.tags.filter(category='set', user=user))
31 work.tags = sets + list(
32 work.tags.filter(~Q(category='set') | ~Q(user=user)))
34 for shelf in [shelf for shelf in old_sets if shelf not in sets]:
36 for shelf in [shelf for shelf in sets if shelf not in old_sets]:
40 Tag.objects.filter(category='set', user=user, items=None).delete()
43 def cites_for_tags(tags):
44 """Returns a QuerySet with all Cites for books with given tags."""
45 return Cite.objects.filter(book__in=Book.tagged.with_all(tags))