Creates a filter for displaying calendars.
Passed value is a dict of dicts of arrays: that is, indexed by year, then by month. The list of month days is used to generate links in the format specified by the argument. If a particular day is not in the list, then no link will be generated and it will just display a number for that day.
EXAMPLE
{{ calendar|calendar_table:"/schedules/calendar/[Y]-[m]-[d]/" }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | from django import template
from datetime import datetime
register = template.Library()
@register.filter(name='calendar_table')
def calendar_table(value, arg):
cal = {}
dates = value.keys()
dates.sort()
for date in value:
d, m, y = date.day, date.month, date.year
if y not in cal:
cal[y] = {}
if m not in cal[y]:
cal[y][m] = []
cal[y][m].append(d)
result = ''
for y in cal:
result += "<h2 style=\"clear: left\">%d</h2>" % y
for m in cal[y]:
sd = datetime(y, m, 1)
result += sd.strftime("<div class=\"month\"><h3>%B</h3>")
result += '<table><thead><tr><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th><th>S</th></tr></thead><tbody><tr>'
days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][m]
if m == 2 and y % 4 == 0 and (y % 100 <> 0 or y % 400 == 0):
days_in_month += 1
w = sd.weekday()
for i in range(w):
result += '<td></td>'
for i in range(days_in_month):
if i in cal[y][m]:
s = arg.replace('[Y]', "%.4d" % y).replace('[m]', "%.2d" % m).replace('[d]', "%.2d" % d)
result += "<td><a href=\"%s\">%d</a></td>" % (s, i + 1)
else:
result += "<td>%d</td>" % (i + 1)
w = (w + 1) % 7
if w == 0 and i + 1 < days_in_month:
result += "</tr><tr>"
for i in range(w,7):
result += '<td></td>'
result += '</tr></tbody></table></div>'
return result
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 1 year ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 1 month ago
- Serializer factory with Django Rest Framework by julio 1 year, 7 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 8 months ago
- Help text hyperlinks by sa2812 1 year, 9 months ago
Comments
Please login first before commenting.