author | sylvain.thenault@logilab.fr |
Tue, 10 Mar 2009 11:35:52 +0100 | |
branch | tls-sprint |
changeset 1016 | 26387b836099 |
parent 767 | b094cf8535b4 |
child 1025 | aebf37f460c1 |
permissions | -rw-r--r-- |
0 | 1 |
"""html calendar views |
2 |
||
3 |
:organization: Logilab |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
524
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
8 |
from datetime import datetime, date, timedelta |
0 | 9 |
|
10 |
from vobject import iCalendar, icalendar |
|
11 |
||
12 |
from logilab.mtconverter import html_escape |
|
13 |
||
14 |
from cubicweb.interfaces import ICalendarable |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
524
diff
changeset
|
15 |
from cubicweb.selectors import implements |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
16 |
from cubicweb.utils import strptime, date_range |
767 | 17 |
from cubicweb.view import EntityView |
0 | 18 |
from cubicweb.common.uilib import ajax_replace_url |
19 |
||
20 |
||
21 |
# For backward compatibility |
|
22 |
from cubicweb.interfaces import ICalendarViews, ITimetableViews |
|
23 |
try: |
|
24 |
from cubicweb.web.views.old_calendar import _CalendarView, AMPMWeekCalendarView |
|
25 |
except ImportError: |
|
26 |
import logging |
|
27 |
logger = logging.getLogger('cubicweb.registry') |
|
28 |
logger.info("old calendar views could not be found and won't be registered") |
|
29 |
||
30 |
_ = unicode |
|
31 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
32 |
# # useful constants & functions |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
33 |
# def mkdt(mxdate): |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
34 |
# """ |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
35 |
# Build a stdlib datetime date from a mx.datetime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
36 |
# """ |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
37 |
# d = mxdate |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
38 |
# return datetime(d.year, d.month, d.day, d.hour, d.minute, |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
39 |
# tzinfo=icalendar.utc) |
0 | 40 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
41 |
# used by i18n tools |
0 | 42 |
WEEKDAYS = (_("monday"), _("tuesday"), _("wednesday"), _("thursday"), |
43 |
_("friday"), _("saturday"), _("sunday")) |
|
44 |
MONTHNAMES = ( _('january'), _('february'), _('march'), _('april'), _('may'), |
|
45 |
_('june'), _('july'), _('august'), _('september'), _('october'), |
|
46 |
_('november'), _('december') |
|
47 |
) |
|
48 |
||
49 |
################# |
|
50 |
# In calendar views (views used as calendar cell item) |
|
51 |
||
52 |
||
53 |
class CalendarItemView(EntityView): |
|
54 |
id = 'calendaritem' |
|
55 |
||
56 |
def cell_call(self, row, col, dates=False): |
|
57 |
task = self.complete_entity(row) |
|
58 |
task.view('oneline', w=self.w) |
|
59 |
if dates: |
|
60 |
if task.start and task.stop: |
|
767 | 61 |
self.w('<br/>' % self.req._('from %(date)s' % {'date': self.format_date(task.start)})) |
62 |
self.w('<br/>' % self.req._('to %(date)s' % {'date': self.format_date(task.stop)})) |
|
0 | 63 |
self.w('<br/>to %s'%self.format_date(task.stop)) |
64 |
||
65 |
class CalendarLargeItemView(CalendarItemView): |
|
66 |
id = 'calendarlargeitem' |
|
67 |
||
68 |
################# |
|
69 |
# Calendar views |
|
70 |
||
71 |
class iCalView(EntityView): |
|
72 |
"""A calendar view that generates a iCalendar file (RFC 2445) |
|
73 |
||
74 |
Does apply to ICalendarable compatible entities |
|
75 |
""" |
|
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
76 |
__select__ = implements(ICalendarable) |
0 | 77 |
need_navigation = False |
78 |
content_type = 'text/calendar' |
|
79 |
title = _('iCalendar') |
|
80 |
templatable = False |
|
81 |
id = 'ical' |
|
82 |
||
83 |
def call(self): |
|
84 |
ical = iCalendar() |
|
85 |
for i in range(len(self.rset.rows)): |
|
86 |
task = self.complete_entity(i) |
|
87 |
event = ical.add('vevent') |
|
88 |
event.add('summary').value = task.dc_title() |
|
89 |
event.add('description').value = task.dc_description() |
|
90 |
if task.start: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
91 |
event.add('dtstart').value = task.start |
0 | 92 |
if task.stop: |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
93 |
event.add('dtend').value = task.stop |
0 | 94 |
|
95 |
buff = ical.serialize() |
|
96 |
if not isinstance(buff, unicode): |
|
97 |
buff = unicode(buff, self.req.encoding) |
|
98 |
self.w(buff) |
|
99 |
||
100 |
class hCalView(EntityView): |
|
101 |
"""A calendar view that generates a hCalendar file |
|
102 |
||
103 |
Does apply to ICalendarable compatible entities |
|
104 |
""" |
|
767 | 105 |
id = 'hcal' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
106 |
__select__ = implements(ICalendarable) |
0 | 107 |
need_navigation = False |
108 |
title = _('hCalendar') |
|
524
eee3983e29e9
hcal is a microformat and can be inserted in html
sylvain.thenault@logilab.fr
parents:
431
diff
changeset
|
109 |
#templatable = False |
0 | 110 |
|
111 |
def call(self): |
|
112 |
self.w(u'<div class="hcalendar">') |
|
113 |
for i in range(len(self.rset.rows)): |
|
114 |
task = self.complete_entity(i) |
|
115 |
self.w(u'<div class="vevent">') |
|
116 |
self.w(u'<h3 class="summary">%s</h3>' % html_escape(task.dc_title())) |
|
117 |
self.w(u'<div class="description">%s</div>' % html_escape(task.dc_description())) |
|
118 |
if task.start: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
119 |
self.w(u'<abbr class="dtstart" title="%s">%s</abbr>' % (task.start.isoformat(), self.format_date(task.start))) |
0 | 120 |
if task.stop: |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
121 |
self.w(u'<abbr class="dtstop" title="%s">%s</abbr>' % (task.stop.isoformat(), self.format_date(task.stop))) |
0 | 122 |
self.w(u'</div>') |
123 |
self.w(u'</div>') |
|
124 |
||
125 |
||
126 |
class _TaskEntry(object): |
|
127 |
def __init__(self, task, color, index=0): |
|
128 |
self.task = task |
|
129 |
self.color = color |
|
130 |
self.index = index |
|
131 |
self.length = 1 |
|
132 |
||
133 |
class OneMonthCal(EntityView): |
|
134 |
"""At some point, this view will probably replace ampm calendars""" |
|
767 | 135 |
id = 'onemonthcal' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
136 |
__select__ = implements(ICalendarable) |
0 | 137 |
need_navigation = False |
138 |
title = _('one month') |
|
139 |
||
140 |
def call(self): |
|
141 |
self.req.add_js('cubicweb.ajax.js') |
|
142 |
self.req.add_css('cubicweb.calendar.css') |
|
143 |
# XXX: restrict courses directy with RQL |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
144 |
_today = datetime.today() |
0 | 145 |
|
146 |
if 'year' in self.req.form: |
|
147 |
year = int(self.req.form['year']) |
|
148 |
else: |
|
149 |
year = _today.year |
|
150 |
if 'month' in self.req.form: |
|
151 |
month = int(self.req.form['month']) |
|
152 |
else: |
|
153 |
month = _today.month |
|
154 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
155 |
first_day_of_month = date(year, month, 1) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
156 |
firstday = first_day_of_month - timedelta(first_day_of_month.weekday(), 0, 0) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
157 |
if month >= 12: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
158 |
last_day_of_month = date(year + 1, 1, 1) - timedelta(1, 0, 0) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
159 |
else: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
160 |
last_day_of_month = date(year, month + 1, 1) - timedelta(1, 0, 0) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
161 |
lastday = last_day_of_month + timedelta(6 - last_day_of_month.weekday(), 0, 0) |
0 | 162 |
month_dates = list(date_range(firstday, lastday)) |
163 |
dates = {} |
|
164 |
users = [] |
|
165 |
task_max = 0 |
|
166 |
for row in xrange(self.rset.rowcount): |
|
167 |
task = self.rset.get_entity(row,0) |
|
168 |
if len(self.rset[row]) > 1 and self.rset.description[row][1] == 'EUser': |
|
169 |
user = self.rset.get_entity(row,1) |
|
170 |
else: |
|
171 |
user = None |
|
172 |
the_dates = [] |
|
173 |
if task.start: |
|
174 |
if task.start > lastday: |
|
175 |
continue |
|
176 |
the_dates = [task.start] |
|
177 |
if task.stop: |
|
178 |
if task.stop < firstday: |
|
179 |
continue |
|
180 |
the_dates = [task.stop] |
|
181 |
if task.start and task.stop: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
182 |
if task.start.isocalendar() == task.stop.isocalendar(): |
0 | 183 |
date = task.start |
184 |
if firstday<= date <= lastday: |
|
185 |
the_dates = [date] |
|
186 |
else: |
|
187 |
the_dates = date_range(max(task.start,firstday), |
|
188 |
min(task.stop,lastday)) |
|
189 |
if not the_dates: |
|
190 |
continue |
|
191 |
||
192 |
for d in the_dates: |
|
193 |
d_tasks = dates.setdefault((d.year, d.month, d.day), {}) |
|
194 |
t_users = d_tasks.setdefault(task,set()) |
|
195 |
t_users.add( user ) |
|
196 |
if len(d_tasks)>task_max: |
|
197 |
task_max = len(d_tasks) |
|
198 |
||
199 |
days = [] |
|
200 |
nrows = max(3,task_max) |
|
201 |
# colors here are class names defined in cubicweb.css |
|
202 |
colors = [ "col%x"%i for i in range(12) ] |
|
203 |
next_color_index = 0 |
|
204 |
||
205 |
visited_tasks = {} # holds a description of a task |
|
206 |
task_colors = {} # remember a color assigned to a task |
|
207 |
for date in month_dates: |
|
208 |
d_tasks = dates.get((date.year, date.month, date.day), {}) |
|
209 |
rows = [None] * nrows |
|
210 |
# every task that is "visited" for the first time |
|
211 |
# require a special treatment, so we put them in |
|
212 |
# 'postpone' |
|
213 |
postpone = [] |
|
214 |
for task in d_tasks: |
|
215 |
if task in visited_tasks: |
|
216 |
task_descr = visited_tasks[ task ] |
|
217 |
rows[task_descr.index] = task_descr |
|
218 |
else: |
|
219 |
postpone.append(task) |
|
220 |
for task in postpone: |
|
221 |
# to every 'new' task we must affect a color |
|
222 |
# (which must be the same for every user concerned |
|
223 |
# by the task) |
|
224 |
for i,t in enumerate(rows): |
|
225 |
if t is None: |
|
226 |
if task in task_colors: |
|
227 |
color = task_colors[task] |
|
228 |
else: |
|
229 |
color = colors[next_color_index] |
|
230 |
next_color_index = (next_color_index+1)%len(colors) |
|
231 |
task_colors[task] = color |
|
232 |
task_descr = _TaskEntry(task, color, i) |
|
233 |
rows[i] = task_descr |
|
234 |
visited_tasks[task] = task_descr |
|
235 |
break |
|
236 |
else: |
|
237 |
raise RuntimeError("is it possible we got it wrong?") |
|
238 |
||
239 |
days.append( rows ) |
|
240 |
||
241 |
curdate = first_day_of_month |
|
242 |
self.w(u'<div id="onemonthcalid">') |
|
243 |
# build schedule |
|
244 |
self.w(u'<table class="omcalendar">') |
|
245 |
prevlink, nextlink = self._prevnext_links(curdate) # XXX |
|
246 |
self.w(u'<tr><th><a href="%s"><<</a></th><th colspan="5">%s %s</th>' |
|
247 |
u'<th><a href="%s">>></a></th></tr>' % |
|
248 |
(html_escape(prevlink), self.req._(curdate.strftime('%B').lower()), |
|
249 |
curdate.year, html_escape(nextlink))) |
|
250 |
||
251 |
# output header |
|
252 |
self.w(u'<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>' % |
|
253 |
tuple(self.req._(day) for day in WEEKDAYS)) |
|
254 |
||
255 |
# build calendar |
|
256 |
for date, task_rows in zip(month_dates, days): |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
257 |
if date.weekday() == 0: |
0 | 258 |
self.w(u'<tr>') |
259 |
self._build_calendar_cell(date, task_rows, curdate) |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
260 |
if date.weekday() == 6: |
0 | 261 |
self.w(u'</tr>') |
262 |
self.w(u'</table></div>') |
|
263 |
||
264 |
def _prevnext_links(self, curdate): |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
265 |
prevdate = curdate - timedelta(31, 0, 0) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
266 |
nextdate = curdate + timedelta(31, 0, 0) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
267 |
rql = self.rset.printable_rql() |
0 | 268 |
prevlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal', |
269 |
year=prevdate.year, month=prevdate.month) |
|
270 |
nextlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal', |
|
271 |
year=nextdate.year, month=nextdate.month) |
|
272 |
return prevlink, nextlink |
|
273 |
||
274 |
def _build_calendar_cell(self, date, rows, curdate): |
|
275 |
curmonth = curdate.month |
|
276 |
classes = "" |
|
277 |
if date.month != curmonth: |
|
278 |
classes += " outOfRange" |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
279 |
if date == datetime.today(): |
0 | 280 |
classes += " today" |
281 |
self.w(u'<td class="cell%s">' % classes) |
|
282 |
self.w(u'<div class="calCellTitle%s">' % classes) |
|
283 |
self.w(u'<div class="day">%s</div>' % date.day) |
|
284 |
||
285 |
if len(self.rset.column_types(0)) == 1: |
|
286 |
etype = list(self.rset.column_types(0))[0] |
|
287 |
url = self.build_url(vid='creation', etype=etype, |
|
288 |
schedule=True, |
|
289 |
start=self.format_date(date), stop=self.format_date(date), |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
290 |
__redirectrql=self.rset.printable_rql(), |
0 | 291 |
__redirectparams=self.req.build_url_params(year=curdate.year, month=curmonth), |
292 |
__redirectvid=self.id |
|
293 |
) |
|
294 |
self.w(u'<div class="cmd"><a href="%s">%s</a></div>' % (html_escape(url), self.req._(u'add'))) |
|
295 |
self.w(u' ') |
|
296 |
self.w(u'</div>') |
|
297 |
self.w(u'<div class="cellContent">') |
|
298 |
for task_descr in rows: |
|
299 |
if task_descr: |
|
300 |
task = task_descr.task |
|
301 |
self.w(u'<div class="task %s">' % task_descr.color) |
|
302 |
task.view('calendaritem', w=self.w ) |
|
303 |
url = task.absolute_url(vid='edition', |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
304 |
__redirectrql=self.rset.printable_rql(), |
0 | 305 |
__redirectparams=self.req.build_url_params(year=curdate.year, month=curmonth), |
306 |
__redirectvid=self.id |
|
307 |
) |
|
308 |
||
309 |
self.w(u'<div class="tooltip" ondblclick="stopPropagation(event); window.location.assign(\'%s\'); return false;">' % html_escape(url)) |
|
310 |
task.view('tooltip', w=self.w ) |
|
311 |
self.w(u'</div>') |
|
312 |
else: |
|
313 |
self.w(u'<div class="task">') |
|
314 |
self.w(u" ") |
|
315 |
self.w(u'</div>') |
|
316 |
self.w(u'</div>') |
|
317 |
self.w(u'</td>') |
|
318 |
||
319 |
||
320 |
class OneWeekCal(EntityView): |
|
321 |
"""At some point, this view will probably replace ampm calendars""" |
|
767 | 322 |
id = 'oneweekcal' |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
323 |
__select__ = implements(ICalendarable) |
0 | 324 |
need_navigation = False |
325 |
title = _('one week') |
|
326 |
||
327 |
def call(self): |
|
328 |
self.req.add_js( ('cubicweb.ajax.js', 'cubicweb.calendar.js') ) |
|
329 |
self.req.add_css('cubicweb.calendar.css') |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
330 |
# XXX: restrict directly with RQL |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
331 |
_today = datetime.today() |
0 | 332 |
if 'year' in self.req.form: |
333 |
year = int(self.req.form['year']) |
|
334 |
else: |
|
335 |
year = _today.year |
|
336 |
if 'week' in self.req.form: |
|
337 |
week = int(self.req.form['week']) |
|
338 |
else: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
339 |
week = _today.isocalendar()[1] |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
340 |
# week - 1 since we get week number > 0 while we want it to start from 0 |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
341 |
first_day_of_week = strptime('%s-%s-1' % (year, week - 1), '%Y-%U-%w') |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
342 |
lastday = first_day_of_week + timedelta(6, 0, 0) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
343 |
firstday = first_day_of_week |
0 | 344 |
dates = [[] for i in range(7)] |
345 |
task_max = 0 |
|
346 |
task_colors = {} # remember a color assigned to a task |
|
347 |
# colors here are class names defined in cubicweb.css |
|
348 |
colors = [ "col%x"%i for i in range(12) ] |
|
349 |
next_color_index = 0 |
|
350 |
done_tasks = [] |
|
351 |
for row in xrange(self.rset.rowcount): |
|
352 |
task = self.rset.get_entity(row,0) |
|
353 |
if task in done_tasks: |
|
354 |
continue |
|
355 |
done_tasks.append(task) |
|
356 |
the_dates = [] |
|
357 |
if task.start: |
|
358 |
if task.start > lastday: |
|
359 |
continue |
|
360 |
the_dates = [task.start] |
|
361 |
if task.stop: |
|
362 |
if task.stop < firstday: |
|
363 |
continue |
|
364 |
the_dates = [task.stop] |
|
365 |
if task.start and task.stop: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
366 |
the_dates = date_range(max(task.start, firstday), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
367 |
min(task.stop, lastday)) |
0 | 368 |
if not the_dates: |
369 |
continue |
|
370 |
||
371 |
if task not in task_colors: |
|
372 |
task_colors[task] = colors[next_color_index] |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
373 |
next_color_index = (next_color_index+1) % len(colors) |
0 | 374 |
|
375 |
for d in the_dates: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
376 |
day = d.weekday() |
0 | 377 |
task_descr = _TaskEntry(task, task_colors[task]) |
378 |
dates[day].append(task_descr) |
|
379 |
||
380 |
self.w(u'<div id="oneweekcalid">') |
|
381 |
# build schedule |
|
382 |
self.w(u'<table class="omcalendar" id="week">') |
|
383 |
prevlink, nextlink = self._prevnext_links(first_day_of_week) # XXX |
|
384 |
self.w(u'<tr><th class="transparent"></th>') |
|
385 |
self.w(u'<th><a href="%s"><<</a></th><th colspan="5">%s %s %s</th>' |
|
386 |
u'<th><a href="%s">>></a></th></tr>' % |
|
387 |
(html_escape(prevlink), first_day_of_week.year, |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
388 |
self.req._(u'week'), first_day_of_week.isocalendar()[1], |
0 | 389 |
html_escape(nextlink))) |
390 |
||
391 |
# output header |
|
392 |
self.w(u'<tr>') |
|
393 |
self.w(u'<th class="transparent"></th>') # column for hours |
|
394 |
_today = today() |
|
395 |
for i, day in enumerate(WEEKDAYS): |
|
396 |
date = first_day_of_week + i |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
397 |
if date.isocalendar() == _today.isocalendar(): |
0 | 398 |
self.w(u'<th class="today">%s<br/>%s</th>' % (self.req._(day), self.format_date(date))) |
399 |
else: |
|
400 |
self.w(u'<th>%s<br/>%s</th>' % (self.req._(day), self.format_date(date))) |
|
401 |
self.w(u'</tr>') |
|
402 |
||
403 |
# build week calendar |
|
404 |
self.w(u'<tr>') |
|
405 |
self.w(u'<td style="width:5em;">') # column for hours |
|
406 |
extra = "" |
|
407 |
for h in range(8, 20): |
|
408 |
self.w(u'<div class="hour" %s>'%extra) |
|
409 |
self.w(u'%02d:00'%h) |
|
410 |
self.w(u'</div>') |
|
411 |
self.w(u'</td>') |
|
412 |
||
413 |
for i, day in enumerate(WEEKDAYS): |
|
414 |
date = first_day_of_week + i |
|
415 |
classes = "" |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
416 |
if date.isocalendar() == _today.isocalendar(): |
0 | 417 |
classes = " today" |
418 |
self.w(u'<td class="column %s" id="%s">'%(classes, day)) |
|
419 |
if len(self.rset.column_types(0)) == 1: |
|
420 |
etype = list(self.rset.column_types(0))[0] |
|
421 |
url = self.build_url(vid='creation', etype=etype, |
|
422 |
schedule=True, |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
423 |
__redirectrql=self.rset.printable_rql(), |
0 | 424 |
__redirectparams=self.req.build_url_params(year=year, week=week), |
425 |
__redirectvid=self.id |
|
426 |
) |
|
427 |
extra = ' ondblclick="addCalendarItem(event, hmin=%s, hmax=%s, year=%s, month=%s, day=%s, duration=%s, baseurl=\'%s\')"' % (8,20,date.year, date.month, date.day, 2, html_escape(url)) |
|
428 |
else: |
|
429 |
extra = "" |
|
430 |
self.w(u'<div class="columndiv"%s>'% extra) |
|
431 |
for h in range(8, 20): |
|
432 |
self.w(u'<div class="hourline" style="top:%sex;">'%((h-7)*8)) |
|
433 |
self.w(u'</div>') |
|
434 |
if dates[i]: |
|
435 |
self._build_calendar_cell(date, dates[i]) |
|
436 |
self.w(u'</div>') |
|
437 |
self.w(u'</td>') |
|
438 |
self.w(u'</tr>') |
|
439 |
self.w(u'</table></div>') |
|
440 |
self.w(u'<div id="coord"></div>') |
|
441 |
self.w(u'<div id="debug"> </div>') |
|
442 |
||
443 |
def _one_day_task(self, task): |
|
444 |
""" |
|
445 |
Return true if the task is a "one day" task; ie it have a start and a stop the same day |
|
446 |
""" |
|
447 |
if task.start and task.stop: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
448 |
if task.start.isocalendar() == task.stop.isocalendar(): |
0 | 449 |
return True |
450 |
return False |
|
451 |
||
452 |
def _build_calendar_cell(self, date, task_descrs): |
|
453 |
inday_tasks = [t for t in task_descrs if self._one_day_task(t.task) and t.task.start.hour<20 and t.task.stop.hour>7] |
|
454 |
wholeday_tasks = [t for t in task_descrs if not self._one_day_task(t.task)] |
|
455 |
||
456 |
inday_tasks.sort(key=lambda t:t.task.start) |
|
457 |
sorted_tasks = [] |
|
458 |
for i, t in enumerate(wholeday_tasks): |
|
459 |
t.index = i |
|
460 |
ncols = len(wholeday_tasks) |
|
461 |
while inday_tasks: |
|
462 |
t = inday_tasks.pop(0) |
|
463 |
for i, c in enumerate(sorted_tasks): |
|
464 |
if not c or c[-1].task.stop <= t.task.start: |
|
465 |
c.append(t) |
|
466 |
t.index = i+ncols |
|
467 |
break |
|
468 |
else: |
|
469 |
t.index = len(sorted_tasks) + ncols |
|
470 |
sorted_tasks.append([t]) |
|
471 |
ncols += len(sorted_tasks) |
|
472 |
if ncols == 0: |
|
473 |
return |
|
474 |
||
475 |
inday_tasks = [] |
|
476 |
for tasklist in sorted_tasks: |
|
477 |
inday_tasks += tasklist |
|
478 |
width = 100.0/ncols |
|
479 |
for task_desc in wholeday_tasks + inday_tasks: |
|
480 |
task = task_desc.task |
|
481 |
start_hour = 8 |
|
482 |
start_min = 0 |
|
483 |
stop_hour = 20 |
|
484 |
stop_min = 0 |
|
485 |
if task.start: |
|
486 |
if date < task.start < date + 1: |
|
487 |
start_hour = max(8, task.start.hour) |
|
488 |
start_min = task.start.minute |
|
489 |
if task.stop: |
|
490 |
if date < task.stop < date + 1: |
|
491 |
stop_hour = min(20, task.stop.hour) |
|
492 |
if stop_hour < 20: |
|
493 |
stop_min = task.stop.minute |
|
494 |
||
495 |
height = 100.0*(stop_hour+stop_min/60.0-start_hour-start_min/60.0)/(20-8) |
|
496 |
top = 100.0*(start_hour+start_min/60.0-8)/(20-8) |
|
497 |
left = width*task_desc.index |
|
498 |
style = "height: %s%%; width: %s%%; top: %s%%; left: %s%%; " % \ |
|
499 |
(height, width, top, left) |
|
500 |
self.w(u'<div class="task %s" style="%s">' % \ |
|
501 |
(task_desc.color, style)) |
|
502 |
task.view('calendaritem', dates=False, w=self.w) |
|
503 |
url = task.absolute_url(vid='edition', |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
504 |
__redirectrql=self.rset.printable_rql(), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
505 |
__redirectparams=self.req.build_url_params(year=date.year, week=date.isocalendar()[1]), |
0 | 506 |
__redirectvid=self.id |
507 |
) |
|
508 |
||
509 |
self.w(u'<div class="tooltip" ondblclick="stopPropagation(event); window.location.assign(\'%s\'); return false;">' % html_escape(url)) |
|
510 |
task.view('tooltip', w=self.w) |
|
511 |
self.w(u'</div>') |
|
512 |
if task.start is None: |
|
513 |
self.w(u'<div class="bottommarker">') |
|
514 |
self.w(u'<div class="bottommarkerline" style="margin: 0px 3px 0px 3px; height: 1px;">') |
|
515 |
self.w(u'</div>') |
|
516 |
self.w(u'<div class="bottommarkerline" style="margin: 0px 2px 0px 2px; height: 1px;">') |
|
517 |
self.w(u'</div>') |
|
518 |
self.w(u'<div class="bottommarkerline" style="margin: 0px 1px 0px 1px; height: 3ex; color: white; font-size: x-small; vertical-align: center; text-align: center;">') |
|
519 |
self.w(u'end') |
|
520 |
self.w(u'</div>') |
|
521 |
self.w(u'</div>') |
|
522 |
self.w(u'</div>') |
|
523 |
||
524 |
||
525 |
def _prevnext_links(self, curdate): |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
526 |
prevdate = curdate - timedelta(7, 0, 0) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
527 |
nextdate = curdate + timedelta(7, 0, 0) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
528 |
rql = self.rset.printable_rql() |
0 | 529 |
prevlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal', |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
530 |
year=prevdate.year, week=prevdate.isocalendar()[1]) |
0 | 531 |
nextlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal', |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
767
diff
changeset
|
532 |
year=nextdate.year, week=nextdate.isocalendar()[1]) |
0 | 533 |
return prevlink, nextlink |
534 |