Custom Search

Friday, March 26, 2010

how to django calendar using template tag

django calendar using template tag
current date highlighted in the calendar
python calendar

------------------------- in template

{% milestone_calendar value late_milestones milestones %}
{{ value }}

------------------------- in css
.calendar {
font-size: 10px;
}

.calendar th {
color: #555555;
background: #DCDCDC;
border-bottom: 1px solid #CCCCCC;
}

.calendar td.today {
background: #99CCFF;
}

.calendar td.weekend {
color: #C3BEBE;
}

.calendar td.late {
background: #339933;
}

.calendar td.future {
background: #FFCC00;
}

.calendar td.future_today {
background: #FFCC00;
color: #6699CC;
}
------------------------- template tag

class MilestoneCalendarNode(template.Node):
def __init__(self ,tag_name, value, late_milestones, upcoming_milestones):
self.late_milestones = late_milestones
self.upcoming_milestones = upcoming_milestones
self.value = value
self.tag_name = tag_name

def render(self, context):
late_milestones = template.resolve_variable(self.late_milestones, context)
upcoming_milestones = template.resolve_variable(self.upcoming_milestones, context)

current_year = datetime.datetime.now().year
current_day = datetime.datetime.now().day
current_month = datetime.datetime.now().month
current_month_name = datetime.datetime.now().strftime("%B")

current_month_late_days = []
current_month_upcoming_days = []

for l in late_milestones:
due_date_month = l.due_date.month
due_date_year= l.due_date.year
if due_date_year == current_year and due_date_month == current_month:
current_month_late_days.append(l.due_date.day)

#print "ldayy", current_month_late_days

for u in upcoming_milestones:
due_date_month = u.due_date.month
due_date_year= u.due_date.year
if due_date_year == current_year and due_date_month == current_month:
current_month_upcoming_days.append(u.due_date.day)

#print "udayy", current_month_upcoming_days

days = calendar.monthcalendar(current_year,current_month)
table_start = "<table class='calendar'><tr><th>"+ current_month_name +":</th><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr>"
table_end = "</table>"
for d in days:
table_start = table_start + "<tr><td></td>"
for x in d:
cls = ""
#print "index", d.index(x)
if x == current_day:
cls = "class=today"
elif d.index(x) == 0 or d.index(x) == 6:
cls = "class=weekend"
if x == 0:
x = ''
if x in current_month_late_days:
cls = "class=late"
if x in current_month_upcoming_days:
print "cccc1", cls
if cls:
cls = "class=future_today"
else:
cls = "class=future"
print "cccc2", cls

table_start = table_start + "<td " + cls + ">" + str(x) + "</td>"
table_start = table_start+"</tr>"
table = table_start + table_end

context[self.value] = mark_safe(table)
return ''


@register.tag
def milestone_calendar(parser, token):
try:
tag_name, value, late_milestones, upcoming_milestones = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError
return MilestoneCalendarNode(tag_name, value, late_milestones, upcoming_milestones)

------------------------------

No comments:

Post a Comment