Commits

Dan Watson  committed 531c35f

Fix search pagination, comment counts, photo stream

  • Participants
  • Parent commits b184cb5

Comments (0)

Files changed (7)

File leaves/models.py

 		return self._meta_cache
 	meta = property(get_meta)
 
+	def get_published_comments(self):
+		return self.comments.filter(status='published')
+	published_comments = property(get_published_comments)
+
 class LeafMeta (models.Model):
 	leaf = models.ForeignKey(Leaf, related_name='metadata')
 	key = models.CharField(max_length=50)

File leaves/plugins/photos/views.py

 from django.shortcuts import get_object_or_404
 from django.template.response import TemplateResponse
 from leaves.plugins.photos.models import Photo, Gallery
+from leaves.utils import get_page
 from PIL import Image
 import os
 
 		return HttpResponse('')
 
 def photo_stream(request):
-	num = request.site.preferences.stream_count
-	photos = Photo.on_site.active()[:num]
 	return TemplateResponse(request, 'photos/stream.html', {
-		'leaves': photos,
+		'page': get_page(request, Photo.on_site.active()),
 	})
 
 def galleries(request):
-	num = request.site.preferences.stream_count
-	gals = Gallery.on_site.active()[:num]
 	return TemplateResponse(request, 'photos/galleries.html', {
-		'leaves': gals,
+		'page': get_page(request, Gallery.on_site.active()),
 	})

File leaves/themes/stream/templates/index.html

     <div class="leaf-stream">There are no leaves to display.</div>
 {% endfor %}
 </div>
+{% block pagination %}
 {% if page.has_other_pages %}
 <div class="pagination">
     {% if page.has_previous %}
 </div>
 {% endif %}
 {% endblock %}
+{% endblock %}

File leaves/themes/stream/templates/leaf_footer.html

 {% load leaf %}
-{% with tag_count=leaf.tags.count comment_count=leaf.comments.count %}
+{% with tag_count=leaf.tags.count comment_count=leaf.published_comments.count %}
 <div class="leaf-footer {{ leaf.subclass }}-footer {{ selector }}">
     <div class="section first">
         <span class="label">posted</span>

File leaves/themes/stream/templates/search.html

 {% extends "index.html" %}
 {% block title %}{{ site.name }} - Search results for "{{ q }}"{% endblock %}
 {% block content-header %}<h1 class="tag">Search results for "{{ q }}"</h1>{% endblock %}
+
+{% block pagination %}
+{% if page.has_other_pages %}
+<div class="pagination">
+    {% if page.has_previous %}
+    <a class="previous" href="?q={{ q }}&page={{ page.previous_page_number }}">&laquo; Newer</a>
+    {% endif %}
+    {% if page.has_next %}
+    <a class="next" href="?q={{ q }}&page={{ page.next_page_number }}">Older &raquo;</a>
+    {% endif %}
+    <div style="clear:both;"></div>
+</div>
+{% endif %}
+{% endblock %}

File leaves/utils.py

 from django.conf import settings
 from django.contrib.sites.models import Site
-from django.core.paginator import Paginator, InvalidPage, EmptyPage
+from django.core.paginator import Paginator, InvalidPage
 from django.db import models
 from django.utils.encoding import force_unicode
 from django.utils.safestring import mark_safe
 		models.Q(**{'status': 'published'}),
 	)
 
-def get_page(request, queryset):
+def get_page(request, queryset, per_page=None):
 	"""
 	Returns a django.core.pagination.Page object based on the page number
 	in the request, the stream_count preference for the site, and the specified
 	queryset.
 	"""
-	paginator = Paginator(queryset, request.site.preferences.stream_count)
+	if per_page is None:
+		per_page = request.site.preferences.stream_count
+	paginator = Paginator(queryset, per_page)
 	try:
 		pagenum = int(request.GET.get('page', '1'))
 		if pagenum < 1:
 		pagenum = 1
 	try:
 		page = paginator.page(pagenum)
-	except (EmptyPage, InvalidPage):
+	except InvalidPage:
 		page = paginator.page(paginator.num_pages)
 	return page
 

File leaves/views.py

 		leaves = leaves.filter(q)
 	return TemplateResponse(request, 'search.html', {
 		'q': request.GET.get('q', '').strip(),
-		'page': get_page(request, leaves),
+		'page': get_page(request, leaves, per_page=10),
 	})
 
 def view_tag(request, slug):