+def likes(user, work, request=None):
+ if not user.is_authenticated():
+ return False
+
+ if request is None:
+ return work.tags.filter(category='set', user=user).exists()
+
+ if not hasattr(request, 'social_likes'):
+ # tuple: unchecked, checked, liked
+ request.social_likes = defaultdict(lambda:(set(), set(), set()))
+
+ ct = ContentType.objects.get_for_model(type(work))
+ likes_t = request.social_likes[ct.pk]
+ if work.pk in likes_t[1]:
+ return work.pk in likes_t[2]
+ else:
+ likes_t[0].add(work.pk)
+ def _likes():
+ if likes_t[0]:
+ ids = tuple(likes_t[0])
+ likes_t[0].clear()
+ likes_t[2].update(Tag.intermediary_table_model.objects.filter(
+ content_type_id=ct.pk, tag__user_id=user.pk,
+ object_id__in=ids
+ ).distinct().values_list('object_id', flat=True))
+ likes_t[1].update(ids)
+ return work.pk in likes_t[2]
+ return lazy(_likes, bool)()