web/views/old_calendar.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 17 Aug 2010 15:22:04 +0200
branchstable
changeset 6113 dbe17a3ebb9f
parent 5922 59dfda8e232c
child 6582 8eb7883b4223
permissions -rw-r--r--
[primary view] consider rtypevid flag for relations in the 'relations' section as well
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""html calendar views"""
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    19
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    20
from datetime import date, time, timedelta
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    21
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    22
from logilab.mtconverter import xml_escape
4466
8b0ca7904820 moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    23
from logilab.common.date import (ONEDAY, ONEWEEK, days_in_month, previous_month,
8b0ca7904820 moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    24
                                 next_month, first_day, last_day, date_range)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    25
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    26
from cubicweb.interfaces import ICalendarViews
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    27
from cubicweb.selectors import implements, adaptable
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    28
from cubicweb.view import EntityView, EntityAdapter, implements_adapter_compat
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    29
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    30
class ICalendarViewsAdapter(EntityAdapter):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    31
    """calendar views interface"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    32
    __regid__ = 'ICalendarViews'
5895
6a3f776292a5 [selectors] avoid spurious warning when using implements by design
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
    33
    __select__ = implements(ICalendarViews, warn=False) # XXX for bw compat, should be abstract
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    34
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    35
    @implements_adapter_compat('ICalendarViews')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    36
    def matching_dates(self, begin, end):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    37
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    38
        :param begin: day considered as begin of the range (`DateTime`)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    39
        :param end: day considered as end of the range (`DateTime`)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    40
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    41
        :return:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    42
          a list of dates (`DateTime`) in the range [`begin`, `end`] on which
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    43
          this entity apply
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    44
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    45
        raise NotImplementedError
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    46
5922
59dfda8e232c [interface deprecation] fix bad comment
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5895
diff changeset
    47
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    48
# used by i18n tools
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    49
WEEKDAYS = [_("monday"), _("tuesday"), _("wednesday"), _("thursday"),
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    50
            _("friday"), _("saturday"), _("sunday")]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    51
MONTHNAMES = [ _('january'), _('february'), _('march'), _('april'), _('may'),
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    52
               _('june'), _('july'), _('august'), _('september'), _('october'),
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    53
               _('november'), _('december')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    54
               ]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    55
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    56
class _CalendarView(EntityView):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    57
    """base calendar view containing helpful methods to build calendar views"""
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    58
    __select__ = adaptable('ICalendarViews')
