NicholasStudt

Changeset 354


Ignore:
Timestamp:
07/19/10 22:27:24 (7 weeks ago)
Author:
nicholas
Message:

Complete month_calendar template tag.

Location:
Python/blog/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • Python/blog/trunk/Changes

    r352 r354  
    55 - RPC support for Blogger 1.0, MetaWeblog, and MoveableType (Patch 
    66   provided by Carson Gee.) 
     7 - Added month_calendar template tag. 
    78  
    891.2 (May  3 2010): 
  • Python/blog/trunk/blog/models.py

    r325 r354  
     1import calendar 
     2import datetime 
     3 
    14from django.db import models 
    25from django.conf import settings 
     
    710from django.contrib.comments.moderation import CommentModerator, moderator 
    811 
    9 import datetime 
    10  
    1112DRAFT = 1 
    1213PUBLISHED = 2 
     
    1415class EntryPublishedManager(Manager): 
    1516    """Returns published posts that are not in the future."""  
    16      
     17    def published_bymonth(self, year, month, **kwargs): 
     18        "Get all items published in a given month" 
     19        gte = datetime.date(year, month, 1) 
     20        lte = datetime.date(year, month, calendar.monthrange(year, month)[1]) 
     21 
     22        if lte >= datetime.date.today(): # Dont' show the future. 
     23            lte = datetime.datetime.now() 
     24 
     25        return self.get_query_set().filter(status__gte=PUBLISHED, pub_date__gte=gte, pub_date__lte=lte, sites__id__exact=settings.SITE_ID, **kwargs) 
     26 
    1727    def published(self, **kwargs): 
     28        # If pub_date__lte is not in kwargs add it. 
    1829        return self.get_query_set().filter(status__gte=PUBLISHED, pub_date__lte=datetime.datetime.now(), sites__id__exact=settings.SITE_ID, **kwargs) 
    1930 
  • Python/blog/trunk/blog/templates/blog/tags/calendar.html

    r346 r354  
    11<table class="cal_month_calendar"> 
     2<tr> 
     3    <th colspan="7">     
     4                {{ month|date:"F Y" }} 
     5    </th> 
     6</tr> 
    27<tr> 
    38{% for day in headers %} 
     
    813<tr> 
    914{% for day in week %} 
    10     <td{% if not day.in_month %} class="cal_not_in_month"{% endif %}>{% if day.event %}<a href="/calendar/{{ day.day|date:"Y/m" }}/">{{ day.day|date:"j" }}</a>{% else %}{{ day.day|date:"j" }}{% endif %}</td> 
     15    <td{% if not day.in_month %} class="cal_not_in_month"{% endif %}>{% if day.event %}<a href="{% url archive_day day.day|date:"Y" day.day|date:"m" day.day|date:"d" %}">{{ day.day|date:"j" }}</a>{% else %}{{ day.day|date:"j" }}{% endif %}</td> 
    1116{% endfor %} 
    1217</tr> 
  • Python/blog/trunk/blog/templatetags/blog.py

    r352 r354  
    4343 
    4444class Calendar(template.Node): 
    45     def __init__(self, var_name, kind='month', limit=None, author=None): 
    46         self.var_name = var_name 
    47         self.kind = kind 
    48         self.limit = limit  
     45    def __init__(self, year, month, author=None): 
     46        self.year = year  
     47        self.month = month  
    4948        self.author = author  
    5049 
    5150    def render(self, context): 
    52         author = None 
     51        year = self.year 
     52        month = self.month 
     53        author = self.author 
    5354 
    5455        if self.author: 
     
    5758            except ObjectDoesNotExist:  
    5859                pass 
    59  
     60            
    6061        if author: 
    61             list = Entry.objects.published(author=author).dates('pub_date', self.kind, order='DESC') 
     62            event_list = Entry.objects.published_bymonth(year, month, author=author) 
    6263        else:  
    63             list = Entry.objects.published().dates('pub_date', self.kind,  
    64                                                order='DESC') 
    65  
    66         if self.limit: 
    67             context[self.var_name] = list[:self.limit] 
    68         else: 
    69             context[self.var_name] = list 
    70         return '' 
     64            event_list = Entry.objects.published_bymonth(year, month) 
     65 
     66        # Fix this to just use calendar.* for all math. 
     67 
     68        start = datetime.date(year, month, 1) 
     69        end = datetime.date(year, month, calendar.monthrange(year, month)[1]) 
     70 
     71        first_day_of_calendar = start - datetime.timedelta(start.weekday())  
     72        last_day_of_calendar = end + datetime.timedelta(7 - end.weekday()) 
     73 
     74        month_cal = [] 
     75        week = [] 
     76        week_headers = [] 
     77     
     78        i = 0 
     79        day = first_day_of_calendar 
     80        while day <= last_day_of_calendar: 
     81            if i < 7: 
     82                week_headers.append(day) 
     83            cal_day = {} 
     84            cal_day['day'] = day 
     85            cal_day['event'] = False 
     86            for event in event_list: 
     87                if day >= event.pub_date.date() and day <= event.pub_date.date(): 
     88                    cal_day['event'] = True 
     89            if day.month == month: 
     90                cal_day['in_month'] = True 
     91            else: 
     92                cal_day['in_month'] = False   
     93            week.append(cal_day) 
     94            if day.weekday() == 6: 
     95                month_cal.append(week) 
     96                week = [] 
     97            i += 1 
     98            day += datetime.timedelta(1) 
     99     
     100        t = template.loader.get_template('blog/tags/calendar.html') 
     101 
     102        return t.render(template.Context({'month': start, 'calendar': month_cal, 'headers': week_headers}, autoescape=context.autoescape)) 
    71103 
    72104class TagList(template.Node): 
     
    78110        return '' 
    79111 
     112@register.tag 
    80113def tag_list_as(parser, token): 
    81114    """ 
     
    100133        raise TemplateSyntaxError, "'%s' tag requires a single argument: the context name to populate" % bits[0] 
    101134    return TagList(bits[1]) 
    102 tag_list_as = register.tag(tag_list_as) 
    103  
     135 
     136@register.tag 
    104137def entry_archive(parser, token): 
    105138    """ 
     
    138171 
    139172    return EntryList(bits[1], type, limit, author) 
    140 entry_archive = register.tag(entry_archive) 
    141  
     173 
     174@register.tag 
    142175def month_calendar(parser, token): 
    143176    """ 
     
    145178    to that day in the archive. 
    146179 
     180    If provided a negative difference it will render the month in the 
     181    past. -1 will render last month, -2 will render two months ago, etc.  
     182 
    147183    Example:: 
    148184 
    149185        {% month_calendar <year> <month> [author_ident] %} 
     186        {% month_calendar <difference> [author_ident] %} 
    150187         
    151188        {% load blog %} 
    152189 
    153190        {% month_calendar 2010 11 author_ident_here %}  
    154  
    155     """ 
    156      
    157     author = None 
     191        {% month_calendar 2010 11 %}  
     192         
     193        {% month_calendar -1 author_ident_here %}  
     194        {% month_calendar -1 %}  
     195 
     196    """ 
     197    
     198    author = difference = None 
    158199    year = datetime.date.today().year 
    159200    month = datetime.date.today().month 
     
    161202    bits = token.contents.split() 
    162203 
    163     if len(bits) < 2: 
    164         raise TemplateSyntaxError, "'%s' tag requires at least the context name to populate" % bits[0] 
    165      
    166     if len(bits) >= 3: 
    167         (limit, type) = bits[2].split(':')  
     204    # AUTHOR -OR- DIFFERENCE 
     205    if len(bits) == 2: 
     206        try: 
     207            difference = int(bits[1]) 
     208        except ValueError: 
     209            author = bits[1] 
     210 
     211    # YYYY MM  -OR- DIFFERENCE AUTHOR 
     212    if len(bits) == 3: 
    168213        try:  
    169             limit = int(limit) 
    170         except ValueError:  
    171             limit = None 
    172  
     214            year = int(bits[1]) 
     215            month = int(bits[2]) 
     216        except ValueError: 
     217            difference = bits[1] 
     218            author = bits[2] 
     219            
     220 
     221    # YYYY MM AUTHOR 
    173222    if len(bits) == 4: 
     223        year = bits[1] 
     224        month = bits[2] 
    174225        author = bits[3] 
    175226 
    176     return Calendar(bits[1], type, limit, author) 
    177 entry_archive = register.tag(entry_archive) 
    178  
    179 def month_cal(parser, token): 
    180     year=datetime.date.today().year 
    181     month=datetime.date.today().month 
    182  
    183     # Fix this to just use calendar.* for all math. 
    184  
    185     first_day_of_month = datetime.date(year, month, 1) 
    186     last_day_of_month = calendar.monthrange(year, month) 
    187     first_day_of_calendar = first_day_of_month - datetime.timedelta(first_day_of_month.weekday()) 
    188  
    189     last_day_of_calendar = datetime.date(year,month,last_day_of_month[1]) + datetime.timedelta(7 - calendar.weekday(year,month,last_day_of_month[1])) 
    190  
    191     return last_day_of_calendar 
    192  
    193     event_list = Entry.objects.published(pub_date__gte=first_day_of_calendar, pub_date__lte=last_day_of_calendar) 
    194  
    195     month_cal = [] 
    196     week = [] 
    197     week_headers = [] 
    198  
    199     i = 0 
    200     day = first_day_of_calendar 
    201     while day <= last_day_of_calendar: 
    202         if i < 7: 
    203             week_headers.append(day) 
    204         cal_day = {} 
    205         cal_day['day'] = day 
    206         cal_day['event'] = False 
    207         for event in event_list: 
    208             if day >= event.start_date.date() and day <= event.end_date.date(): 
    209                 cal_day['event'] = True 
    210         if day.month == month: 
    211             cal_day['in_month'] = True 
     227    if difference: 
     228        today = datetime.date.today() 
     229        year = today.year 
     230        month = today.month 
     231 
     232        if difference > 0: 
     233            month += difference 
     234            while month > 12: 
     235                month -= 12 
     236                year += 1 
    212237        else: 
    213             cal_day['in_month'] = False   
    214         week.append(cal_day) 
    215         if day.weekday() == 6: 
    216             month_cal.append(week) 
    217             week = [] 
    218         i += 1 
    219         day += datetime.timedelta(1) 
    220  
    221     return {'calendar': month_cal, 'headers': week_headers} 
    222  
    223 register.inclusion_tag('blog/tags/calendar.html')(month_cal) 
     238            month += difference 
     239            while month < 0: 
     240                month += 12 
     241                year -= 1 
     242 
     243    try:  
     244        year = int(year) 
     245    except ValueError:  
     246        year = datetime.date.today().year 
     247     
     248    try:  
     249        month = int(month) 
     250    except ValueError:  
     251        month = datetime.date.today().month 
     252 
     253    return Calendar(year, month, author) 
Note: See TracChangeset for help on using the changeset viewer.