web/views/calendar.py
branchtls-sprint
changeset 1033 f5be65616a31
parent 1025 aebf37f460c1
child 1132 96752791c2b6
equal deleted inserted replaced
1032:25ec009daa95 1033:f5be65616a31
    19 
    19 
    20 _ = unicode
    20 _ = unicode
    21 
    21 
    22 # useful constants & functions ################################################
    22 # useful constants & functions ################################################
    23 
    23 
    24 ONEDAY = timedelta(1, 0)
    24 ONEDAY = timedelta(1)
    25 
    25 
    26 WEEKDAYS = (_("monday"), _("tuesday"), _("wednesday"), _("thursday"),
    26 WEEKDAYS = (_("monday"), _("tuesday"), _("wednesday"), _("thursday"),
    27             _("friday"), _("saturday"), _("sunday"))
    27             _("friday"), _("saturday"), _("sunday"))
    28 MONTHNAMES = ( _('january'), _('february'), _('march'), _('april'), _('may'),
    28 MONTHNAMES = ( _('january'), _('february'), _('march'), _('april'), _('may'),
    29                _('june'), _('july'), _('august'), _('september'), _('october'),
    29                _('june'), _('july'), _('august'), _('september'), _('october'),
   142             month = int(self.req.form['month'])
   142             month = int(self.req.form['month'])
   143         else:
   143         else:
   144             month = _today.month
   144             month = _today.month
   145 
   145 
   146         first_day_of_month = date(year, month, 1)
   146         first_day_of_month = date(year, month, 1)
   147         firstday = first_day_of_month - timedelta(first_day_of_month.weekday(), 0, 0)
   147         firstday = first_day_of_month - timedelta(first_day_of_month.weekday())
   148         if month >= 12:
   148         if month >= 12:
   149             last_day_of_month = date(year + 1, 1, 1) - timedelta(1, 0, 0)
   149             last_day_of_month = date(year + 1, 1, 1) - timedelta(1)
   150         else:
   150         else:
   151             last_day_of_month = date(year, month + 1, 1) - timedelta(1, 0, 0)
   151             last_day_of_month = date(year, month + 1, 1) - timedelta(1)
   152         lastday = last_day_of_month + timedelta(6 - last_day_of_month.weekday(), 0, 0)
   152         lastday = last_day_of_month + timedelta(6 - last_day_of_month.weekday())
   153         month_dates = list(date_range(firstday, lastday))
   153         month_dates = list(date_range(firstday, lastday))
   154         dates = {}
   154         dates = {}
   155         users = []
   155         users = []
   156         task_max = 0
   156         task_max = 0
   157         for row in xrange(self.rset.rowcount):
   157         for row in xrange(self.rset.rowcount):
   252             if mdate.weekday() == 6:
   252             if mdate.weekday() == 6:
   253                 self.w(u'</tr>')
   253                 self.w(u'</tr>')
   254         self.w(u'</table></div>')
   254         self.w(u'</table></div>')
   255 
   255 
   256     def _prevnext_links(self, curdate):
   256     def _prevnext_links(self, curdate):
   257         prevdate = curdate - timedelta(31, 0, 0)
   257         prevdate = curdate - timedelta(31)
   258         nextdate = curdate + timedelta(31, 0, 0)
   258         nextdate = curdate + timedelta(31)
   259         rql = self.rset.printable_rql()
   259         rql = self.rset.printable_rql()
   260         prevlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
   260         prevlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
   261                                     year=prevdate.year, month=prevdate.month)
   261                                     year=prevdate.year, month=prevdate.month)
   262         nextlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
   262         nextlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
   263                                     year=nextdate.year, month=nextdate.month)
   263                                     year=nextdate.year, month=nextdate.month)
   328         if 'week' in self.req.form:
   328         if 'week' in self.req.form:
   329             week = int(self.req.form['week'])
   329             week = int(self.req.form['week'])
   330         else:
   330         else:
   331             week = _today.isocalendar()[1]        
   331             week = _today.isocalendar()[1]        
   332         # week - 1 since we get week number > 0 while we want it to start from 0
   332         # week - 1 since we get week number > 0 while we want it to start from 0
   333         first_day_of_week = strptime('%s-%s-1' % (year, week - 1), '%Y-%U-%w')
   333         first_day_of_week = todate(strptime('%s-%s-1' % (year, week - 1), '%Y-%U-%w'))
   334         lastday = first_day_of_week + timedelta(6, 0, 0)
   334         lastday = first_day_of_week + timedelta(6)
   335         firstday = first_day_of_week
   335         firstday = first_day_of_week
   336         dates = [[] for i in range(7)]
   336         dates = [[] for i in range(7)]
   337         task_max = 0
   337         task_max = 0
   338         task_colors = {}   # remember a color assigned to a task
   338         task_colors = {}   # remember a color assigned to a task
   339         # colors here are class names defined in cubicweb.css
   339         # colors here are class names defined in cubicweb.css
   383                 html_escape(nextlink)))
   383                 html_escape(nextlink)))
   384 
   384 
   385         # output header
   385         # output header
   386         self.w(u'<tr>')
   386         self.w(u'<tr>')
   387         self.w(u'<th class="transparent"></th>') # column for hours
   387         self.w(u'<th class="transparent"></th>') # column for hours
   388         _today = today()
   388         _today = date.today()
   389         for i, day in enumerate(WEEKDAYS):
   389         for i, day in enumerate(WEEKDAYS):
   390             date = first_day_of_week + i
   390             wdate = first_day_of_week + timedelta(i)
   391             if date.isocalendar() == _today.isocalendar():
   391             if wdate.isocalendar() == _today.isocalendar():
   392                 self.w(u'<th class="today">%s<br/>%s</th>' % (self.req._(day), self.format_date(date)))
   392                 self.w(u'<th class="today">%s<br/>%s</th>' % (self.req._(day), self.format_date(wdate)))
   393             else:
   393             else:
   394                 self.w(u'<th>%s<br/>%s</th>' % (self.req._(day), self.format_date(date)))
   394                 self.w(u'<th>%s<br/>%s</th>' % (self.req._(day), self.format_date(wdate)))
   395         self.w(u'</tr>')
   395         self.w(u'</tr>')
   396         
   396         
   397         # build week calendar
   397         # build week calendar
   398         self.w(u'<tr>')
   398         self.w(u'<tr>')
   399         self.w(u'<td style="width:5em;">') # column for hours
   399         self.w(u'<td style="width:5em;">') # column for hours
   403             self.w(u'%02d:00'%h)
   403             self.w(u'%02d:00'%h)
   404             self.w(u'</div>')            
   404             self.w(u'</div>')            
   405         self.w(u'</td>')
   405         self.w(u'</td>')
   406         
   406         
   407         for i, day in enumerate(WEEKDAYS):
   407         for i, day in enumerate(WEEKDAYS):
   408             date = first_day_of_week + i
   408             wdate = first_day_of_week + timedelta(i)
   409             classes = ""
   409             classes = ""
   410             if date.isocalendar() == _today.isocalendar():
   410             if wdate.isocalendar() == _today.isocalendar():
   411                 classes = " today"
   411                 classes = " today"
   412             self.w(u'<td class="column %s" id="%s">'%(classes, day))
   412             self.w(u'<td class="column %s" id="%s">' % (classes, day))
   413             if len(self.rset.column_types(0)) == 1:
   413             if len(self.rset.column_types(0)) == 1:
   414                 etype = list(self.rset.column_types(0))[0]
   414                 etype = list(self.rset.column_types(0))[0]
   415                 url = self.build_url(vid='creation', etype=etype,
   415                 url = self.build_url(vid='creation', etype=etype,
   416                                      schedule=True,
   416                                      schedule=True,
   417                                      __redirectrql=self.rset.printable_rql(),
   417                                      __redirectrql=self.rset.printable_rql(),
   418                                      __redirectparams=self.req.build_url_params(year=year, week=week),
   418                                      __redirectparams=self.req.build_url_params(year=year, week=week),
   419                                      __redirectvid=self.id
   419                                      __redirectvid=self.id
   420                                      )
   420                                      )
   421                 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))
   421                 extra = ' ondblclick="addCalendarItem(event, hmin=8, hmax=20, year=%s, month=%s, day=%s, duration=2, baseurl=\'%s\')"' % (
       
   422                     wdate.year, wdate.month, wdate.day, html_escape(url))
   422             else:
   423             else:
   423                 extra = ""
   424                 extra = ""
   424             self.w(u'<div class="columndiv"%s>'% extra)
   425             self.w(u'<div class="columndiv"%s>'% extra)
   425             for h in range(8, 20):
   426             for h in range(8, 20):
   426                 self.w(u'<div class="hourline" style="top:%sex;">'%((h-7)*8))
   427                 self.w(u'<div class="hourline" style="top:%sex;">'%((h-7)*8))
   427                 self.w(u'</div>')            
   428                 self.w(u'</div>')            
   428             if dates[i]:
   429             if dates[i]:
   429                 self._build_calendar_cell(date, dates[i])
   430                 self._build_calendar_cell(wdate, dates[i])
   430             self.w(u'</div>')
   431             self.w(u'</div>')
   431             self.w(u'</td>')
   432             self.w(u'</td>')
   432         self.w(u'</tr>')
   433         self.w(u'</tr>')
   433         self.w(u'</table></div>')
   434         self.w(u'</table></div>')
   434         self.w(u'<div id="coord"></div>')
   435         self.w(u'<div id="coord"></div>')
   514                 self.w(u'</div>')
   515                 self.w(u'</div>')
   515             self.w(u'</div>')
   516             self.w(u'</div>')
   516 
   517 
   517             
   518             
   518     def _prevnext_links(self, curdate):
   519     def _prevnext_links(self, curdate):
   519         prevdate = curdate - timedelta(7, 0, 0)
   520         prevdate = curdate - timedelta(7)
   520         nextdate = curdate + timedelta(7, 0, 0)
   521         nextdate = curdate + timedelta(7)
   521         rql = self.rset.printable_rql()
   522         rql = self.rset.printable_rql()
   522         prevlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
   523         prevlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
   523                                     year=prevdate.year, week=prevdate.isocalendar()[1])
   524                                     year=prevdate.year, week=prevdate.isocalendar()[1])
   524         nextlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
   525         nextlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
   525                                     year=nextdate.year, week=nextdate.isocalendar()[1])
   526                                     year=nextdate.year, week=nextdate.isocalendar()[1])