# HG changeset patch
# User sylvain.thenault@logilab.fr
# Date 1236694675 -3600
# Node ID aebf37f460c1e4a8edc81b9362ce401c1e48bd94
# Parent bb96289257bfb0945c7a7e2e6c2b2733dd17ed93
some datetime fixes
diff -r bb96289257bf -r aebf37f460c1 web/views/calendar.py
--- a/web/views/calendar.py Tue Mar 10 15:17:40 2009 +0100
+++ b/web/views/calendar.py Tue Mar 10 15:17:55 2009 +0100
@@ -13,60 +13,25 @@
from cubicweb.interfaces import ICalendarable
from cubicweb.selectors import implements
-from cubicweb.utils import strptime, date_range
+from cubicweb.utils import strptime, date_range, todate
from cubicweb.view import EntityView
from cubicweb.common.uilib import ajax_replace_url
-
-# For backward compatibility
-from cubicweb.interfaces import ICalendarViews, ITimetableViews
-try:
- from cubicweb.web.views.old_calendar import _CalendarView, AMPMWeekCalendarView
-except ImportError:
- import logging
- logger = logging.getLogger('cubicweb.registry')
- logger.info("old calendar views could not be found and won't be registered")
-
_ = unicode
-# # useful constants & functions
-# def mkdt(mxdate):
-# """
-# Build a stdlib datetime date from a mx.datetime
-# """
-# d = mxdate
-# return datetime(d.year, d.month, d.day, d.hour, d.minute,
-# tzinfo=icalendar.utc)
+# useful constants & functions ################################################
-# used by i18n tools
+ONEDAY = timedelta(1, 0)
+
WEEKDAYS = (_("monday"), _("tuesday"), _("wednesday"), _("thursday"),
_("friday"), _("saturday"), _("sunday"))
MONTHNAMES = ( _('january'), _('february'), _('march'), _('april'), _('may'),
_('june'), _('july'), _('august'), _('september'), _('october'),
_('november'), _('december')
)
-
-#################
-# In calendar views (views used as calendar cell item)
-
-
-class CalendarItemView(EntityView):
- id = 'calendaritem'
+
+# Calendar views ##############################################################
- def cell_call(self, row, col, dates=False):
- task = self.complete_entity(row)
- task.view('oneline', w=self.w)
- if dates:
- if task.start and task.stop:
- self.w('
' % self.req._('from %(date)s' % {'date': self.format_date(task.start)}))
- self.w('
' % self.req._('to %(date)s' % {'date': self.format_date(task.stop)}))
- self.w('
to %s'%self.format_date(task.stop))
-
-class CalendarLargeItemView(CalendarItemView):
- id = 'calendarlargeitem'
-
-#################
-# Calendar views
class iCalView(EntityView):
"""A calendar view that generates a iCalendar file (RFC 2445)
@@ -122,6 +87,22 @@
self.w(u'')
self.w(u'')
+
+class CalendarItemView(EntityView):
+ id = 'calendaritem'
+
+ def cell_call(self, row, col, dates=False):
+ task = self.complete_entity(row)
+ task.view('oneline', w=self.w)
+ if dates:
+ if task.start and task.stop:
+ self.w('
' % self.req._('from %(date)s' % {'date': self.format_date(task.start)}))
+ self.w('
' % self.req._('to %(date)s' % {'date': self.format_date(task.stop)}))
+ self.w('
to %s'%self.format_date(task.stop))
+
+class CalendarLargeItemView(CalendarItemView):
+ id = 'calendarlargeitem'
+
class _TaskEntry(object):
def __init__(self, task, color, index=0):
@@ -130,6 +111,16 @@
self.index = index
self.length = 1
+ def in_working_hours(self):
+ """predicate returning True is the task is in working hours"""
+ if self.task.start.hour > 7 and self.task.stop.hour < 20:
+ return True
+ return False
+
+ def is_one_day_task(self):
+ task = self.task
+ return task.start and task.stop and task.start.isocalendar() == task.stop.isocalendar()
+
class OneMonthCal(EntityView):
"""At some point, this view will probably replace ampm calendars"""
id = 'onemonthcal'
@@ -170,22 +161,23 @@
else:
user = None
the_dates = []
- if task.start:
- if task.start > lastday:
+ tstart = todate(task.start)
+ if tstart:
+ if tstart > lastday:
continue
- the_dates = [task.start]
- if task.stop:
- if task.stop < firstday:
+ the_dates = [tstart]
+ tstop = todate(task.start)
+ if tstop:
+ if tstop < firstday:
continue
- the_dates = [task.stop]
- if task.start and task.stop:
- if task.start.isocalendar() == task.stop.isocalendar():
- date = task.start
- if firstday<= date <= lastday:
- the_dates = [date]
+ the_dates = [tstop]
+ if tstart and tstop:
+ if tstart.isocalendar() == tstop.isocalendar():
+ if firstday <= tstart <= lastday:
+ the_dates = [tstart]
else:
- the_dates = date_range(max(task.start,firstday),
- min(task.stop,lastday))
+ the_dates = date_range(max(tstart, firstday),
+ min(tstop, lastday))
if not the_dates:
continue
@@ -204,8 +196,8 @@
visited_tasks = {} # holds a description of a task
task_colors = {} # remember a color assigned to a task
- for date in month_dates:
- d_tasks = dates.get((date.year, date.month, date.day), {})
+ for mdate in month_dates:
+ d_tasks = dates.get((mdate.year, mdate.month, mdate.day), {})
rows = [None] * nrows
# every task that is "visited" for the first time
# require a special treatment, so we put them in
@@ -253,11 +245,11 @@
tuple(self.req._(day) for day in WEEKDAYS))
# build calendar
- for date, task_rows in zip(month_dates, days):
- if date.weekday() == 0:
+ for mdate, task_rows in zip(month_dates, days):
+ if mdate.weekday() == 0:
self.w(u'