4244
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    59
    paginable = False
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    60
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    61
    # Navigation building methods / views ####################################
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    62
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
    63
    PREV = u'<a href="%s">&lt;&lt;</a>&#160;&#160;<a href="%s">&lt;</a>'
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
    64
    NEXT = u'<a href="%s">&gt;</a>&#160;&#160;<a href="%s">&gt;&gt;</a>'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    65
    NAV_HEADER = u"""<table class="calendarPageHeader">
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    66
<tr><td class="prev">%s</td><td class="next">%s</td></tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    67
</table>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    68
""" % (PREV, NEXT)
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    69
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    70
    def nav_header(self, date, smallshift=3, bigshift=9):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    71
        """prints shortcut links to go to previous/next steps (month|week)"""
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    72
        prev1 = previous_month(date, smallshift)
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    73
        next1 = next_month(date, smallshift)
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    74
        prev2 = previous_month(date, bigshift)
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    75
        next2 = next_month(date, bigshift)
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    76
        rql = self.cw_rset.printable_rql()
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    77
        return self.NAV_HEADER % (
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    78
            xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=prev2.year,
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    79
                                          month=prev2.month)),
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    80
            xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=prev1.year,
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    81
                                          month=prev1.month)),
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    82
            xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=next1.year,
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    83
                                          month=next1.month)),
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    84
            xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=next2.year,
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
    85
                                          month=next2.month)))
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    86
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    87
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    88
    # Calendar building methods ##############################################
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    89
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    90
    def build_calendars(self, schedule, begin, end):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    91
        """build several HTML calendars at once, one for each month
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    92
        between begin and end
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    93
        """
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    94
        return [self.build_calendar(schedule, date)
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
    95
                for date in date_range(begin, end, incmonth=1)]
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
    96
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    97
    def build_calendar(self, schedule, first_day):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    98
        """method responsible for building *one* HTML calendar"""
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
    99
        # FIXME  iterates between [first_day-first_day.day_of_week ;
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   100
        #                          last_day+6-last_day.day_of_week]
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   101
        umonth = self._cw.format_date(first_day, '%B %Y') # localized month name
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   102
        rows = []
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   103
        current_row = [NO_CELL] * first_day.weekday()
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   104
        for daynum in xrange(0, days_in_month(first_day)):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   105
            # build cell day
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   106
            day = first_day + timedelta(daynum)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   107
            events = schedule.get(day)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   108
            if events:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   109
                events = [u'\n'.join(event) for event in events.values()]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   110
                current_row.append(CELL % (daynum+1, '\n'.join(events)))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   111
            else:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   112
                current_row.append(EMPTY_CELL % (daynum+1))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   113
            # store & reset current row on Sundays
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   114
            if day.weekday() == 6:
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   115
                rows.append(u'<tr>%s%s</tr>' % (WEEKNUM_CELL % day.isocalendar()[1], ''.join(current_row)))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   116
                current_row = []
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   117
        current_row.extend([NO_CELL] * (6-day.weekday()))
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   118
        rql = self.cw_rset.printable_rql()
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   119
        if day.weekday() != 6:
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   120
            rows.append(u'<tr>%s%s</tr>' % (WEEKNUM_CELL % day.isocalendar()[1], ''.join(current_row)))
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   121
        url = self._cw.build_url(rql=rql, vid='calendarmonth',
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   122
                                 year=first_day.year, month=first_day.month)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   123
        monthlink = u'<a href="%s">%s</a>' % (xml_escape(url), umonth)
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   124
        return CALENDAR(self._cw) % (monthlink, '\n'.join(rows))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   125
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   126
    def _mk_schedule(self, begin, end, itemvid='calendaritem'):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   127
        """private method that gathers information from resultset
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   128
        and builds calendars according to it
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   129
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   130
        :param begin: begin of date range
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   131
        :param end: end of date rangs
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   132
        :param itemvid: which view to call to render elements in cells
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   133
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   134
        returns { day1 : { hour : [views] },
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   135
                  day2 : { hour : [views] } ... }
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   136
        """
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   137
        # put this here since all sub views are calling this method
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   138
        self._cw.add_css('cubicweb.calendar.css')
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   139
        schedule = {}
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   140
        for row in xrange(len(self.cw_rset.rows)):
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   141
            entity = self.cw_rset.get_entity(row, 0)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   142
            infos = u'<div class="event">'
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   143
            infos += self._cw.view(itemvid, self.cw_rset, row=row)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   144
            infos += u'</div>'
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   145
            for date_ in entity.cw_adapt_to('ICalendarViews').matching_dates(begin, end):
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   146
                day = date(date_.year, date_.month, date_.day)
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   147
                try:
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   148
                    dt = time(date_.hour, date_.minute, date_.second)
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   149
                except AttributeError:
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   150
                    # date instance
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   151
                    dt = time(0, 0, 0)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   152
                schedule.setdefault(day, {})
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   153
                schedule[day].setdefault(dt, []).append(infos)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   154
        return schedule
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   155
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   156
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   157
    @staticmethod
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   158
    def get_date_range(day, shift=4):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   159
        """returns a couple (begin, end)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   160
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   161
        <begin> is the first day of current_month - shift
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   162
        <end> is the last day of current_month + (shift+1)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   163
        """
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   164
        begin = first_day(previous_month(day, shift))
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   165
        end = last_day(next_month(day, shift))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   166
        return begin, end
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   167
4722
9c13d5db03d9 pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   168
    def _build_ampm_cells(self, events):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   169
        """create a view without any hourly details.
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   170
4722
9c13d5db03d9 pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   171
        :param events: dictionnary with all events classified by hours
9c13d5db03d9 pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   172
        """
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   173
        # split events according am/pm
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   174
        am_events = [event for e_time, e_list in events.iteritems()
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   175
                     if 0 <= e_time.hour < 12
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   176
                     for event in e_list]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   177
        pm_events = [event for e_time, e_list in events.iteritems()
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   178
                     if 12 <= e_time.hour < 24
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   179
                     for event in e_list]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   180
        # format each am/pm cell
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   181
        if am_events:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   182
            am_content = AMPM_CONTENT % ("amCell", "am", '\n'.join(am_events))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   183
        else:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   184
            am_content = AMPM_EMPTY % ("amCell", "am")
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   185
        if pm_events:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   186
            pm_content = AMPM_CONTENT % ("pmCell", "pm", '\n'.join(pm_events))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   187
        else:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   188
            pm_content = AMPM_EMPTY % ("pmCell", "pm")
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   189
        return am_content, pm_content
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   190
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   191
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   192
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   193
class YearCalendarView(_CalendarView):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   194
    __regid__ = 'calendaryear'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   195
    title = _('calendar (year)')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   196
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   197
    def call(self, year=None, month=None):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   198
        """this view renders a 3x3 calendars' table"""
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   199
        year = year or int(self._cw.form.get('year', date.today().year))
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   200
        month = month or int(self._cw.form.get('month', date.today().month))
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   201
        center_date = date(year, month, 1)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   202
        begin, end = self.get_date_range(day=center_date)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   203
        schedule = self._mk_schedule(begin, end)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   204
        self.w(self.nav_header(center_date))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   205
        calendars = tuple(self.build_calendars(schedule, begin, end))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   206
        self.w(SMALL_CALENDARS_PAGE % calendars)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   207
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   208
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   209
class SemesterCalendarView(_CalendarView):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   210
    """this view renders three semesters as three rows of six columns,
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   211
    one column per month
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   212
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   213
    __regid__ = 'calendarsemester'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   214
    title = _('calendar (semester)')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   215
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   216
    def call(self, year=None, month=None):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   217
        year = year or int(self._cw.form.get('year', date.today().year))
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   218
        month = month or int(self._cw.form.get('month', date.today().month))
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   219
        begin = previous_month(date(year, month, 1), 2)
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   220
        end = next_month(date(year, month, 1), 3)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   221
        schedule = self._mk_schedule(begin, end)
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   222
        self.w(self.nav_header(date(year, month, 1), 1, 6))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   223
        self.w(u'<table class="semesterCalendar">')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   224
        self.build_calendars(schedule, begin, end)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   225
        self.w(u'</table>')
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   226
        self.w(self.nav_header(date(year, month, 1), 1, 6))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   227
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   228
    def build_calendars(self, schedule, begin, end):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   229
        self.w(u'<tr>')
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   230
        rql = self.cw_rset.printable_rql()
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   231
        for cur_month in date_range(begin, end, incmonth=1):
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   232
            umonth = u'%s&#160;%s' % (self._cw.format_date(cur_month, '%B'), cur_month.year)
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   233
            url = self._cw.build_url(rql=rql, vid=self.__regid__,
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   234
                                     year=cur_month.year, month=cur_month.month)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   235
            self.w(u'<th colspan="2"><a href="%s">%s</a></th>' % (xml_escape(url),
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   236
                                                                  umonth))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   237
        self.w(u'</tr>')
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   238
        _ = self._cw._
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   239
        for day_num in xrange(31):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   240
            self.w(u'<tr>')
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   241
            for cur_month in date_range(begin, end, incmonth=1):
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   242
                if day_num >= days_in_month(cur_month):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   243
                    self.w(u'%s%s' % (NO_CELL, NO_CELL))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   244
                else:
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   245
                    day = date(cur_month.year, cur_month.month, day_num+1)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   246
                    events = schedule.get(day)
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   247
                    self.w(u'<td>%s&#160;%s</td>\n' % (_(WEEKDAYS[day.weekday()])[0].upper(), day_num+1))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   248
                    self.format_day_events(day, events)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   249
            self.w(u'</tr>')
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   250
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   251
    def format_day_events(self, day, events):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   252
        if events:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   253
            events = ['\n'.join(event) for event in events.values()]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   254
            self.w(WEEK_CELL % '\n'.join(events))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   255
        else:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   256
            self.w(WEEK_EMPTY_CELL)
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   257
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   258
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   259
class MonthCalendarView(_CalendarView):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   260
    """this view renders a 3x1 calendars' table"""
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   261
    __regid__ = 'calendarmonth'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   262
    title = _('calendar (month)')
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   263
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   264
    def call(self, year=None, month=None):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   265
        year = year or int(self._cw.form.get('year', date.today().year))
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   266
        month = month or int(self._cw.form.get('month', date.today().month))
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   267
        center_date = date(year, month, 1)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   268
        begin, end = self.get_date_range(day=center_date, shift=1)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   269
        schedule = self._mk_schedule(begin, end)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   270
        calendars = self.build_calendars(schedule, begin, end)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   271
        self.w(self.nav_header(center_date, 1, 3))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   272
        self.w(BIG_CALENDARS_PAGE % tuple(calendars))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   273
        self.w(self.nav_header(center_date, 1, 3))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   274
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   275
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   276
class WeekCalendarView(_CalendarView):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   277
    """this view renders a calendar for week events"""
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   278
    __regid__ = 'calendarweek'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   279
    title = _('calendar (week)')
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   280
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   281
    def call(self, year=None, week=None):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   282
        year = year or int(self._cw.form.get('year', date.today().year))
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   283
        week = week or int(self._cw.form.get('week', date.today().isocalendar()[1]))
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   284
        day0 = date(year, 1, 1)
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   285
        first_day_of_week = day0 - day0.weekday()*ONEDAY + ONEWEEK
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   286
        begin, end = first_day_of_week- ONEWEEK, first_day_of_week + 2*ONEWEEK
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   287
        schedule = self._mk_schedule(begin, end, itemvid='calendarlargeitem')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   288
        self.w(self.nav_header(first_day_of_week))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   289
        self.w(u'<table class="weekCalendar">')
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   290
        _weeks = [(first_day_of_week-ONEWEEK, first_day_of_week-ONEDAY),
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   291
                  (first_day_of_week, first_day_of_week+6*ONEDAY),
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   292
                  (first_day_of_week+ONEWEEK, first_day_of_week+13*ONEDAY)]
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   293
        self.build_calendar(schedule, _weeks)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   294
        self.w(u'</table>')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   295
        self.w(self.nav_header(first_day_of_week))
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   296
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   297
    def build_calendar(self, schedule, weeks):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   298
        rql = self.cw_rset.printable_rql()
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   299
        _ = self._cw._
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   300
        for monday, sunday in weeks:
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   301
            umonth = self._cw.format_date(monday, '%B %Y')
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   302
            url = self._cw.build_url(rql=rql, vid='calendarmonth',
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   303
                                     year=monday.year, month=monday.month)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   304
            monthlink = '<a href="%s">%s</a>' % (xml_escape(url), umonth)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   305
            self.w(u'<tr><th colspan="3">%s %s (%s)</th></tr>' \
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   306
                  % (_('week'), monday.isocalendar()[1], monthlink))
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   307
            for day in date_range(monday, sunday+ONEDAY):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   308
                self.w(u'<tr>')
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   309
                self.w(u'<td>%s</td>' % _(WEEKDAYS[day.weekday()]))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   310
                self.w(u'<td>%s</td>' % (day.strftime('%Y-%m-%d')))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   311
                events = schedule.get(day)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   312
                if events:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   313
                    events = ['\n'.join(event) for event in events.values()]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   314
                    self.w(WEEK_CELL % '\n'.join(events))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   315
                else:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   316
                    self.w(WEEK_EMPTY_CELL)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   317
                self.w(u'</tr>')
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   318
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   319
    def nav_header(self, date, smallshift=1, bigshift=3):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   320
        """prints shortcut links to go to previous/next steps (month|week)"""
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   321
        prev1 = date - ONEWEEK * smallshift
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   322
        prev2 = date - ONEWEEK * bigshift
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   323
        next1 = date + ONEWEEK * smallshift
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   324
        next2 = date + ONEWEEK * bigshift
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   325
        rql = self.cw_rset.printable_rql()
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   326
        return self.NAV_HEADER % (
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   327
            xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=prev2.year, week=prev2.isocalendar()[1])),
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   328
            xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=prev1.year, week=prev1.isocalendar()[1])),
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   329
            xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=next1.year, week=next1.isocalendar()[1])),
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   330
            xml_escape(self._cw.build_url(rql=rql, vid=self.__regid__, year=next2.year, week=next2.isocalendar()[1])))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   331
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   332
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   333
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   334
class AMPMYearCalendarView(YearCalendarView):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   335
    __regid__ = 'ampmcalendaryear'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   336
    title = _('am/pm calendar (year)')
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   337
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   338
    def build_calendar(self, schedule, first_day):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   339
        """method responsible for building *one* HTML calendar"""
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   340
        umonth = self._cw.format_date(first_day, '%B %Y') # localized month name
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   341
        rows = [] # each row is: (am,pm), (am,pm) ... week_title
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   342
        current_row = [(NO_CELL, NO_CELL, NO_CELL)] * first_day.weekday()
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   343
        rql = self.cw_rset.printable_rql()
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   344
        for daynum in xrange(0, days_in_month(first_day)):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   345
            # build cells day
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   346
            day = first_day + timedelta(daynum)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   347
            events = schedule.get(day)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   348
            if events:
