author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Thu, 03 Jun 2010 09:41:41 +0200 | |
branch | stable |
changeset 5649 | a07dee204187 |
parent 5424 | 8ecbcbff9777 |
child 5556 | 9ab2b4c74baf |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""html calendar views |
19 |
||
20 |
""" |
|
21 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
22 |
from logilab.mtconverter import xml_escape |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
23 |
from logilab.common.date import date_range, todatetime |
0 | 24 |
|
25 |
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
|
26 |
from cubicweb.selectors import implements |
889 | 27 |
from cubicweb.view import AnyRsetView |
0 | 28 |
|
29 |
||
30 |
class _TaskEntry(object): |
|
31 |
def __init__(self, task, color, column): |
|
32 |
self.task = task |
|
33 |
self.color = color |
|
34 |
self.column = column |
|
35 |
self.lines = 1 |
|
36 |
||
37 |
MIN_COLS = 3 # minimum number of task columns for a single user |
|
3914
36be7a5e53ad
fix name error in timetable view, always write unicode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3829
diff
changeset
|
38 |
ALL_USERS = object() |
0 | 39 |
|
40 |
class TimeTableView(AnyRsetView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
41 |
__regid__ = 'timetable' |
0 | 42 |
title = _('timetable') |
728
a95b284150d1
first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
43 |
__select__ = implements(ITimetableViews) |
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
44 |
paginable = False |
0 | 45 |
|
46 |
def call(self, title=None): |
|
47 |
"""Dumps a timetable from a resultset composed of a note (anything |
|
48 |
with start/stop) and a user (anything)""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
49 |
self._cw.add_css('cubicweb.timetable.css') |
0 | 50 |
dates = {} |
51 |
users = [] |
|
52 |
users_max = {} |
|
53 |
# XXX: try refactoring with calendar.py:OneMonthCal |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
54 |
for row in xrange(self.cw_rset.rowcount): |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
55 |
task = self.cw_rset.get_entity(row, 0) |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
56 |
if len(self.cw_rset[row]) > 1: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
57 |
user = self.cw_rset.get_entity(row, 1) |
0 | 58 |
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
|
59 |
user = ALL_USERS |
0 | 60 |
the_dates = [] |
61 |
if task.start and task.stop: |
|
1555 | 62 |
if task.start.toordinal() == task.stop.toordinal(): |
0 | 63 |
the_dates.append(task.start) |
64 |
else: |
|
65 |
the_dates += date_range( task.start, task.stop ) |
|
66 |
elif task.start: |
|
67 |
the_dates.append(task.start) |
|
68 |
elif task.stop: |
|
69 |
the_dates.append(task.stop) |
|
70 |
for d in the_dates: |
|
3917
0f912c1cde28
[views] ticket #34808, make sure timetable view uses datetime and not date
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3914
diff
changeset
|
71 |
d = todatetime(d) |
0 | 72 |
d_users = dates.setdefault(d, {}) |
1132 | 73 |
u_tasks = d_users.setdefault(user, set()) |
0 | 74 |
u_tasks.add( task ) |
1132 | 75 |
task_max = users_max.setdefault(user, 0) |
0 | 76 |
if len(u_tasks)>task_max: |
77 |
users_max[user] = len(u_tasks) |
|
78 |
if user not in users: |
|
79 |
# keep original ordering |
|
80 |
users.append(user) |
|
81 |
if not dates: |
|
82 |
return |
|
83 |
date_min = min(dates) |
|
84 |
date_max = max(dates) |
|
85 |
#users = list(sorted(users, key=lambda u:u.login)) |
|
86 |
||
87 |
rows = [] |
|
88 |
# colors here are class names defined in cubicweb.css |
|
1132 | 89 |
colors = ["col%x" % i for i in xrange(12)] |
0 | 90 |
next_color_index = 0 |
91 |
||
92 |
visited_tasks = {} # holds a description of a task for a user |
|
93 |
task_colors = {} # remember a color assigned to a task |
|
94 |
for date in date_range(date_min, date_max): |
|
95 |
columns = [date] |
|
96 |
d_users = dates.get(date, {}) |
|
97 |
for user in users: |
|
98 |
# every user has its column "splitted" in at least MIN_COLS |
|
99 |
# sub-columns (for overlapping tasks) |
|
100 |
user_columns = [None] * max(MIN_COLS, users_max[user]) |
|
101 |
# every task that is "visited" for the first time |
|
102 |
# require a special treatment, so we put them in |
|
103 |
# 'postpone' |
|
104 |
postpone = [] |
|
105 |
for task in d_users.get(user, []): |
|
106 |
key = (task, user) |
|
107 |
if key in visited_tasks: |
|
108 |
task_descr = visited_tasks[ key ] |
|
109 |
user_columns[task_descr.column] = task_descr, False |
|
1133 | 110 |
task_descr.lines += 1 |
0 | 111 |
else: |
112 |
postpone.append(key) |
|
113 |
for key in postpone: |
|
114 |
# to every 'new' task we must affect a color |
|
115 |
# (which must be the same for every user concerned |
|
116 |
# by the task) |
|
117 |
task, user = key |
|
1132 | 118 |
for i, t in enumerate(user_columns): |
0 | 119 |
if t is None: |
120 |
if task in task_colors: |
|
121 |
color = task_colors[task] |
|
122 |
else: |
|
123 |
color = colors[next_color_index] |
|
124 |
next_color_index = (next_color_index+1)%len(colors) |
|
125 |
task_colors[task] = color |
|
126 |
task_descr = _TaskEntry(task, color, i) |
|
127 |
user_columns[i] = task_descr, True |
|
128 |
visited_tasks[key] = task_descr |
|
129 |
break |
|
130 |
else: |
|
131 |
raise RuntimeError("is it possible we got it wrong?") |
|
132 |
||
133 |
columns.append( user_columns ) |
|
134 |
rows.append( columns ) |
|
135 |
||
136 |
widths = [ len(col) for col in rows[0][1:] ] |
|
137 |
self.w(u'<div class="section">') |
|
138 |
if title: |
|
139 |
self.w(u'<h4>%s</h4>\n' % title) |
|
140 |
self.w(u'<table class="listing timetable">') |
|
141 |
self.render_col_headers(users, widths) |
|
142 |
self.render_rows(rows) |
|
143 |
self.w(u'</table>') |
|
144 |
self.w(u'</div>\n') |
|
145 |
||
1132 | 146 |
def render_col_headers(self, users, widths): |
0 | 147 |
""" render column headers """ |
148 |
self.w(u'<tr class="header">\n') |
|
149 |
||
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
150 |
self.w(u'<th class="ttdate"> </th>\n') |
0 | 151 |
columns = [] |
1132 | 152 |
for user, width in zip(users, widths): |
153 |
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
|
154 |
if user is ALL_USERS: |
3914
36be7a5e53ad
fix name error in timetable view, always write unicode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3829
diff
changeset
|
155 |
self.w(u'*') |
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
|
156 |
else: |
2471
3e2b50ece726
begin to drop ref to secondary view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
157 |
user.view('oneline', w=self.w) |
0 | 158 |
self.w(u'</th>') |
159 |
self.w(u'</tr>\n') |
|
160 |
return columns |
|
161 |
||
162 |
def render_rows(self, rows): |
|
163 |
""" render table content (row headers and central content) """ |
|
164 |
odd = False |
|
165 |
previous_is_empty = False |
|
166 |
for row in rows: |
|
167 |
date = row[0] |
|
168 |
empty_line = True |
|
169 |
for group in row[1:]: |
|
170 |
for value in group: |
|
171 |
if value: |
|
172 |
empty_line = False |
|
173 |
break |
|
174 |
else: |
|
175 |
continue |
|
176 |
break |
|
177 |
if empty_line and previous_is_empty: |
|
178 |
continue |
|
179 |
previous_is_empty = False |
|
180 |
||
181 |
klass = "even" |
|
1575
5eb02a4a4bcf
datetime fixes use weekday() instead of day_of_week
sylvain.thenault@logilab.fr
parents:
1555
diff
changeset
|
182 |
if date.weekday() in (5, 6) and not empty_line: |
0 | 183 |
klass = "odd" |
184 |
self.w(u'<tr class="%s">' % klass) |
|
185 |
odd = not odd |
|
186 |
||
187 |
if not empty_line: |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
188 |
self.w(u'<th class="ttdate">%s</th>' % self._cw.format_date(date) ) |
0 | 189 |
else: |
190 |
self.w(u'<th>...</th>' ) |
|
191 |
previous_is_empty = True |
|
192 |
||
193 |
empty_klasses = [ "ttle", "ttme", "ttre" ] |
|
194 |
filled_klasses = [ "ttlf", "ttmf", "ttrf" ] |
|
195 |
kj = 0 # 0: left, 1: mid, 2: right |
|
196 |
for uid, group in enumerate(row[1:]): |
|
197 |
for i, value in enumerate(group): |
|
198 |
if i == 0: |
|
199 |
kj = 0 |
|
200 |
elif i == len(group): |
|
201 |
kj = 2 |
|
202 |
else: |
|
203 |
kj = 1 |
|
204 |
if value: |
|
205 |
task_descr, first_row = value |
|
206 |
if first_row: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
207 |
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
|
208 |
self.w(u'<td rowspan="%d" class="%s %s" onclick="document.location=\'%s\'"> <div>' % ( |
0 | 209 |
task_descr.lines, task_descr.color, filled_klasses[kj], url)) |
210 |
task_descr.task.view('tooltip', w=self.w) |
|
211 |
self.w(u'</div></td>') |
|
212 |
else: |
|
213 |
if empty_line: |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
214 |
self.w(u'<td class="ttempty"> </td>') |
0 | 215 |
else: |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2471
diff
changeset
|
216 |
self.w(u'<td class="%s"> </td>' % empty_klasses[kj] ) |
0 | 217 |
self.w(u'</tr>\n') |