author | Tero Marttila <terom@fixme.fi> |
Sun, 09 Jan 2011 00:09:40 +0200 | |
changeset 42 | fa5694ee0f98 |
parent 41 | 36d029a47d37 |
child 46 | 547940cb0e1c |
permissions | -rw-r--r-- |
37 | 1 |
""" |
2 |
Calendar view of orders |
|
3 |
""" |
|
4 |
||
5 |
from svv.controllers import PageHandler |
|
6 |
from svv.html import tags |
|
7 |
from svv import database as db |
|
8 |
from svv import html |
|
9 |
||
10 |
||
11 |
import datetime |
|
12 |
import calendar |
|
13 |
import logging |
|
14 |
||
15 |
log = logging.getLogger('svv.cal') |
|
16 |
||
17 |
class CalendarView (PageHandler) : |
|
18 |
""" |
|
19 |
Single-month calendar view with events for given month shown |
|
20 |
""" |
|
21 |
||
22 |
# first date of week |
|
41 | 23 |
FIRST_WEEKDAY = 0 |
37 | 24 |
|
25 |
# year/month format for URLs |
|
26 |
URL_FORMAT = "%Y-%m" |
|
27 |
||
28 |
# formatting styles |
|
29 |
MONTH_TITLE_FORMAT = "%B %Y" |
|
30 |
||
31 |
@classmethod |
|
32 |
def dayofweek_title (cls, dow) : |
|
33 |
""" |
|
34 |
Return day of week name for given dow number |
|
35 |
""" |
|
36 |
||
37 |
return calendar.day_name[dow] |
|
38 |
||
39 |
@classmethod |
|
40 |
def _wrap_year (cls, year, month) : |
|
41 |
""" |
|
42 |
Wraps month to between [1, 12], spilling overflow/underflow by to year. |
|
43 |
||
44 |
Returns (year, month) |
|
45 |
""" |
|
46 |
||
47 |
# underflow? |
|
48 |
if month == 0 : |
|
49 |
# wrap to previous year |
|
50 |
return (year - 1, 12) |
|
51 |
||
52 |
# overflow? |
|
53 |
elif month == 13 : |
|
54 |
# wrap to next year |
|
55 |
return (year + 1, 1) |
|
56 |
||
57 |
# sane value |
|
58 |
elif 1 <= month <= 12 : |
|
59 |
return (year, month) |
|
60 |
||
61 |
# insane value |
|
62 |
else : |
|
63 |
assert False, "invalid year/month: %d/%d" % (year, month) |
|
64 |
||
65 |
@classmethod |
|
66 |
def prev_month (cls, month) : |
|
67 |
""" |
|
68 |
Compute date for previous month. |
|
69 |
""" |
|
70 |
||
71 |
# normalize |
|
72 |
y, m = cls._wrap_year(month.year, month.month - 1) |
|
73 |
||
74 |
return datetime.date(y, m, 1) |
|
75 |
||
76 |
@classmethod |
|
77 |
def next_month (cls, month) : |
|
78 |
""" |
|
79 |
Compute date for following month. |
|
80 |
""" |
|
81 |
||
82 |
# normalize |
|
83 |
y, m = cls._wrap_year(month.year, month.month + 1) |
|
84 |
||
85 |
return datetime.date(y, m, 1) |
|
86 |
||
87 |
||
88 |
def process (self, **foo) : |
|
89 |
""" |
|
90 |
Setup |
|
91 |
""" |
|
92 |
||
93 |
# db session |
|
94 |
self.session = self.app.session() |
|
95 |
||
96 |
def render_day_header (self, month, date) : |
|
97 |
""" |
|
98 |
Render <th> for day |
|
99 |
""" |
|
100 |
||
101 |
today = datetime.date.today() |
|
102 |
||
103 |
classes = [] |
|
104 |
||
105 |
if (date.year, date.month) == (month.year, month.month) : |
|
106 |
# current month |
|
107 |
classes.append('in-month') |
|
108 |
||
109 |
else : |
|
110 |
classes.append('out-month') |
|
111 |
||
112 |
if date == today : |
|
113 |
classes.append('today') |
|
114 |
||
115 |
class_ = ' '.join(classes) |
|
116 |
||
117 |
return tags.th(date.day, class_=class_) |
|
118 |
||
119 |
def get_events_for_interval (self, start, end) : |
|
120 |
""" |
|
121 |
Returns list of Order objects for given interval, ordered by start time. |
|
122 |
""" |
|
123 |
||
124 |
# XXX: bad imports |
|
125 |
from orders import Order |
|
126 |
||
127 |
return self.session.query(Order).filter( |
|
128 |
(Order.event_start.between(start, end)) |
|
129 |
| (Order.event_end.between(start, end)) |
|
42
fa5694ee0f98
cal: also include events that span across the whole week
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
130 |
| (db.between(start, Order.event_start, Order.event_end)) |
37 | 131 |
).order_by(Order.event_start).all() |
132 |
||
133 |
def render_week (self, month, week) : |
|
134 |
""" |
|
135 |
Render day rows for given week. |
|
136 |
""" |
|
137 |
||
138 |
# XXX: nasty |
|
139 |
from svv import urls |
|
140 |
||
141 |
# load events for week |
|
142 |
week_start = datetime.datetime.combine(min(week), datetime.time(0, 0, 0)) |
|
143 |
week_end = datetime.datetime.combine(max(week), datetime.time(23, 59, 59)) |
|
144 |
||
145 |
orders = self.get_events_for_interval(week_start, week_end) |
|
146 |
||
147 |
log.debug("Render week %r -> %r: %d", week_start, week_end, len(orders)) |
|
148 |
||
149 |
# day headers |
|
150 |
yield tags.tr(class_='week-header')( |
|
151 |
self.render_day_header(month, date) for date in week |
|
152 |
) |
|
153 |
||
154 |
# each even on its own row for now |
|
155 |
for order in orders : |
|
39
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
156 |
# start/end date for this week |
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
157 |
start = min(date for date in week if order.on_date(date)) |
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
158 |
end = max(date for date in week if order.on_date(date)) |
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
159 |
|
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
160 |
# as vector into week |
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
161 |
leading = (start - min(week)).days |
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
162 |
length = (end - start).days + 1 |
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
163 |
trailing = (max(week) - end).days |
37 | 164 |
|
40
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
165 |
# continues prev/next? |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
166 |
prev = (start > order.event_start.date()) |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
167 |
next = (end < order.event_end.date()) |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
168 |
|
39
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
169 |
log.debug("Event %r from %r -> %r", order.event_name, start, end) |
37 | 170 |
|
39
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
171 |
yield tags.tr(class_='week-data')( |
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
172 |
[tags.td("")] * leading, |
40
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
173 |
tags.td(colspan=length, class_=(' '.join(cls for cls in ( |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
174 |
'event', |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
175 |
'continues-prev' if prev else None, |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
176 |
'continues-next' if next else None, |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
177 |
) if cls)))( |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
178 |
tags.a(href=self.url_for(urls.OrderView, id=order.id))( |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
179 |
tags.div(class_='arrow-left')("") if prev else None, |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
180 |
order.event_name, |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
181 |
tags.div(class_='arrow-right')("") if next else None, |
30a0a0fa8c54
cal: span events across weeks
Tero Marttila <terom@fixme.fi>
parents:
39
diff
changeset
|
182 |
) |
39
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
183 |
), |
4f331cfc76a4
cal: span events across week's days
Tero Marttila <terom@fixme.fi>
parents:
37
diff
changeset
|
184 |
[tags.td("")] * trailing, |
37 | 185 |
) |
186 |
||
187 |
def render_calendar (self, month) : |
|
188 |
""" |
|
189 |
Render calendar for given date's month. |
|
190 |
""" |
|
191 |
||
192 |
cal = calendar.Calendar(self.FIRST_WEEKDAY) |
|
193 |
||
194 |
# next/prev month |
|
195 |
prev = self.prev_month(month) |
|
196 |
next = self.next_month(month) |
|
197 |
||
198 |
return tags.table(class_='calendar')( |
|
199 |
tags.caption( |
|
200 |
tags.a(href=self.url_for(CalendarView, yearmonth=prev.strftime(self.URL_FORMAT)), class_='prev-month')( |
|
201 |
html.raw("«") |
|
202 |
), |
|
203 |
month.strftime(self.MONTH_TITLE_FORMAT), |
|
204 |
tags.a(href=self.url_for(CalendarView, yearmonth=next.strftime(self.URL_FORMAT)), class_='next-month')( |
|
205 |
html.raw("»") |
|
206 |
), |
|
207 |
), |
|
208 |
||
209 |
# week-day headers |
|
210 |
tags.thead( |
|
211 |
tags.tr( |
|
212 |
tags.th( |
|
213 |
self.dayofweek_title(dow) |
|
214 |
) for dow in cal.iterweekdays() |
|
215 |
) |
|
216 |
), |
|
217 |
||
218 |
# month weeks |
|
219 |
tags.tbody( |
|
220 |
( |
|
221 |
self.render_week(month, week) |
|
222 |
) for week in cal.monthdatescalendar(month.year, month.month) |
|
223 |
), |
|
224 |
) |
|
225 |
||
226 |
def render_content (self, yearmonth=None) : |
|
227 |
""" |
|
228 |
Render calendar HTML for given year/month. |
|
229 |
""" |
|
230 |
||
231 |
if yearmonth : |
|
232 |
# requested month |
|
233 |
month = datetime.datetime.strptime(yearmonth, self.URL_FORMAT).date() |
|
234 |
||
235 |
else : |
|
236 |
# this month |
|
237 |
month = datetime.date.today() |
|
238 |
||
239 |
return ( |
|
240 |
self.render_calendar(month), |
|
241 |
) |
|
242 |