web/views/timetable.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """html timetable views"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 from cubicweb import _
       
    22 
       
    23 from six.moves import range
       
    24 
       
    25 from logilab.mtconverter import xml_escape
       
    26 from logilab.common.date import ONEDAY, date_range, todatetime
       
    27 
       
    28 from cubicweb.predicates import adaptable
       
    29 from cubicweb.view import EntityView
       
    30 
       
    31 
       
    32 class _TaskEntry(object):
       
    33     def __init__(self, task, color, column):
       
    34         self.task = task
       
    35         self.color = color
       
    36         self.column = column
       
    37         self.lines = 1
       
    38 
       
    39 MIN_COLS = 3  # minimum number of task columns for a single user
       
    40 ALL_USERS = object()
       
    41 
       
    42 class TimeTableView(EntityView):
       
    43     __regid__ = 'timetable'
       
    44     title = _('timetable')
       
    45     __select__ = adaptable('ICalendarable')
       
    46     paginable = False
       
    47 
       
    48     def call(self, title=None):
       
    49         """Dumps a timetable from a resultset composed of a note (anything
       
    50         with start/stop) and a user (anything)"""
       
    51         self._cw.add_css('cubicweb.timetable.css')
       
    52         dates = {}
       
    53         users = []
       
    54         users_max = {}
       
    55         # XXX: try refactoring with calendar.py:OneMonthCal
       
    56         for row in range(self.cw_rset.rowcount):
       
    57             task = self.cw_rset.get_entity(row, 0)
       
    58             icalendarable = task.cw_adapt_to('ICalendarable')
       
    59             if len(self.cw_rset[row]) > 1 and self.cw_rset.description[row][1] == 'CWUser':
       
    60                 user = self.cw_rset.get_entity(row, 1)
       
    61             else:
       
    62                 user = ALL_USERS
       
    63             the_dates = []
       
    64             if icalendarable.start and icalendarable.stop:
       
    65                 if icalendarable.start.toordinal() == icalendarable.stop.toordinal():
       
    66                     the_dates.append(icalendarable.start)
       
    67                 else:
       
    68                     the_dates += date_range(icalendarable.start,
       
    69                                             icalendarable.stop + ONEDAY)
       
    70             elif icalendarable.start:
       
    71                 the_dates.append(icalendarable.start)
       
    72             elif icalendarable.stop:
       
    73                 the_dates.append(icalendarable.stop)
       
    74             for d in the_dates:
       
    75                 d = todatetime(d)
       
    76                 d_users = dates.setdefault(d, {})
       
    77                 u_tasks = d_users.setdefault(user, set())
       
    78                 u_tasks.add( task )
       
    79                 task_max = users_max.setdefault(user, 0)
       
    80                 if len(u_tasks)>task_max:
       
    81                     users_max[user] = len(u_tasks)
       
    82             if user not in users:
       
    83                 # keep original ordering
       
    84                 users.append(user)
       
    85         if not dates:
       
    86             return
       
    87         date_min = min(dates)
       
    88         date_max = max(dates)
       
    89         #users = list(sorted(users, key=lambda u:u.login))
       
    90 
       
    91         rows = []
       
    92         # colors here are class names defined in cubicweb.css
       
    93         colors = ["col%x" % i for i in range(12)]
       
    94         next_color_index = 0
       
    95 
       
    96         visited_tasks = {} # holds a description of a task for a user
       
    97         task_colors = {}   # remember a color assigned to a task
       
    98         for date in date_range(date_min, date_max + ONEDAY):
       
    99             columns = [date]
       
   100             d_users = dates.get(date, {})
       
   101             for user in users:
       
   102                 # every user has its column "splitted" in at least MIN_COLS
       
   103                 # sub-columns (for overlapping tasks)
       
   104                 user_columns = [None] * max(MIN_COLS, users_max[user])
       
   105                 # every task that is "visited" for the first time
       
   106                 # require a special treatment, so we put them in
       
   107                 # 'postpone'
       
   108                 postpone = []
       
   109                 for task in d_users.get(user, []):
       
   110                     key = (task, user)
       
   111                     if key in visited_tasks:
       
   112                         task_descr = visited_tasks[ key ]
       
   113                         user_columns[task_descr.column] = task_descr, False
       
   114                         task_descr.lines += 1
       
   115                     else:
       
   116                         postpone.append(key)
       
   117                 for key in postpone:
       
   118                     # to every 'new' task we must affect a color
       
   119                     # (which must be the same for every user concerned
       
   120                     # by the task)
       
   121                     task, user = key
       
   122                     for i, t in enumerate(user_columns):
       
   123                         if t is None:
       
   124                             if task in task_colors:
       
   125                                 color = task_colors[task]
       
   126                             else:
       
   127                                 color = colors[next_color_index]
       
   128                                 next_color_index = (next_color_index+1)%len(colors)
       
   129                                 task_colors[task] = color
       
   130                             task_descr = _TaskEntry(task, color, i)
       
   131                             user_columns[i] = task_descr, True
       
   132                             visited_tasks[key] = task_descr
       
   133                             break
       
   134                     else:
       
   135                         raise RuntimeError("is it possible we got it wrong?")
       
   136 
       
   137                 columns.append( user_columns )
       
   138             rows.append( columns )
       
   139 
       
   140         widths = [ len(col) for col in rows[0][1:] ]
       
   141         self.w(u'<div class="section">')
       
   142         if title:
       
   143             self.w(u'<h4>%s</h4>\n' % title)
       
   144         self.w(u'<table class="listing timetable">')
       
   145         self.render_col_headers(users, widths)
       
   146         self.render_rows(rows)
       
   147         self.w(u'</table>')
       
   148         self.w(u'</div>\n')
       
   149 
       
   150     def render_col_headers(self, users, widths):
       
   151         """ render column headers """
       
   152         self.w(u'<tr class="header">\n')
       
   153 
       
   154         self.w(u'<th class="ttdate">&#160;</th>\n')
       
   155         columns = []
       
   156         for user, width in zip(users, widths):
       
   157             self.w(u'<th colspan="%s">' % max(MIN_COLS, width))
       
   158             if user is ALL_USERS:
       
   159                 self.w(u'*')
       
   160             else:
       
   161                 user.view('oneline', w=self.w)
       
   162             self.w(u'</th>')
       
   163         self.w(u'</tr>\n')
       
   164         return columns
       
   165 
       
   166     def render_rows(self, rows):
       
   167         """ render table content (row headers and central content) """
       
   168         odd = False
       
   169         previous_is_empty = False
       
   170         for row in rows:
       
   171             date = row[0]
       
   172             empty_line = True
       
   173             for group in row[1:]:
       
   174                 for value in group:
       
   175                     if value:
       
   176                         empty_line = False
       
   177                         break
       
   178                 else:
       
   179                     continue
       
   180                 break
       
   181             if empty_line and previous_is_empty:
       
   182                 continue
       
   183             previous_is_empty = False
       
   184 
       
   185             klass = "even"
       
   186             if date.weekday() in (5, 6) and not empty_line:
       
   187                 klass = "odd"
       
   188             self.w(u'<tr class="%s">' % klass)
       
   189             odd = not odd
       
   190 
       
   191             if not empty_line:
       
   192                 self.w(u'<th class="ttdate">%s</th>' % self._cw.format_date(date) )
       
   193             else:
       
   194                 self.w(u'<th>...</th>'  )
       
   195                 previous_is_empty = True
       
   196 
       
   197             empty_klasses = [ "ttle", "ttme", "ttre" ]
       
   198             filled_klasses = [ "ttlf", "ttmf", "ttrf" ]
       
   199             kj = 0 # 0: left, 1: mid, 2: right
       
   200             for uid, group in enumerate(row[1:]):
       
   201                 for i, value in enumerate(group):
       
   202                     if i == 0:
       
   203                         kj = 0
       
   204                     elif i == len(group):
       
   205                         kj = 2
       
   206                     else:
       
   207                         kj = 1
       
   208                     if value:
       
   209                         task_descr, first_row = value
       
   210                         if first_row:
       
   211                             url = xml_escape(task_descr.task.absolute_url(vid="edition"))
       
   212                             self.w(u'<td rowspan="%d" class="%s %s" onclick="document.location=\'%s\'">&#160;<div>' % (
       
   213                                 task_descr.lines, task_descr.color, filled_klasses[kj], url))
       
   214                             task_descr.task.view('tooltip', w=self.w)
       
   215                             self.w(u'</div></td>')
       
   216                     else:
       
   217                         if empty_line:
       
   218                             self.w(u'<td class="ttempty">&#160;</td>')
       
   219                         else:
       
   220                             self.w(u'<td class="%s">&#160;</td>' % empty_klasses[kj] )
       
   221             self.w(u'</tr>\n')