4722
9c13d5db03d9 pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   349
                current_row.append((AMPM_DAY % (daynum+1),) + self._build_ampm_cells(events))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   350
            else:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   351
                current_row.append((AMPM_DAY % (daynum+1),
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   352
                                    AMPM_EMPTY % ("amCell", "am"),
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   353
                                    AMPM_EMPTY % ("pmCell", "pm")))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   354
            # store & reset current row on Sundays
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   355
            if day.weekday() == 6:
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   356
                url = self._cw.build_url(rql=rql, vid='ampmcalendarweek',
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   357
                                     year=day.year, week=day.isocalendar()[1])
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   358
                weeklink = '<a href="%s">%s</a>' % (xml_escape(url),
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   359
                                                    day.isocalendar()[1])
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   360
                current_row.append(WEEKNUM_CELL % weeklink)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   361
                rows.append(current_row)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   362
                current_row = []
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   363
        current_row.extend([(NO_CELL, NO_CELL, NO_CELL)] * (6-day.weekday()))
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   364
        url = self._cw.build_url(rql=rql, vid='ampmcalendarweek',
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   365
                             year=day.year, week=day.isocalendar()[1])
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   366
        weeklink = '<a href="%s">%s</a>' % (xml_escape(url), day.isocalendar()[1])
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   367
        current_row.append(WEEKNUM_CELL % weeklink)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   368
        rows.append(current_row)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   369
        # build two rows for each week: am & pm
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   370
        formatted_rows = []
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   371
        for row in rows:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   372
            week_title = row.pop()
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   373
            day_row = [day for day, am, pm in row]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   374
            am_row = [am for day, am, pm in row]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   375
            pm_row = [pm for day, am, pm in row]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   376
            formatted_rows.append('<tr>%s%s</tr>'% (week_title, '\n'.join(day_row)))
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   377
            formatted_rows.append('<tr class="amRow"><td>&#160;</td>%s</tr>'% '\n'.join(am_row))
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   378
            formatted_rows.append('<tr class="pmRow"><td>&#160;</td>%s</tr>'% '\n'.join(pm_row))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   379
        # tigh everything together
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   380
        url = self._cw.build_url(rql=rql, vid='ampmcalendarmonth',
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   381
                             year=first_day.year, month=first_day.month)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   382
        monthlink = '<a href="%s">%s</a>' % (xml_escape(url), umonth)
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   383
        return CALENDAR(self._cw) % (monthlink, '\n'.join(formatted_rows))
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   384
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   385
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   386
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   387
class AMPMSemesterCalendarView(SemesterCalendarView):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   388
    """this view renders a 3x1 calendars' table"""
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   389
    __regid__ = 'ampmcalendarsemester'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   390
    title = _('am/pm calendar (semester)')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   391
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   392
    def build_calendars(self, schedule, begin, end):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   393
        self.w(u'<tr>')
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   394
        rql = self.cw_rset.printable_rql()
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   395
        for cur_month in date_range(begin, end, incmonth=1):
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   396
            umonth = u'%s&#160;%s' % (self._cw.format_date(cur_month, '%B'), cur_month.year)
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   397
            url = self._cw.build_url(rql=rql, vid=self.__regid__,
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   398
                                 year=cur_month.year, month=cur_month.month)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   399
            self.w(u'<th colspan="3"><a href="%s">%s</a></th>' % (xml_escape(url),
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   400
                                                                  umonth))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   401
        self.w(u'</tr>')
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   402
        _ = self._cw._
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   403
        for day_num in xrange(31):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   404
            self.w(u'<tr>')
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   405
            for cur_month in date_range(begin, end, incmonth=1):
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   406
                if day_num >= days_in_month(cur_month):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   407
                    self.w(u'%s%s%s' % (NO_CELL, NO_CELL, NO_CELL))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   408
                else:
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   409
                    day = date(cur_month.year, cur_month.month, day_num+1)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   410
                    events = schedule.get(day)
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   411
                    self.w(u'<td>%s&#160;%s</td>\n' % (_(WEEKDAYS[day.weekday()])[0].upper(),
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   412
                                                       day_num+1))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   413
                    self.format_day_events(day, events)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   414
            self.w(u'</tr>')
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   415
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   416
    def format_day_events(self, day, events):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   417
        if events:
