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