Changeset 354
- Timestamp:
- 07/19/10 22:27:24 (7 weeks ago)
- Location:
- Python/blog/trunk
- Files:
-
- 4 edited
-
Changes (modified) (1 diff)
-
blog/models.py (modified) (3 diffs)
-
blog/templates/blog/tags/calendar.html (modified) (2 diffs)
-
blog/templatetags/blog.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
Python/blog/trunk/Changes
r352 r354 5 5 - RPC support for Blogger 1.0, MetaWeblog, and MoveableType (Patch 6 6 provided by Carson Gee.) 7 - Added month_calendar template tag. 7 8 8 9 1.2 (May 3 2010): -
Python/blog/trunk/blog/models.py
r325 r354 1 import calendar 2 import datetime 3 1 4 from django.db import models 2 5 from django.conf import settings … … 7 10 from django.contrib.comments.moderation import CommentModerator, moderator 8 11 9 import datetime10 11 12 DRAFT = 1 12 13 PUBLISHED = 2 … … 14 15 class EntryPublishedManager(Manager): 15 16 """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 17 27 def published(self, **kwargs): 28 # If pub_date__lte is not in kwargs add it. 18 29 return self.get_query_set().filter(status__gte=PUBLISHED, pub_date__lte=datetime.datetime.now(), sites__id__exact=settings.SITE_ID, **kwargs) 19 30 -
Python/blog/trunk/blog/templates/blog/tags/calendar.html
r346 r354 1 1 <table class="cal_month_calendar"> 2 <tr> 3 <th colspan="7"> 4 {{ month|date:"F Y" }} 5 </th> 6 </tr> 2 7 <tr> 3 8 {% for day in headers %} … … 8 13 <tr> 9 14 {% 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> 11 16 {% endfor %} 12 17 </tr> -
Python/blog/trunk/blog/templatetags/blog.py
r352 r354 43 43 44 44 class 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 49 48 self.author = author 50 49 51 50 def render(self, context): 52 author = None 51 year = self.year 52 month = self.month 53 author = self.author 53 54 54 55 if self.author: … … 57 58 except ObjectDoesNotExist: 58 59 pass 59 60 60 61 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) 62 63 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)) 71 103 72 104 class TagList(template.Node): … … 78 110 return '' 79 111 112 @register.tag 80 113 def tag_list_as(parser, token): 81 114 """ … … 100 133 raise TemplateSyntaxError, "'%s' tag requires a single argument: the context name to populate" % bits[0] 101 134 return TagList(bits[1]) 102 tag_list_as = register.tag(tag_list_as) 103 135 136 @register.tag 104 137 def entry_archive(parser, token): 105 138 """ … … 138 171 139 172 return EntryList(bits[1], type, limit, author) 140 entry_archive = register.tag(entry_archive) 141 173 174 @register.tag 142 175 def month_calendar(parser, token): 143 176 """ … … 145 178 to that day in the archive. 146 179 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 147 183 Example:: 148 184 149 185 {% month_calendar <year> <month> [author_ident] %} 186 {% month_calendar <difference> [author_ident] %} 150 187 151 188 {% load blog %} 152 189 153 190 {% 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 158 199 year = datetime.date.today().year 159 200 month = datetime.date.today().month … … 161 202 bits = token.contents.split() 162 203 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: 168 213 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 173 222 if len(bits) == 4: 223 year = bits[1] 224 month = bits[2] 174 225 author = bits[3] 175 226 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 212 237 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.