From: Jan Szejko Date: Fri, 22 Apr 2016 11:50:22 +0000 (+0200) Subject: convert some more ssi to template fragment cache X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/c3af41ec13e26ad2b4cb000291ff989dc1b3686f?ds=sidebyside convert some more ssi to template fragment cache --- diff --git a/src/catalogue/templates/catalogue/collection_box.html b/src/catalogue/templates/catalogue/collection_box.html index ac59fbe86..0e2733862 100644 --- a/src/catalogue/templates/catalogue/collection_box.html +++ b/src/catalogue/templates/catalogue/collection_box.html @@ -3,19 +3,18 @@ {% load ssi_include from ssify %} {% load cache %}
-

{% trans "Collection" %}: {{ obj }}

- {% if obj.description %} - {{ obj.description|safe|truncatewords_html:40 }} +

{% trans "Collection" %}: {{ collection }}

+ {% if collection.description %} + {{ collection.description|safe|truncatewords_html:40 }} {% endif %} - {% for book in obj.get_books|slice:":5" %} + {% for book in collection.get_books|slice:":5" %} {% cache 86400 book_mini_box book.pk %} {% include 'catalogue/book_mini_box.html' %} {% endcache %} - {#% ssi_include 'catalogue_book_mini' pk=book.pk %#} {% endfor %} - {% with obj.get_books.count|add:-5 as more %} + {% with collection.get_books.count|add:-5 as more %} {% if more > 0 %} - + {% blocktrans count c=more %}and one more{% plural %}and {{ c }} more{% endblocktrans %} {% endif %} diff --git a/src/catalogue/views.py b/src/catalogue/views.py index 81d0a02b8..2aa98f721 100644 --- a/src/catalogue/views.py +++ b/src/catalogue/views.py @@ -731,10 +731,10 @@ def tag_box(request, pk): @ssi_included def collection_box(request, pk): - obj = get_object_or_404(Collection, pk=pk) + collection = get_object_or_404(Collection, pk=pk) return render(request, 'catalogue/collection_box.html', { - 'obj': obj, + 'collection': collection, }) diff --git a/src/social/templatetags/social_tags.py b/src/social/templatetags/social_tags.py index 484b52ecb..4cc621525 100755 --- a/src/social/templatetags/social_tags.py +++ b/src/social/templatetags/social_tags.py @@ -2,16 +2,13 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from random import randint -from django.db.models import Q from django import template from django.utils.functional import lazy from django.utils.cache import add_never_cache_headers -from catalogue.models import Book, Tag +from catalogue.models import Book from ssify import ssi_variable from ssify.utils import ssi_vary_on_cookie -from social.models import Cite -from social.utils import likes, cites_for_tags +from social.utils import likes, get_or_choose_cite register = template.Library() @@ -21,36 +18,9 @@ def likes_book(request, book_id): return likes(request.user, Book.objects.get(pk=book_id), request) -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 = stickies.count() - if count: - cite = stickies[randint(0, count - 1)] - else: - count = cites.count() - if count: - cite = cites[randint(0, count - 1)] - else: - 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): - 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) + cite = get_or_choose_cite(request, book_id, tag_ids) return cite.pk if cite is not None else None diff --git a/src/social/utils.py b/src/social/utils.py index 63a42279b..f8fb2410a 100755 --- a/src/social/utils.py +++ b/src/social/utils.py @@ -3,6 +3,8 @@ # 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 @@ -76,3 +78,35 @@ def set_sets(user, work, sets): 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) diff --git a/src/wolnelektury/settings/cache.py b/src/wolnelektury/settings/cache.py index 69ce6cb63..107ac0db7 100644 --- a/src/wolnelektury/settings/cache.py +++ b/src/wolnelektury/settings/cache.py @@ -17,6 +17,13 @@ CACHES = { '127.0.0.1:11211', ], }, + 'template_fragments': { + 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', + 'TIMEOUT': 86400, + 'LOCATION': [ + '127.0.0.1:11211', + ], + }, } CACHE_MIDDLEWARE_SECONDS = 24 * 60 * 60 diff --git a/src/wolnelektury/templates/main_page.html b/src/wolnelektury/templates/main_page.html index deea0a53c..c3468ba8e 100755 --- a/src/wolnelektury/templates/main_page.html +++ b/src/wolnelektury/templates/main_page.html @@ -1,8 +1,9 @@ {% extends "base/base.html" %} {% load static from staticfiles %} -{% load i18n catalogue_tags infopages_tags social_tags %} +{% load i18n catalogue_tags infopages_tags %} {% load ssi_include from ssify %} {% load cache %} +{% get_current_language as LANGUAGE_CODE %} {% block title %}{% trans "Wolne Lektury internet library" %}{% endblock %} @@ -10,10 +11,11 @@ {% block body %} {% spaceless %} - {% choose_cite as cite_pk %} - {{ cite_pk.if }} - {% ssi_include 'social_cite_main' pk=cite_pk %} - {{ cite_pk.endif }} + {% if cite %} + {% cache 3600 main_cite cite.pk %} + {% include "social/cite_promo.html" with main=True %} + {% endcache %} + {% endif %}

W naszej cyfrowej bibliotece znajdziesz

@@ -23,7 +25,6 @@ {% cache 86400 book_mini_box book.pk %} {% include 'catalogue/book_mini_box.html' %} {% endcache %} - {#% ssi_include 'catalogue_book_mini' pk=b.pk %#} {% endfor %}
@@ -43,10 +44,11 @@ {% cache 86400 book_mini_box book.pk %} {% include 'catalogue/book_mini_box.html' %} {% endcache %} - {#% ssi_include 'catalogue_book_mini' pk=book.pk %#} {% endfor %} {% if theme_fragment %} - {% ssi_include 'catalogue_fragment_promo' pk=theme_fragment.pk %} + {% cache 3600 fragment_promo theme_fragment.pk %} + {% include 'catalogue/fragment_promo.html' with fragment=theme_fragment %} + {% endcache %} {% endif %} Zobacz katalog motywów @@ -89,7 +91,9 @@ {% if collection %}

Kolekcje

- {% ssi_include 'catalogue_collection_box' pk=collection.pk %} + {% cache 3600 collection_box collection.pk LANGUAGE_CODE %} + {% include 'catalogue/collection_box.html' %} + {% endcache %} Zobacz katalog kolekcji
{% endif %} @@ -100,7 +104,6 @@ {% cache 86400 book_mini_box book.pk %} {% include 'catalogue/book_mini_box.html' %} {% endcache %} - {#% ssi_include 'catalogue_book_mini' pk=book.pk %#} {% endfor %} {% trans "More recent publications" %} @@ -151,10 +154,13 @@ {% block add_footer %} {% spaceless %} - {{ cite_pk.if }} -

{% trans "Image used:" %} - {% ssi_include 'social_cite_info' pk=cite_pk %} + {% if cite %} +

+ {% trans "Image used:" %} + {% cache 3600 cite_info cite.pk %} + {% include 'social/cite_info.html' %} + {% endcache %}

- {{ cite_pk.endif }} + {% endif %} {% endspaceless %} {% endblock %} diff --git a/src/wolnelektury/views.py b/src/wolnelektury/views.py index 1a6738ba0..265f76694 100644 --- a/src/wolnelektury/views.py +++ b/src/wolnelektury/views.py @@ -21,11 +21,14 @@ from ajaxable.utils import placeholdized from catalogue.models import Book, Collection, Tag, Fragment from ssify import ssi_included +from social.utils import get_or_choose_cite + def main_page(request): ctx = { 'last_published': Book.objects.exclude(cover_thumb='').filter(parent=None).order_by('-created_at')[:6], - 'theme_books': [] + 'theme_books': [], + 'cite': get_or_choose_cite(request), } # for category in ('author', 'epoch', 'genre', 'kind'):