[PATCH 0 of 2] blog.Post manager cleanups

13 views
Skip to first unread message

Ben Jackson

unread,
Dec 3, 2009, 12:16:07 AM12/3/09
to byteflow-hackers
Per my earlier message, this cleans up usage of Post managers so that
'future' articles are treated like drafts and are only visible to
admins. This removes many duplicate filters from the code as well as
catching more cases (eg tag clouds) that were overlooked before.

The other patch in the series fixes some administrative cases which
needed to use an all-inclusive manager rather than public or site-
specific filters.

--
Ben Jackson AD7GD
<b...@ben.com>
http://www.ben.com/

Ben Jackson

unread,
Dec 3, 2009, 12:16:09 AM12/3/09
to byteflow-hackers
# HG changeset patch
# User Ben Jackson <b...@ben.com>
# Date 1259816722 28800
# Node ID d96cd609dffa38c400fc9c0ae2cd622e5c9277ac
# Parent 3e328d99dbf6b332ecd52a76cb171cf96128e1a6
Centralize 'date__lte now' and 'site=current' filters in PublicPostManager.

Removes all individual 'no future article' filters (some exclude>now,
some filter<=now) and moves the test to PublicPostManager. This also
makes the test more inclusive by keeping it out of places that were
previously overlooked, such as tag clouds.

Also removes a few 'site=current' tests that were already covered by
Post managers.

diff -r 3e328d99dbf6 -r d96cd609dffa apps/blog/managers.py
--- a/apps/blog/managers.py Wed Dec 02 20:44:00 2009 -0800
+++ b/apps/blog/managers.py Wed Dec 02 21:05:22 2009 -0800
@@ -1,5 +1,7 @@
# -*- mode: python; coding: utf-8; -*-

+from datetime import datetime as dt
+
from django.db import models
from django.contrib.sites.models import Site

@@ -47,5 +49,5 @@
class PublicPostManager(SitePostManager):
def get_query_set(self):
qs = super(PublicPostManager, self).get_query_set()
- qs = qs.filter(is_draft=False)
+ qs = qs.filter(date__lte=dt.now(), is_draft=False)
return qs
diff -r 3e328d99dbf6 -r d96cd609dffa apps/blog/sitemaps.py
--- a/apps/blog/sitemaps.py Wed Dec 02 20:44:00 2009 -0800
+++ b/apps/blog/sitemaps.py Wed Dec 02 21:05:22 2009 -0800
@@ -14,7 +14,7 @@
priority = 0.8

def items(self):
- return Post.objects.exclude(date__gt=dt.now())
+ return Post.objects.all()

def lastmod(self, obj):
return obj.date
@@ -32,7 +32,7 @@

def lastmod(self, obj):
if obj == "post_list":
- return Post.objects.exclude(date__gt=dt.now()).latest('date').date
+ return Post.objects.all().latest('date').date


class BlogTagsSitemap(Sitemap):
@@ -40,11 +40,11 @@
priority = 0.5

def items(self):
- return Tag.objects.usage_for_queryset(Post.objects.exclude(date__gt=dt.now()))
+ return Tag.objects.usage_for_queryset(Post.objects.all())

def location(self, obj):
return reverse('post_by_tag', tag=obj.name)

def lastmod(self, obj):
- return TaggedItem.objects.get_by_model(Post.objects.exclude(
- date__gt=dt.now()), [obj.name]).latest('date').date
+ return TaggedItem.objects.get_by_model(Post.objects.all(),
+ [obj.name]).latest('date').date
diff -r 3e328d99dbf6 -r d96cd609dffa apps/blog/templatetags/sidebar.py
--- a/apps/blog/templatetags/sidebar.py Wed Dec 02 20:44:00 2009 -0800
+++ b/apps/blog/templatetags/sidebar.py Wed Dec 02 21:05:22 2009 -0800
@@ -13,7 +13,7 @@

@register.inclusion_tag('blog/sidebar.html', takes_context=True)
def sidebar(context):
- all_posts = Post.objects.filter(date__lte=dt.now()).order_by('-date')
+ all_posts = Post.objects.order_by('-date')
comments = CommentNode.objects.for_similar_objects(all_posts)
if comments:
comments = comments.select_related().order_by('-pub_date')[:5]
diff -r 3e328d99dbf6 -r d96cd609dffa apps/blog/views.py
--- a/apps/blog/views.py Wed Dec 02 20:44:00 2009 -0800
+++ b/apps/blog/views.py Wed Dec 02 21:05:22 2009 -0800
@@ -35,7 +35,7 @@

def post_list(request, *args, **kwargs):
"""Post listing. Only shows posts that are older than now()"""
- kwargs['queryset'] = Post.objects.exclude(date__gt=dt.now())
+ kwargs['queryset'] = Post.objects.all()
return object_list(request, *args, **kwargs)


