Created
December 10, 2009 14:33
-
-
Save uggedal/253357 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from calendar import LocaleHTMLCalendar | |
| from datetime import date | |
| from itertools import groupby | |
| from django.utils.html import conditional_escape as esc | |
| class WorkoutCalendar(LocaleHTMLCalendar): | |
| def __init__(self, workouts, locale): | |
| super(WorkoutCalendar, self).__init__(locale=locale) | |
| self.workouts = self.group_by_day(workouts) | |
| def formatday(self, day, weekday): | |
| if day != 0: | |
| cssclass = self.cssclasses[weekday] | |
| if date.today() == date(self.year, self.month, day): | |
| cssclass += ' today' | |
| if day in self.workouts: | |
| cssclass += ' filled' | |
| body = ['<ul>'] | |
| for workout in self.workouts[day]: | |
| body.append('<li>') | |
| body.append('<a href="%s">' % workout.get_absolute_url()) | |
| body.append(esc(workout.title)) | |
| body.append('</a></li>') | |
| body.append('</ul>') | |
| return self.day_cell(cssclass, '%d %s' % (day, ''.join(body))) | |
| return self.day_cell(cssclass, day) | |
| return self.day_cell('noday', ' ') | |
| def formatmonth(self, year, month): | |
| self.year, self.month = year, month | |
| return super(WorkoutCalendar, self).formatmonth(year, month) | |
| def group_by_day(self, workouts): | |
| field = lambda workout: workout.performed_at.day | |
| return dict( | |
| [(day, list(items)) for day, items in groupby(workouts, field)] | |
| ) | |
| def day_cell(self, cssclass, body): | |
| return '<td class="%s">%s</td>' % (cssclass, body) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.shortcuts import render_to_response | |
| from django.utils.safestring import mark_safe | |
| def calendar(request, year, month): | |
| my_workouts = Workouts.objects.order_by('my_date').filter( | |
| my_date__year=year, my_date__month=month | |
| ) | |
| locale = 'de_DE' # change this to your locale string | |
| cal = WorkoutCalendar(my_workouts, locale).formatmonth(year, month) | |
| return render_to_response('my_template.html', {'calendar': mark_safe(cal),}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello there, are you still having somewhere this project ? I would be really grateful to see models and your template. Thanks!