minor refactoring
authorJan Szejko <j-sz@o2.pl>
Thu, 4 Feb 2016 12:45:46 +0000 (13:45 +0100)
committerJan Szejko <j-sz@o2.pl>
Thu, 4 Feb 2016 12:45:46 +0000 (13:45 +0100)
src/social/templatetags/social_tags.py

index 9ba3acc..484b52e 100755 (executable)
@@ -21,36 +21,36 @@ def likes_book(request, book_id):
     return likes(request.user, Book.objects.get(pk=book_id), request)
 
 
-def choose_cite(request, book_id=None, tag_ids=None):
+def choose_cite(book_id=None, tag_ids=None):
     """Choose a cite for main page, for book or for set of tags."""
-    try:
-        assert request.user.is_staff
-        assert 'choose_cite' in request.GET
-        cite = Cite.objects.get(pk=request.GET['choose_cite'])
-    except (AssertionError, Cite.DoesNotExist):
-        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 = stickies.count()
+    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 = stickies.count()
+    if count:
+        cite = stickies[randint(0, count - 1)]
+    else:
+        count = cites.count()
         if count:
-            cite = stickies[randint(0, count - 1)]
+            cite = cites[randint(0, count - 1)]
         else:
-            count = cites.count()
-            if count:
-                cite = cites[randint(0, count - 1)]
-            else:
-                cite = None
+            cite = None
     return cite
 
 
 @ssi_variable(register, name='choose_cite', patch_response=[add_never_cache_headers])
 def choose_cite_tag(request, book_id=None, tag_ids=None):
-    cite = choose_cite(request, book_id, tag_ids)
+    try:
+        assert request.user.is_staff
+        assert 'choose_cite' in request.GET
+        cite = Cite.objects.get(pk=request.GET['choose_cite'])
+    except (AssertionError, Cite.DoesNotExist):
+        cite = choose_cite(book_id, tag_ids)
     return cite.pk if cite is not None else None