X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/a66152cd03df94d103944255b836ed6ebed6ed15..aa974af0f752e8b3e23337764e0bbd5e140217d8:/contrib/penfield/models.py?ds=sidebyside diff --git a/contrib/penfield/models.py b/contrib/penfield/models.py index c4cb49e..94ad5c3 100644 --- a/contrib/penfield/models.py +++ b/contrib/penfield/models.py @@ -3,6 +3,7 @@ from django.conf import settings from philo.models import Tag, Titled, Entity, MultiView, Page, register_value_model from philo.exceptions import ViewCanNotProvideSubpath from django.conf.urls.defaults import url, patterns +from django.core.paginator import EmptyPage from django.core.urlresolvers import reverse from django.http import Http404, HttpResponse from datetime import datetime @@ -129,13 +130,14 @@ class BlogView(MultiView): ) return base_patterns + entry_patterns + def paginate(self, objects, page_number): + try: + return paginate(objects, self.entries_per_page, page_number) + except EmptyPage: + raise Http404 + def index_view(self, request, node=None, extra_context=None): - entries = self.blog.entries.all() - if self.entries_per_page: - paginated_page = paginate(request, entries, self.entries_per_page) - entries = paginated_page.object_list - else: - paginated_page = None + paginated_page, entries = self.paginate(self.blog.entries.all(), request.GET.get('page', 1)) context = {} context.update(extra_context or {}) context.update({'blog': self.blog, 'entries': entries, 'paginated_page': paginated_page}) @@ -168,11 +170,8 @@ class BlogView(MultiView): entries = entries.filter(date__month=month) if day: entries = entries.filter(date__day=day) - if self.entries_per_page: - paginated_page = paginate(request, entries, self.entries_per_page) - entries = paginated_page.object_list - else: - paginated_page = None + + paginated_page, entries = self.paginate(entries, request.GET.get('page', 1)) context = {} context.update(extra_context or {}) context.update({'blog': self.blog, 'year': year, 'month': month, 'day': day, 'entries': entries, 'paginated_page': paginated_page}) @@ -196,11 +195,7 @@ class BlogView(MultiView): if entries.count() <= 0: raise Http404 - if self.entries_per_page: - paginated_page = paginate(request, entries, self.entries_per_page) - entries = paginated_page.object_list - else: - paginated_page = None + paginated_page, entries = self.paginate(entries, request.GET.get('page', 1)) context = {} context.update(extra_context or {}) context.update({'blog': self.blog, 'tags': tags, 'entries': entries, 'paginated_page': paginated_page}) @@ -230,6 +225,7 @@ class NewsletterArticle(Entity, Titled): full_text = models.TextField() class Meta: + get_latest_by = 'date' ordering = ['-date'] unique_together = (('newsletter', 'slug'),) @@ -240,7 +236,7 @@ register_value_model(NewsletterArticle) class NewsletterIssue(Entity, Titled): newsletter = models.ForeignKey(Newsletter, related_name='issues') number = models.PositiveIntegerField() - articles = models.ManyToManyField(NewsletterArticle) + articles = models.ManyToManyField(NewsletterArticle, related_name='issues') class Meta: ordering = ['-number']