convert some more ssi to template fragment cache
authorJan Szejko <jan.szejko@gmail.com>
Fri, 22 Apr 2016 11:50:22 +0000 (13:50 +0200)
committerJan Szejko <jan.szejko@gmail.com>
Fri, 22 Apr 2016 11:50:22 +0000 (13:50 +0200)
src/catalogue/templates/catalogue/collection_box.html
src/catalogue/views.py
src/social/templatetags/social_tags.py
src/social/utils.py
src/wolnelektury/settings/cache.py
src/wolnelektury/templates/main_page.html
src/wolnelektury/views.py

index ac59fbe..0e27338 100644 (file)
@@ -3,19 +3,18 @@
   {% load ssi_include from ssify %}
   {% load cache %}
   <div class="collection-box white-box">
-    <h2><a href="{{ obj.get_absolute_url }}">{% trans "Collection" %}: {{ obj }}</a></h2>
-    {% if obj.description %}
-      {{ obj.description|safe|truncatewords_html:40 }}
+    <h2><a href="{{ collection.get_absolute_url }}">{% trans "Collection" %}: {{ collection }}</a></h2>
+    {% 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 %}
-        <a href="{{ obj.get_absolute_url }}">
+        <a href="{{ collection.get_absolute_url }}">
           {% blocktrans count c=more %}and one more{% plural %}and {{ c }} more{% endblocktrans %}
         </a>
       {% endif %}
index 81d0a02..2aa98f7 100644 (file)
@@ -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,
     })
 
 
index 484b52e..4cc6215 100755 (executable)
@@ -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
 
 
index 63a4227..f8fb241 100755 (executable)
@@ -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)
index 69ce6cb..107ac0d 100644 (file)
@@ -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
index deea0a5..c3468ba 100755 (executable)
@@ -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 %}
 
 {% 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 %}
 
     <section id="main-library">
       <h1>W naszej cyfrowej bibliotece znajdziesz</h1>
@@ -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 %}
         </div>
         <a href="{% url "book_list" %}">
           {% 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 %}
       </div>
       <a class="more" href="{% url 'theme_catalogue' %}">Zobacz katalog motywów</a>
@@ -89,7 +91,9 @@
     {% if collection %}
       <section>
         <h1>Kolekcje</h1>
-        {% ssi_include 'catalogue_collection_box' pk=collection.pk %}
+        {% cache 3600 collection_box collection.pk LANGUAGE_CODE %}
+          {% include 'catalogue/collection_box.html' %}
+        {% endcache %}
         <a class="more" href="{% url 'catalogue_collections' %}">Zobacz katalog kolekcji</a>
       </section>
     {% endif %}
           {% cache 86400 book_mini_box book.pk %}
             {% include 'catalogue/book_mini_box.html' %}
           {% endcache %}
-          {#% ssi_include 'catalogue_book_mini' pk=book.pk %#}
         {% endfor %}
       <a class="more" href="{% url 'recent_list' %}">{% trans "More recent publications" %}</a>
     </section>
 
 {% block add_footer %}
   {% spaceless %}
-    {{ cite_pk.if }}
-      <p>{% trans "Image used:" %}
-        {% ssi_include 'social_cite_info' pk=cite_pk %}
+    {% if cite %}
+      <p>
+        {% trans "Image used:" %}
+        {% cache 3600 cite_info cite.pk %}
+          {% include 'social/cite_info.html' %}
+        {% endcache %}
       </p>
-    {{ cite_pk.endif }}
+    {% endif %}
   {% endspaceless %}
 {% endblock %}
index 1a6738b..265f766 100644 (file)
@@ -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'):