@@ -47,7 +47,7 @@
def archive_month(request, year, month):
if int(year) < 1901: # to exclude all troubles with strftime
raise Http404
- qs = Post.objects.filter(site=Site.objects.get_current())
+ qs = Post.objects.all()
return date_based.archive_month(request, year, month, queryset=qs,
month_format='%m',
template_name='blog/post_list.html',
@@ -57,7 +57,7 @@
def archive_year(request, year):
if int(year) < 1901:
raise Http404
- qs = Post.objects.filter(site=Site.objects.get_current())
+ qs = Post.objects.all()
return date_based.archive_year(request, year, make_object_list=True,
queryset=qs,
template_name='blog/post_archive_year.html',
@@ -67,7 +67,7 @@
def archive_day(request, year, month, day):
if int(year) < 1901:
raise Http404
- qs = Post.objects.filter(site=Site.objects.get_current())
+ qs = Post.objects.all()
return date_based.archive_day(request, year, month, day, month_format='%m',
template_name='blog/post_archive_year.html',
queryset=qs, **archive_dict)
@@ -75,7 +75,7 @@

def by_tag(request, tag, *args, **kwargs):
"""Post listing. Only shows posts that belong to specified tags"""
- queryset = Post.objects.filter(date__lte=dt.now())
+ queryset = Post.objects.all()
if not kwargs.has_key('extra_context'):
kwargs['extra_context'] = {}
kwargs['extra_context']['feedurl'] = 'tag/%s' % tag
@@ -94,7 +94,7 @@
user = User.objects.get(username__exact=author)
except User.DoesNotExist:
raise Http404
- queryset = Post.objects.filter(date__lte=dt.now(), author=user)
+ queryset = Post.objects.filter(author=user)
if not kwargs.has_key('extra_context'):
kwargs['extra_context'] = {}
kwargs['extra_context']['feedurl'] = 'author/%s' % user
@@ -111,9 +111,10 @@
'date__year': year,
'date__month': month,
'date__day': day}
- if not request.user.is_staff:
- filtr['is_draft'] = False
- post = get_object_or_404(Post.all_objects, **filtr)
+ qs = (request.user.is_staff
+ and Post.all_objects
+ or Post.objects)
+ post = get_object_or_404(qs, **filtr)
if post.comments_open():
Form = (request.user.is_authenticated()
and CommentForm
diff -r 3e328d99dbf6 -r d96cd609dffa apps/feed/blog_feeds.py
--- a/apps/feed/blog_feeds.py Wed Dec 02 20:44:00 2009 -0800
+++ b/apps/feed/blog_feeds.py Wed Dec 02 21:05:22 2009 -0800
@@ -49,7 +49,7 @@
{'rel': u'self', 'href': link(reverse(get_feed_type(type), 'blog'))})

def items(self):
- return Post.objects.exclude(date__gt=dt.now()).order_by('-date')[:5]
+ return Post.objects.order_by('-date')[:5]

def item_id(self, item):
return link(item.get_absolute_url())
@@ -109,7 +109,7 @@
'author/%s' % author.username))})

def items(self, author):
- return Post.objects.filter(date__lt=dt.now(), author=author).order_by('-date')[:5]
+ return Post.objects.filter(author=author).order_by('-date')[:5]

def item_id(self, item):
return link(item.get_absolute_url())
@@ -329,11 +329,11 @@
return Post.featured_objects.order_by('-date')[0].date

def items(self):
- return Post.featured_objects.exclude(date__gt=dt.now()).order_by('-date')[:5]
+ return Post.featured_objects.order_by('-date')[:5]

class RssFeaturedBlogEntries(RssBlogEntries):
def feed_updated(self):
return Post.featured_objects.order_by('-date')[0].date

def items(self):
- return Post.featured_objects.exclude(date__gt=dt.now()).order_by('-date')[:5]
+ return Post.featured_objects.order_by('-date')[:5]

Yuri Baburov

unread,
Dec 3, 2009, 6:07:04 AM12/3/09
to byteflow...@googlegroups.com
Hi Ben,

On Thu, Dec 3, 2009 at 11:16 AM, Ben Jackson <b...@ben.com> wrote:
> -  qs = Post.objects.filter(site=Site.objects.get_current())
> +    qs = Post.objects.all()

Sorry?


--
Best regards, Yuri V. Baburov, ICQ# 99934676, Skype: yuri.baburov,
MSN: bu...@live.com

Yuri Baburov

unread,
Dec 3, 2009, 7:48:23 AM12/3/09
to byteflow...@googlegroups.com
ok, got it. Just blog.Post is becoming too complex.
4 different managers with weird class and instance names,
some unknown one used for admin... (first declared AFAIK?, "whole_objects" one?)

Alexander Solovyov

unread,
Dec 3, 2009, 8:33:45 AM12/3/09
to byteflow...@googlegroups.com
On 2009-12-03, Ben Jackson wrote:

> Per my earlier message, this cleans up usage of Post managers so that
> 'future' articles are treated like drafts and are only visible to
> admins. This removes many duplicate filters from the code as well as
> catching more cases (eg tag clouds) that were overlooked before.

Cool patches! All four are applied and pushed to main repo.

--
Alexander

Ben Jackson

unread,
Dec 3, 2009, 1:32:44 PM12/3/09
to byteflow...@googlegroups.com
On Thu, Dec 03, 2009 at 05:07:04PM +0600, Yuri Baburov wrote:
>
> Hi Ben,
>
> On Thu, Dec 3, 2009 at 11:16 AM, Ben Jackson <b...@ben.com> wrote:
> > - ?qs = Post.objects.filter(site=Site.objects.get_current())
> > + ? ?qs = Post.objects.all()
>
> Sorry?

in blog.models:

class Post(models.Model):
...
whole_objects = PostManager()
all_objects = SitePostManager()
objects = PublicPostManager() <<<
plain_manager = models.Manager()

in blog.managers:

class SitePostManager(PostManager):
def get_query_set(self):
qs = super(SitePostManager, self).get_query_set()
qs = qs.filter(site=Site.objects.get_current()) <<<
return qs

class PublicPostManager(SitePostManager): <<<
def get_query_set(self):
qs = super(PublicPostManager, self).get_query_set()

qs = qs.filter(date__lte=dt.now(), is_draft=False)
return qs

Reply all
Reply to author
Forward
0 new messages