1 from django.db.models import Q
2 from catalogue.models import Tag
3 from catalogue import utils
4 from catalogue.tasks import touch_tag
8 return user.is_authenticated() and work.tags.filter(category='set', user=user).exists()
11 def get_set(user, name):
12 """Returns a tag for use by the user. Creates it, if necessary."""
14 tag = Tag.objects.get(category='set', user=user, name=name)
15 except Tag.DoesNotExist:
16 tag = Tag.objects.create(category='set', user=user, name=name,
17 slug=utils.get_random_hash(name), sort_key=name.lower())
21 def set_sets(user, work, sets):
22 """Set tags used for given work by a given user."""
24 old_sets = list(work.tags.filter(category='set', user=user))
26 work.tags = sets + list(
27 work.tags.filter(~Q(category='set') | ~Q(user=user)))
29 for shelf in [shelf for shelf in old_sets if shelf not in sets]:
31 for shelf in [shelf for shelf in sets if shelf not in old_sets]:
35 Tag.objects.filter(category='set', user=user, book_count=0).delete()