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