Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved performance on the aggregator index page.

The previous version generated O(N) gratuitous COUNTs.
  • Loading branch information...
commit 36800b9c8dd90838937555c17a8391d84cf0d6b3 1 parent 8f0ff61
Jacob Kaplan-Moss authored
5  aggregator/views.py
@@ -20,7 +20,10 @@ def index(request):
20 20
     """
21 21
     Displays the latest feeds of each type.
22 22
     """
23  
-    ctx = {'feedtype_list': FeedType.objects.all()}
  23
+    feeds = []
  24
+    for ft in FeedType.objects.all():
  25
+        feeds.append((ft, ft.items()[0:5]))
  26
+    ctx = {'feedtype_list': feeds}
24 27
     return render(request, 'aggregator/index.html', ctx)
25 28
 
26 29
 class FeedListView(ListView):
9  templates/aggregator/index.html
@@ -7,6 +7,7 @@
7 7
   font-weight: 600;
8 8
   list-style: none;
9 9
   padding: 20px 0;
  10
+}
10 11
 </style>
11 12
 {% endblock %}
12 13
 
@@ -22,21 +23,21 @@ <h2 class="deck">This page, updated regularly, aggregates what's going on in the
22 23
 </ul>
23 24
 {% endif %}
24 25
 
25  
-{% for feedtype in feedtype_list %}
  26
+{% for feedtype, latest_feeds in feedtype_list %}
26 27
 <div id="{{ feedtype.slug }}" class="module {% cycle "first" "last" %}">
27 28
   <h3 class="header">
28 29
     {{ feedtype.name }}
29 30
     <a class="rss" href="{% url 'aggregator-feed' slug=feedtype.slug %}">RSS</a>
30 31
   </h3>
31  
-  {% for item in feedtype.items|slice:":5" %}
  32
+  {% for item in latest_feeds %}
32 33
     <h5><a href="{{ item.link }}">{{ item.title }}</a></h5>
33 34
     <p class="date">{{ item.date_modified|date:"N jS, Y \a\t P" }} by <a href="{{ item.feed.public_url }}">{{ item.feed.title }}</a></p>
34 35
   {% endfor %}
35 36
   <p>
36  
-    {% if feedtype.items|length %}
  37
+    {% if latest_feeds %}
37 38
       <a href="{% url 'community-feed-list' feedtype.slug %}">View more</a>
38 39
     {% endif %}
39  
-    {% if feedtype.items and feedtype.can_self_add %}
  40
+    {% if latest_feeds and feedtype.can_self_add %}
40 41
       or
41 42
     {% endif %}
42 43
     {% if feedtype.can_self_add %}

0 notes on commit 36800b9

Please sign in to comment.
Something went wrong with that request. Please try again.