4722
9c13d5db03d9 pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   418
            self.w(u'\n'.join(self._build_ampm_cells(events)))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   419
        else:
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   420
            self.w(u'%s %s'% (AMPM_EMPTY % ("amCell", "am"),
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   421
                              AMPM_EMPTY % ("pmCell", "pm")))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   422
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   423
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   424
class AMPMMonthCalendarView(MonthCalendarView):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   425
    """this view renders a 3x1 calendars' table"""
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   426
    __regid__ = 'ampmcalendarmonth'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   427
    title = _('am/pm calendar (month)')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   428
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   429
    def build_calendar(self, schedule, first_day):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   430
        """method responsible for building *one* HTML calendar"""
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   431
        umonth = self._cw.format_date(first_day, '%B %Y') # localized month name
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   432
        rows = [] # each row is: (am,pm), (am,pm) ... week_title
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   433
        current_row = [(NO_CELL, NO_CELL, NO_CELL)] * first_day.weekday()
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   434
        rql = self.cw_rset.printable_rql()
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   435
        for daynum in xrange(0, days_in_month(first_day)):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   436
            # build cells day
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   437
            day = first_day + timedelta(daynum)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   438
            events = schedule.get(day)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   439
            if events:
4722
9c13d5db03d9 pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   440
                current_row.append((AMPM_DAY % (daynum+1),) + self._build_ampm_cells(events))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   441
            else:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   442
                current_row.append((AMPM_DAY % (daynum+1),
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   443
                                    AMPM_EMPTY % ("amCell", "am"),
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   444
                                    AMPM_EMPTY % ("pmCell", "pm")))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   445
            # store & reset current row on Sundays
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   446
            if day.weekday() == 6:
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   447
                url = self._cw.build_url(rql=rql, vid='ampmcalendarweek',
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   448
                                         year=day.year, week=day.isocalendar()[1])
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   449
                weeklink = '<a href="%s">%s</a>' % (xml_escape(url),
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   450
                                                    day.isocalendar()[1])
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   451
                current_row.append(WEEKNUM_CELL % weeklink)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   452
                rows.append(current_row)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   453
                current_row = []
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   454
        current_row.extend([(NO_CELL, NO_CELL, NO_CELL)] * (6-day.weekday()))
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   455
        url = self._cw.build_url(rql=rql, vid='ampmcalendarweek',
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   456
                                 year=day.year, week=day.isocalendar()[1])
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   457
        weeklink = '<a href="%s">%s</a>' % (xml_escape(url),
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   458
                                            day.isocalendar()[1])
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   459
        current_row.append(WEEKNUM_CELL % weeklink)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   460
        rows.append(current_row)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   461
        # build two rows for each week: am & pm
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   462
        formatted_rows = []
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   463
        for row in rows:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   464
            week_title = row.pop()
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   465
            day_row = [day for day, am, pm in row]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   466
            am_row = [am for day, am, pm in row]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   467
            pm_row = [pm for day, am, pm in row]
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   468
            formatted_rows.append('<tr>%s%s</tr>'% (week_title, '\n'.join(day_row)))
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   469
            formatted_rows.append('<tr class="amRow"><td>&#160;</td>%s</tr>'% '\n'.join(am_row))
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   470
            formatted_rows.append('<tr class="pmRow"><td>&#160;</td>%s</tr>'% '\n'.join(pm_row))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   471
        # tigh everything together
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   472
        url = self._cw.build_url(rql=rql, vid='ampmcalendarmonth',
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   473
                                 year=first_day.year, month=first_day.month)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   474
        monthlink = '<a href="%s">%s</a>' % (xml_escape(url),
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   475
                                             umonth)
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   476
        return CALENDAR(self._cw) % (monthlink, '\n'.join(formatted_rows))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   477
