author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 13 Nov 2009 11:16:24 +0100 | |
branch | stable |
changeset 3840 | 2eff4348b1e4 |
parent 3829 | 9674d62a54f9 |
child 3890 | d7a270f50f54 |
child 3914 | 36be7a5e53ad |
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:
1575
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:
1575
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
9 |
from logilab.mtconverter import xml_escape |
0 | 10 |
|
11 |
from cubicweb.interfaces import ITimetableViews |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
12 |
from cubicweb.selectors import implements |
889 | 13 |
from cubicweb.utils import date_range |
14 |
from cubicweb.view import AnyRsetView |
|
0 | 15 |
|
16 |
||
17 |
class _TaskEntry(object): |
|
18 |
def __init__(self, task, color, column): |
|
19 |
self.task = task |
|
20 |
self.color = color |
|
21 |
self.column = column |
|
22 |
self.lines = 1 |
|
23 |
||
24 |
MIN_COLS = 3 # minimum number of task columns for a single user |
|
25 |
||
26 |
class TimeTableView(AnyRsetView): |
|
27 |
id = 'timetable' |
|
28 |
title = _('timetable') |
|
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
29 |
__select__ = implements(ITimetableViews) |
0 | 30 |
need_navigation = False |
31 |
||
32 |
def call(self, title=None): |
|
33 |
"""Dumps a timetable from a resultset composed of a note (anything |
|
34 |
with start/stop) and a user (anything)""" |
|
35 |
self.req.add_css('cubicweb.timetable.css') |
|
36 |
dates = {} |
|
37 |
users = [] |
|
38 |
users_max = {} |
|
39 |
# XXX: try refactoring with calendar.py:OneMonthCal |
|
40 |
for row in xrange(self.rset.rowcount): |
|
1132 | 41 |
task = self.rset.get_entity(row, 0) |
3829
9674d62a54f9
close #474415 by using 'is' instead of comparison which explicitly fails on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
42 |
if len(self.rset[row]) > 1: |
1132 | 43 |
user = self.rset.get_entity(row, 1) |
0 | 44 |
else: |
3829
9674d62a54f9
close #474415 by using 'is' instead of comparison which explicitly fails on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
45 |
user = ALL_USERS |
0 | 46 |
the_dates = [] |
47 |
if task.start and task.stop: |
|
1555 | 48 |
if task.start.toordinal() == task.stop.toordinal(): |
0 | 49 |
the_dates.append(task.start) |
50 |
else: |
|
51 |
the_dates += date_range( task.start, task.stop ) |
|
52 |
elif task.start: |
|
53 |
the_dates.append(task.start) |
|
54 |
elif task.stop: |
|
55 |
the_dates.append(task.stop) |
|
56 |
for d in the_dates: |
|
57 |
d_users = dates.setdefault(d, {}) |
|
1132 | 58 |
u_tasks = d_users.setdefault(user, set()) |
0 | 59 |
u_tasks.add( task ) |
1132 | 60 |
task_max = users_max.setdefault(user, 0) |
0 | 61 |
if len(u_tasks)>task_max: |
62 |
users_max[user] = len(u_tasks) |
|
63 |
if user not in users: |
|
64 |
# keep original ordering |
|
65 |
users.append(user) |
|
66 |
if not dates: |
|
67 |
return |
|
68 |
date_min = min(dates) |
|
69 |
date_max = max(dates) |
|
70 |
#users = list(sorted(users, key=lambda u:u.login)) |
|
71 |
||
72 |
rows = [] |
|
73 |
# colors here are class names defined in cubicweb.css |
|
1132 | 74 |
colors = ["col%x" % i for i in xrange(12)] |
0 | 75 |
next_color_index = 0 |
76 |
||
77 |
visited_tasks = {} # holds a description of a task for a user |
|
78 |
task_colors = {} # remember a color assigned to a task |
|
79 |
for date in date_range(date_min, date_max): |
|
80 |
columns = [date] |
|
81 |
d_users = dates.get(date, {}) |
|
82 |
for user in users: |
|
83 |
# every user has its column "splitted" in at least MIN_COLS |
|
84 |
# sub-columns (for overlapping tasks) |
|
85 |
user_columns = [None] * max(MIN_COLS, users_max[user]) |
|
86 |
# every task that is "visited" for the first time |
|
87 |
# require a special treatment, so we put them in |
|
88 |
# 'postpone' |
|
89 |
postpone = [] |
|
90 |
for task in d_users.get(user, []): |
|
91 |
key = (task, user) |
|
92 |
if key in visited_tasks: |
|
93 |
task_descr = visited_tasks[ key ] |
|
94 |
user_columns[task_descr.column] = task_descr, False |
|
1133 | 95 |
task_descr.lines += 1 |
0 | 96 |
else: |
97 |
postpone.append(key) |
|
98 |
for key in postpone: |
|
99 |
# to every 'new' task we must affect a color |
|
100 |
# (which must be the same for every user concerned |
|
101 |
# by the task) |
|
102 |
task, user = key |
|
1132 | 103 |
for i, t in enumerate(user_columns): |
0 | 104 |
if t is None: |
105 |
if task in task_colors: |
|
106 |
color = task_colors[task] |
|
107 |
else: |
|
108 |
color = colors[next_color_index] |
|
109 |
next_color_index = (next_color_index+1)%len(colors) |
|
110 |
task_colors[task] = color |
|
111 |
task_descr = _TaskEntry(task, color, i) |
|
112 |
user_columns[i] = task_descr, True |
|
113 |
visited_tasks[key] = task_descr |
|
114 |
break |
|
115 |
else: |
|
116 |
raise RuntimeError("is it possible we got it wrong?") |
|
117 |
||
118 |
columns.append( user_columns ) |
|
119 |
rows.append( columns ) |
|
120 |
||
121 |
widths = [ len(col) for col in rows[0][1:] ] |
|
122 |
self.w(u'<div class="section">') |
|
123 |
if title: |
|
124 |
self.w(u'<h4>%s</h4>\n' % title) |
|
125 |
self.w(u'<table class="listing timetable">') |
|
126 |
self.render_col_headers(users, widths) |
|
127 |
self.render_rows(rows) |
|
128 |
self.w(u'</table>') |
|
129 |
self.w(u'</div>\n') |
|
130 |
||
1132 | 131 |
def render_col_headers(self, users, widths): |
0 | 132 |
""" render column headers """ |
133 |
self.w(u'<tr class="header">\n') |
|
134 |
||
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
135 |
self.w(u'<th class="ttdate"> </th>\n') |
0 | 136 |
columns = [] |
1132 | 137 |
for user, width in zip(users, widths): |
138 |
self.w(u'<th colspan="%s">' % max(MIN_COLS, width)) |
|
3829
9674d62a54f9
close #474415 by using 'is' instead of comparison which explicitly fails on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
139 |
if user is ALL_USERS: |
9674d62a54f9
close #474415 by using 'is' instead of comparison which explicitly fails on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
140 |
self.w('*') |
9674d62a54f9
close #474415 by using 'is' instead of comparison which explicitly fails on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
141 |
else: |
2471
3e2b50ece726
begin to drop ref to secondary view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
142 |
user.view('oneline', w=self.w) |
0 | 143 |
self.w(u'</th>') |
144 |
self.w(u'</tr>\n') |
|
145 |
return columns |
|
146 |
||
147 |
def render_rows(self, rows): |
|
148 |
""" render table content (row headers and central content) """ |
|
149 |
odd = False |
|
150 |
previous_is_empty = False |
|
151 |
for row in rows: |
|
152 |
date = row[0] |
|
153 |
empty_line = True |
|
154 |
for group in row[1:]: |
|
155 |
for value in group: |
|
156 |
if value: |
|
157 |
empty_line = False |
|
158 |
break |
|
159 |
else: |
|
160 |
continue |
|
161 |
break |
|
162 |
if empty_line and previous_is_empty: |
|
163 |
continue |
|
164 |
previous_is_empty = False |
|
165 |
||
166 |
klass = "even" |
|
1575
5eb02a4a4bcf
datetime fixes use weekday() instead of day_of_week
sylvain.thenault@logilab.fr
parents:
1555
diff
changeset
|
167 |
if date.weekday() in (5, 6) and not empty_line: |
0 | 168 |
klass = "odd" |
169 |
self.w(u'<tr class="%s">' % klass) |
|
170 |
odd = not odd |
|
171 |
||
172 |
if not empty_line: |
|
173 |
self.w(u'<th class="ttdate">%s</th>' % self.format_date(date) ) |
|
174 |
else: |
|
175 |
self.w(u'<th>...</th>' ) |
|
176 |
previous_is_empty = True |
|
177 |
||
178 |
empty_klasses = [ "ttle", "ttme", "ttre" ] |
|
179 |
filled_klasses = [ "ttlf", "ttmf", "ttrf" ] |
|
180 |
kj = 0 # 0: left, 1: mid, 2: right |
|
181 |
for uid, group in enumerate(row[1:]): |
|
182 |
for i, value in enumerate(group): |
|
183 |
if i == 0: |
|
184 |
kj = 0 |
|
185 |
elif i == len(group): |
|
186 |
kj = 2 |
|
187 |
else: |
|
188 |
kj = 1 |
|
189 |
if value: |
|
190 |
task_descr, first_row = value |
|
191 |
if first_row: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
192 |
url = xml_escape(task_descr.task.absolute_url(vid="edition")) |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
193 |
self.w(u'<td rowspan="%d" class="%s %s" onclick="document.location=\'%s\'"> <div>' % ( |
0 | 194 |
task_descr.lines, task_descr.color, filled_klasses[kj], url)) |
195 |
task_descr.task.view('tooltip', w=self.w) |
|
196 |
self.w(u'</div></td>') |
|
197 |
else: |
|
198 |
if empty_line: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
199 |
self.w(u'<td class="ttempty"> </td>') |
0 | 200 |
else: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
201 |
self.w(u'<td class="%s"> </td>' % empty_klasses[kj] ) |
0 | 202 |
self.w(u'</tr>\n') |