Calendar table

 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

  1. Display arbitrary models by bjornkri 5 years, 9 months ago
  2. Simple Django event calendar template tag by sandinmyjoints_ 2 years, 10 months ago
  3. DateTimeWidget using JSCal2 by ramusus 4 years, 9 months ago
  4. Python Calendar wrapper template tag by dokterbob 4 years, 11 months ago
  5. Generate Google Calendar links from django-event-calendar by ElfSternberg 3 years, 6 months ago

Comments

(Forgotten your password?)