PageRenderTime 21ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/blogmaker/blog/templatetags/blog_extras.py

http://blogmaker.googlecode.com/
Python | 151 lines | 134 code | 10 blank | 7 comment | 2 complexity | 49fca3085d3ab656605e65317c90299d MD5 | raw file
  1''' 
  2@copyright:Copyright (c) 2006-2007, PreFab Software Inc.
  3
  4Copyright (c) 2006, Andrew Gwozdziewycz <apgwoz@gmail.com>
  5All rights reserved.
  6'''
  7
  8
  9import datetime
 10
 11from django import template 
 12from django.template import resolve_variable
 13from django.conf import settings
 14
 15from blogmaker.blog.models import Entry, Tag
 16
 17
 18register = template.Library()
 19
 20@register.tag('get_yearly_archive')
 21def do_archive_year(parser, token):
 22    ''' get_yearly_archive as archive_list '''
 23    
 24    bits = token.contents.split()
 25    if len(bits) == 3 and bits[1] == 'as':
 26        return YearArchiveNode(bits[2])
 27    else:
 28        return template.TemplateSyntaxError
 29
 30class YearArchiveNode(template.Node):
 31    def __init__(self, context_variable):
 32        self.context_variable = context_variable
 33
 34    def render(self, context):
 35        date_list = Entry.objects.current_active().dates('pub_date', 'month', order='DESC')
 36        context[self.context_variable] = date_list
 37        return ''
 38
 39
 40@register.tag('get_recent_posts')
 41def do_recent_posts(parser, token):
 42    ''' get_recent_posts 5 as post_list object.id '''
 43    
 44    bits = token.contents.split()
 45    if len(bits) == 5 and bits[2] == 'as':
 46        return RecentPostsNode(bits[3], int(bits[1]), bits[4])
 47    elif len(bits) == 4 and bits[2] == 'as':
 48        return RecentPostsNode(bits[3], int(bits[1]), 0)
 49    else:
 50        return template.TemplateSyntaxError
 51
 52class RecentPostsNode(template.Node):
 53    def __init__(self, context_variable, count, ident):
 54        self.context_variable = context_variable
 55        self.count = count
 56        self.ident = ident
 57    def render(self, context):
 58        if self.ident > 0:
 59            current_ident = resolve_variable(self.ident, context)
 60        else:
 61            current_ident = 0
 62        post_list = Entry.objects.current_active().extra(select={'current_post': "id=%s"}, params=[current_ident]).order_by('-pub_date')[:self.count]
 63        context[self.context_variable] = post_list
 64        return ''
 65
 66
 67@register.tag('get_popular_tags')
 68def do_popular_tags(parser, token):
 69    ''' get_popular_tags 5 as popular_tags '''
 70    
 71    bits = token.contents.split() 
 72    if len(bits) == 4 and bits[2] == 'as':
 73    	return PopularTagsNode(int(bits[1]), bits[3])
 74    
 75class PopularTagsNode(template.Node):
 76    def __init__(self, num, varname):
 77        self.num = num
 78        self.varname = varname
 79    def render(self, context):
 80        tags = Tag.objects.all()
 81        popular_tags = []
 82        for tag in tags:
 83            entry_count = tag.entry_set.current_active().count()
 84            if entry_count > 0:
 85                tag = tag
 86                popular_tags.append( (entry_count, tag) )
 87        popular_tags.sort(reverse=True)
 88        context[self.varname] = popular_tags[:self.num]
 89        return ''
 90
 91
 92@register.tag('get_next')
 93def do_next_post(parser, token):
 94    ''' get_next object.id next_post '''
 95    
 96    bits = token.contents.split()
 97    if len(bits) == 3:
 98        return NextPostNode(bits[1], bits[2])
 99    else:
100        return template.TemplateSyntaxError
101
102class NextPostNode(template.Node):
103    def __init__(self, ident, context_variable):
104        self.context_variable = context_variable
105        self.ident = ident
106    def render(self, context):
107        current_ident = resolve_variable(self.ident, context)
108        current_entry = Entry.objects.get(id__exact=current_ident)
109        try:
110            next_post = current_entry.get_next_by_pub_date(pub_date__lte=datetime.datetime.now(), active=True)
111        except Entry.DoesNotExist:
112            next_post = []
113        context[self.context_variable] = next_post
114        return ''
115
116    
117@register.tag('get_previous')
118def do_previous_post(parser, token):
119    ''' get_previous object.id previous_post '''
120    
121    bits = token.contents.split()
122    if len(bits) == 3:
123        return PreviousPostNode(bits[1], bits[2])
124    else:
125        return template.TemplateSyntaxError
126
127class PreviousPostNode(template.Node):
128    def __init__(self, ident, context_variable):
129        self.context_variable = context_variable
130        self.ident = ident
131    def render(self, context):
132        current_ident = resolve_variable(self.ident, context)
133        current_entry = Entry.objects.get(id__exact=current_ident)
134        try:
135            print 'here'
136            previous_post = current_entry.get_previous_by_pub_date(pub_date__lte=datetime.datetime.now(), active=True)
137        except Entry.DoesNotExist:
138            previous_post = []
139        context[self.context_variable] = previous_post
140        return ''
141        
142@register.simple_tag
143def BLOG_ROOT():
144    return settings.BLOG_ROOT
145        
146@register.simple_tag
147def SITE_ROOT():
148    return settings.SITE_ROOT
149
150
151