From: Jan Szejko Date: Thu, 4 Feb 2016 12:45:46 +0000 (+0100) Subject: minor refactoring X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/79ac7d1116560971a70246601957995789536f9f minor refactoring --- diff --git a/src/social/templatetags/social_tags.py b/src/social/templatetags/social_tags.py index 9ba3acc54..484b52ecb 100755 --- a/src/social/templatetags/social_tags.py +++ b/src/social/templatetags/social_tags.py @@ -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