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
|
|
23 |
FIRST_WEEKDAY = 6
|
|
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))
|
|
130 |
).order_by(Order.event_start).all()
|
|
131 |
|
|
132 |
def render_week (self, month, week) :
|
|
133 |
"""
|
|
134 |
Render day rows for given week.
|
|
135 |
"""
|
|
136 |
|
|
137 |
# XXX: nasty
|
|
138 |
from svv import urls
|
|
139 |
|
|
140 |
# load events for week
|
|
141 |
week_start = datetime.datetime.combine(min(week), datetime.time(0, 0, 0))
|
|
142 |
week_end = datetime.datetime.combine(max(week), datetime.time(23, 59, 59))
|
|
143 |
|
|
144 |
orders = self.get_events_for_interval(week_start, week_end)
|
|
145 |
|
|
146 |
log.debug("Render week %r -> %r: %d", week_start, week_end, len(orders))
|
|
147 |
|
|
148 |
# day headers
|
|
149 |
yield tags.tr(class_='week-header')(
|
|
150 |
self.render_day_header(month, date) for date in week
|
|
151 |
)
|
|
152 |
|
|
153 |
# each even on its own row for now
|
|
154 |
for order in orders :
|
|
155 |
yield tags.tr(class_='week-data')(
|
|
156 |
(
|
|
157 |
tags.td(
|
|
158 |
tags.a(href=self.url_for(urls.OrderView, id=order.id))(order.event_name)
|
|
159 |
|
|
160 |
) if order.on_date(date) else (
|
|
161 |
tags.td("")
|
|
162 |
|
|
163 |
)
|
|
164 |
) for date in week
|
|
165 |
)
|
|
166 |
|
|
167 |
def render_calendar (self, month) :
|
|
168 |
"""
|
|
169 |
Render calendar for given date's month.
|
|
170 |
"""
|
|
171 |
|
|
172 |
cal = calendar.Calendar(self.FIRST_WEEKDAY)
|
|
173 |
|
|
174 |
# next/prev month
|
|
175 |
prev = self.prev_month(month)
|
|
176 |
next = self.next_month(month)
|
|
177 |
|
|
178 |
return tags.table(class_='calendar')(
|
|
179 |
tags.caption(
|
|
180 |
tags.a(href=self.url_for(CalendarView, yearmonth=prev.strftime(self.URL_FORMAT)), class_='prev-month')(
|
|
181 |
html.raw("«")
|
|
182 |
),
|
|
183 |
month.strftime(self.MONTH_TITLE_FORMAT),
|
|
184 |
tags.a(href=self.url_for(CalendarView, yearmonth=next.strftime(self.URL_FORMAT)), class_='next-month')(
|
|
185 |
html.raw("»")
|
|
186 |
),
|
|
187 |
),
|
|
188 |
|
|
189 |
# week-day headers
|
|
190 |
tags.thead(
|
|
191 |
tags.tr(
|
|
192 |
tags.th(
|
|
193 |
self.dayofweek_title(dow)
|
|
194 |
) for dow in cal.iterweekdays()
|
|
195 |
)
|
|
196 |
),
|
|
197 |
|
|
198 |
# month weeks
|
|
199 |
tags.tbody(
|
|
200 |
(
|
|
201 |
self.render_week(month, week)
|
|
202 |
) for week in cal.monthdatescalendar(month.year, month.month)
|
|
203 |
),
|
|
204 |
)
|
|
205 |
|
|
206 |
def render_content (self, yearmonth=None) :
|
|
207 |
"""
|
|
208 |
Render calendar HTML for given year/month.
|
|
209 |
"""
|
|
210 |
|
|
211 |
if yearmonth :
|
|
212 |
# requested month
|
|
213 |
month = datetime.datetime.strptime(yearmonth, self.URL_FORMAT).date()
|
|
214 |
|
|
215 |
else :
|
|
216 |
# this month
|
|
217 |
month = datetime.date.today()
|
|
218 |
|
|
219 |
return (
|
|
220 |
self.render_calendar(month),
|
|
221 |
)
|
|
222 |
|