1700
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   478
b5b323f8a05b update old_calendar, more needed
sylvain.thenault@logilab.fr
parents: 1688
diff changeset
   479
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   480
class AMPMWeekCalendarView(WeekCalendarView):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   481
    """this view renders a 3x1 calendars' table"""
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   482
    __regid__ = 'ampmcalendarweek'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   483
    title = _('am/pm calendar (week)')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   484
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   485
    def build_calendar(self, schedule, weeks):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   486
        rql = self.cw_rset.printable_rql()
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   487
        w = self.w
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   488
        _ = self._cw._
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   489
        for monday, sunday in weeks:
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   490
            umonth = self._cw.format_date(monday, '%B %Y')
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   491
            url = self._cw.build_url(rql=rql, vid='ampmcalendarmonth',
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   492
                                     year=monday.year, month=monday.month)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   493
            monthlink = '<a href="%s">%s</a>' % (xml_escape(url), umonth)
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   494
            w(u'<tr>%s</tr>' % (
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   495
                WEEK_TITLE % (_('week'), monday.isocalendar()[1], monthlink)))
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   496
            w(u'<tr><th>%s</th><th>&#160;</th></tr>'% _(u'Date'))
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   497
            for day in date_range(monday, sunday+ONEDAY):
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   498
                events = schedule.get(day)
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   499
                style = day.weekday() % 2 and "even" or "odd"
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   500
                w(u'<tr class="%s">' % style)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   501
                if events:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   502
                    hours = events.keys()
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   503
                    hours.sort()
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   504
                    w(AMPM_DAYWEEK % (
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   505
                        len(hours), _(WEEKDAYS[day.weekday()]),
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   506
                        self._cw.format_date(day)))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   507
                    w(AMPM_WEEK_CELL % (
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   508
                        hours[0].hour, hours[0].minute,
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   509
                        '\n'.join(events[hours[0]])))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   510
                    w(u'</tr>')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   511
                    for hour in hours[1:]:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   512
                        w(u'<tr class="%s">%s</tr>'% (
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   513
                            style, AMPM_WEEK_CELL % (hour.hour, hour.minute,
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   514
                                                     '\n'.join(events[hour]))))
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   515
                else:
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   516
                    w(AMPM_DAYWEEK_EMPTY % (
1707
71b55db64269 Use of datetime instead of mx.datetime and use of new date_range.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1700
diff changeset
   517
                        _(WEEKDAYS[day.weekday()]),
3457
0924d0d08d60 [api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   518
                        self._cw.format_date(day)))
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   519
                    w(WEEK_EMPTY_CELL)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   520
                    w(u'</tr>')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   521
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   522
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   523
SMALL_CALENDARS_PAGE = u"""<table class="smallCalendars">
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   524
<tr><td class="calendar">%s</td><td class="calendar">%s</td><td class="calendar">%s</td></tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   525
<tr><td class="calendar">%s</td><td class="calendar">%s</td><td class="calendar">%s</td></tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   526
<tr><td class="calendar">%s</td><td class="calendar">%s</td><td class="calendar">%s</td></tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   527
</table>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   528
"""
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   529
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   530
BIG_CALENDARS_PAGE = u"""<table class="bigCalendars">
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   531
<tr><td class="calendar">%s</td></tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   532
<tr><td class="calendar">%s</td></tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   533
<tr><td class="calendar">%s</td></tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   534
</table>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   535
"""
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   536
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   537
WEEKNUM_CELL = u'<td class="weeknum">%s</td>'
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   538
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   539
def CALENDAR(req):
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   540
    _ = req._
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   541
    WEEKNUM_HEADER = u'<th class="weeknum">%s</th>' % _('week')
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   542
    CAL_HEADER = WEEKNUM_HEADER + u' \n'.join([u'<th class="weekday">%s</th>' % _(day)[0].upper()
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   543
                                               for day in WEEKDAYS])
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   544
    return u"""<table>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   545
<tr><th class="month" colspan="8">%%s</th></tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   546
<tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   547
  %s
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   548
</tr>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   549
%%s
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   550
</table>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   551
""" % (CAL_HEADER,)
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   552
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   553
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   554
DAY_TEMPLATE = """<tr><td class="weekday">%(daylabel)s</td><td>%(dmydate)s</td><td>%(dayschedule)s</td>
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   555
"""
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   556
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   557
NO_CELL = u'<td class="noday"></td>'
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   558
EMPTY_CELL = u'<td class="cellEmpty"><span class="cellTitle">%s</span></td>'
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   559
CELL = u'<td class="cell"><span class="cellTitle">%s</span><div class="cellContent">%s</div></td>'
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   560
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   561
AMPM_DAY = u'<td class="cellDay">%d</td>'
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   562
AMPM_EMPTY = u'<td class="%sEmpty"><span class="cellTitle">%s</span></td>'
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   563
AMPM_CONTENT = u'<td class="%s"><span class="cellTitle">%s</span><div class="cellContent">%s</div></td>'
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   564
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   565
WEEK_TITLE = u'<th class="weekTitle" colspan="2">%s %s (%s)</th>'
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   566
WEEK_EMPTY_CELL = u'<td class="weekEmptyCell">&#160;</td>'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   567
WEEK_CELL = u'<td class="weekCell"><div class="cellContent">%s</div></td>'
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   568
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   569
AMPM_DAYWEEK_EMPTY = u'<td>%s&#160;%s</td>'
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2312
diff changeset
   570
AMPM_DAYWEEK = u'<td rowspan="%d">%s&#160;%s</td>'
1688
1f16db872f92 Back to old_calendar for now.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
diff changeset
   571
AMPM_WEEK_CELL = u'<td class="ampmWeekCell"><div class="cellContent">%02d:%02d - %s</div></td>'