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 += "

%d

" % y for m in cal[y]: sd = datetime(y, m, 1) result += sd.strftime("

%B

") result += '' 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 += '' 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 += "" % (s, i + 1) else: result += "" % (i + 1) w = (w + 1) % 7 if w == 0 and i + 1 < days_in_month: result += "" for i in range(w,7): result += '' result += '
MTWTFSS
%d%d
' return result