4503e2493b8d

Refactored Thoughts to use Djangos built-in pagination.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Thu, 12 Feb 2009 13:56:47 -0500
parents 54160ba670db
children ef26da70b954
branches/tags (none)
files site-media/style/thoughts.css templates/thoughts/list.html thoughts/views.py

Changes

--- a/site-media/style/thoughts.css	Mon Feb 09 19:55:19 2009 -0500
+++ b/site-media/style/thoughts.css	Thu Feb 12 13:56:47 2009 -0500
@@ -33,4 +33,8 @@
     padding-left: 25px;
     margin-left: 14px;
     border-left: 1px solid #eee;
+}
+
+span#thought-list-newer {
+    float: right;
 }
\ No newline at end of file
--- a/templates/thoughts/list.html	Mon Feb 09 19:55:19 2009 -0500
+++ b/templates/thoughts/list.html	Thu Feb 12 13:56:47 2009 -0500
@@ -5,75 +5,75 @@
 {% block title %}Thoughts{% endblock %}
 
 {% block style %}
-	<link rel="stylesheet" href="/site-media/style/thoughts.css" 
-		  type="text/css"/>
+    <link rel="stylesheet" href="/site-media/style/thoughts.css" 
+          type="text/css"/>
 {% endblock %}
 
 {% block header %}/ thoughts{% endblock %}
 
 {% block content %}
-	<div id="thoughts-list">
-		{% for thought in thoughts %}
-			<div class="thought">
-				{% ifequal thought.type 'text' %}
-					<a name="text-{{ thought.id }}"></a>
-					<div class="thought-type span-2">
-						<h2>text</h2>
-					</div>
-					<div class="thought-content text-thought colborder-left span-14 last">
-						{% ifnotequal thought.title None %}
-							<h2>{{ thought.title|typogrify }}</h2>
-						{% endifnotequal %}
-						{{ thought.body|markdown|typogrify }}
-					</div>
-				{% endifequal %}
-				{% ifequal thought.type 'link' %}
-					<a name="link-{{ thought.id }}"></a>
-					<div class="thought-type span-2">
-						<h2>link</h2>
-					</div>
-					<div class="thought-content link-thought colborder-left span-14 last">
-						<h2><a href="{{ thought.url }}">
-							{% if thought.name %}
-								{{ thought.name|typogrify }}
-							{% else %}
-								{{ thought.url }}
-							{% endif %}
-						</a></h2>
-						{% if thought.description %}
-							{{ thought.description|markdown|typogrify }}
-						{% endif %}
-					</div>
-				{% endifequal %}
-			</div>
-		{% endfor %}
-		
-		{% ifnotequal newer_page older_page  %}
-			<div class="thought thought-navigation">
-			{% ifnotequal newer_page None %}
-				{% ifequal newer_page 0 %}
-					{% url thoughts-list-newest as new_page %}
-				{% else %}
-					{% url thoughts-list-page newer_page as new_page %}
-				{% endifequal %}
-				<span id="thought-list-newer">
-					<h2><a href="{{ new_page }}">
-						Newer &raquo;
-					</a></h2>
-				</span>
-			{% endifnotequal %}
-			{% ifnotequal older_page None %}
-				<span id="thought-list-older">
-					<h2><a href="{% url thoughts-list-page older_page %}">
-						&laquo; Older
-					</a></h2>
-				</span>
-			{% else %}
-				<span id="thought-list-older">
-					<h2>&nbsp;</h2>
-				</span>
-			{% endifnotequal %}
-			</div>
-		{% endifnotequal %}
-	</div>
+    <div id="thoughts-list">
+        {% for thought in thoughts %}
+            <div class="thought">
+                {% ifequal thought.type 'text' %}
+                    <a name="text-{{ thought.id }}"></a>
+                    <div class="thought-type span-2">
+                        <h2>text</h2>
+                    </div>
+                    <div class="thought-content text-thought colborder-left span-14 last">
+                        {% ifnotequal thought.title None %}
+                            <h2>{{ thought.title|typogrify }}</h2>
+                        {% endifnotequal %}
+                        {{ thought.body|markdown|typogrify }}
+                    </div>
+                {% endifequal %}
+                {% ifequal thought.type 'link' %}
+                    <a name="link-{{ thought.id }}"></a>
+                    <div class="thought-type span-2">
+                        <h2>link</h2>
+                    </div>
+                    <div class="thought-content link-thought colborder-left span-14 last">
+                        <h2><a href="{{ thought.url }}">
+                            {% if thought.name %}
+                                {{ thought.name|typogrify }}
+                            {% else %}
+                                {{ thought.url }}
+                            {% endif %}
+                        </a></h2>
+                        {% if thought.description %}
+                            {{ thought.description|markdown|typogrify }}
+                        {% endif %}
+                    </div>
+                {% endifequal %}
+            </div>
+        {% endfor %}
+        
+        {% ifnotequal newer_page older_page  %}
+            <div class="thought thought-navigation prepend-3">
+            {% ifnotequal newer_page None %}
+                {% ifequal newer_page 0 %}
+                    {% url thoughts-list-newest as new_page %}
+                {% else %}
+                    {% url thoughts-list-page newer_page as new_page %}
+                {% endifequal %}
+                <span id="thought-list-newer">
+                    <h2><a href="{{ new_page }}">
+                        Newer &raquo;
+                    </a></h2>
+                </span>
+            {% endifnotequal %}
+            {% ifnotequal older_page None %}
+                <span id="thought-list-older">
+                    <h2><a href="{% url thoughts-list-page older_page %}">
+                        &laquo; Older
+                    </a></h2>
+                </span>
+            {% else %}
+                <span id="thought-list-older">
+                    <h2>&nbsp;</h2>
+                </span>
+            {% endifnotequal %}
+            </div>
+        {% endifnotequal %}
+    </div>
 {% endblock %}
\ No newline at end of file
--- a/thoughts/views.py	Mon Feb 09 19:55:19 2009 -0500
+++ b/thoughts/views.py	Thu Feb 12 13:56:47 2009 -0500
@@ -1,23 +1,23 @@
 from stevelosh.thoughts.models import TextThought, LinkThought
 from django.shortcuts import render_to_response
+from django.core.paginator import Paginator
 import operator
 
 ENTRIES_PER_PAGE = 10
 
-def list(request, page=0):
+def list(request, page=1):
     page = int(page)
-    start_index = page * ENTRIES_PER_PAGE
-    end_index = start_index + ENTRIES_PER_PAGE
-    total_count = TextThought.objects.count() + LinkThought.objects.count()
     
     thoughts = []
     thoughts += TextThought.objects.all().order_by('-posted')
     thoughts += LinkThought.objects.all().order_by('-posted')
     thoughts.sort(key=operator.attrgetter('posted'))
     thoughts.reverse()
-    thoughts = thoughts[start_index:end_index]
+    
+    paginator = Paginator(thoughts, 5, orphans=2)
+    p = paginator.page(page)
     
     return render_to_response('thoughts/list.html', 
-        { 'thoughts': thoughts,
-          'older_page': page+1 if end_index < total_count else None,
-          'newer_page': page-1 if page != 0 else None } )
+        { 'thoughts': p.object_list,
+          'older_page': p.next_page_number() if p.has_next() else None,
+          'newer_page': p.previous_page_number() if p.has_previous() else None } )