remove contest form link
[wolnelektury.git] / src / social / utils.py
index bf1c242..f8fb241 100755 (executable)
@@ -3,6 +3,8 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from collections import defaultdict
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from collections import defaultdict
+from random import randint
+
 from django.contrib.contenttypes.models import ContentType
 from django.db.models import Q
 from django.utils.functional import lazy
 from django.contrib.contenttypes.models import ContentType
 from django.db.models import Q
 from django.utils.functional import lazy
@@ -21,7 +23,7 @@ def likes(user, work, request=None):
 
     if not hasattr(request, 'social_likes'):
         # tuple: unchecked, checked, liked
 
     if not hasattr(request, 'social_likes'):
         # tuple: unchecked, checked, liked
-        request.social_likes = defaultdict(lambda:(set(), set(), set()))
+        request.social_likes = defaultdict(lambda: (set(), set(), set()))
 
     ct = ContentType.objects.get_for_model(type(work))
     likes_t = request.social_likes[ct.pk]
 
     ct = ContentType.objects.get_for_model(type(work))
     likes_t = request.social_likes[ct.pk]
@@ -29,6 +31,7 @@ def likes(user, work, request=None):
         return work.pk in likes_t[2]
     else:
         likes_t[0].add(work.pk)
         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])
         def _likes():
             if likes_t[0]:
                 ids = tuple(likes_t[0])
@@ -47,8 +50,8 @@ def get_set(user, name):
     try:
         tag = Tag.objects.get(category='set', user=user, name=name)
     except Tag.DoesNotExist:
     try:
         tag = Tag.objects.get(category='set', user=user, name=name)
     except Tag.DoesNotExist:
-        tag = Tag.objects.create(category='set', user=user, name=name,
-                slug=utils.get_random_hash(name), sort_key=name.lower())
+        tag = Tag.objects.create(
+            category='set', user=user, name=name, slug=utils.get_random_hash(name), sort_key=name.lower())
     return tag
 
 
     return tag
 
 
@@ -68,7 +71,42 @@ def set_sets(user, work, sets):
     # delete empty tags
     Tag.objects.filter(category='set', user=user, items=None).delete()
 
     # delete empty tags
     Tag.objects.filter(category='set', user=user, items=None).delete()
 
+    if isinstance(work, Book):
+        work.update_popularity()
+
 
 def cites_for_tags(tags):
     """Returns a QuerySet with all Cites for books with given tags."""
     return Cite.objects.filter(book__in=Book.tagged.with_all(tags))
 
 def cites_for_tags(tags):
     """Returns a QuerySet with all Cites for books with given tags."""
     return Cite.objects.filter(book__in=Book.tagged.with_all(tags))
+
+
+# tag_ids is never used
+def choose_cite(book_id=None, tag_ids=None):
+    """Choose a cite for main page, for book or for set of tags."""
+    if book_id is not None:
+        cites = Cite.objects.filter(Q(book=book_id) | Q(book__ancestor=book_id))
+    elif tag_ids is not None:
+        tags = Tag.objects.filter(pk__in=tag_ids)
+        cites = cites_for_tags(tags)
+    else:
+        cites = Cite.objects.all()
+    stickies = cites.filter(sticky=True)
+    count = len(stickies)
+    if count:
+        cites = stickies
+    else:
+        count = len(cites)
+    if count:
+        cite = cites[randint(0, count - 1)]
+    else:
+        cite = None
+    return cite
+
+
+def get_or_choose_cite(request, book_id=None, tag_ids=None):
+    try:
+        assert request.user.is_staff
+        assert 'choose_cite' in request.GET
+        return Cite.objects.get(pk=request.GET['choose_cite'])
+    except (AssertionError, Cite.DoesNotExist):
+        return choose_cite(book_id, tag_ids)