Commit fd70c604 authored by Sebastien Robin's avatar Sebastien Robin

many changes to setNextAlarmDate, but still not very nice


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@1819 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 5b8375cd
...@@ -33,6 +33,7 @@ from Products.ERP5Type.Base import Base ...@@ -33,6 +33,7 @@ from Products.ERP5Type.Base import Base
from Acquisition import aq_base, aq_parent, aq_inner, aq_acquire from Acquisition import aq_base, aq_parent, aq_inner, aq_acquire
from Products.CMFCore.utils import getToolByName from Products.CMFCore.utils import getToolByName
from DateTime import DateTime from DateTime import DateTime
from Products.ERP5Type.DateUtils import addToDate
from zLOG import LOG from zLOG import LOG
...@@ -65,8 +66,8 @@ class Periodicity(Base): ...@@ -65,8 +66,8 @@ class Periodicity(Base):
, PropertySheet.Periodicity , PropertySheet.Periodicity
) )
security.declareProtected(Permissions.View, 'getNextStartDate') security.declareProtected(Permissions.View, 'setNextAlarmDate')
def getNextStartDate(self): def setNextAlarmDate(self,current_date=None):
""" """
Get the next date where this periodic event should start. Get the next date where this periodic event should start.
...@@ -81,57 +82,85 @@ class Periodicity(Base): ...@@ -81,57 +82,85 @@ class Periodicity(Base):
- if the periodicity start date is in the past but we already - if the periodicity start date is in the past but we already
have started the periodic event, then see have started the periodic event, then see
""" """
current_date = DateTime()
# XXX Never store attributes
#next_start_date = getattr(self,'_next_start_date',None)
next_start_date = self.getPeriodicityStartDate() next_start_date = self.getPeriodicityStartDate()
start_date = self.getStartDate()
# AXXX never store attributeslready calculated
#if next_start_date is not None and \
# (next_start_date > current_date) or \
# (start_date is not None and start_date < next_start_date):
# pass
# simple cases where the next start date will be the periodicity start date
#elif next_start_date is None and \
# (self.getPeriodicityStartDate() > current_date) or \
# (self.getStartDate() is None) or \
# (self.getStartDate(DateTime()) < self.getPeriodicityStartDate()):
# next_start_date = self.getPeriodicityStartDate()
# We have to do calculation in order to know the next start date
#else:
if next_start_date is None: if next_start_date is None:
next_start_date = self.getPeriodicityStartDate() return
if start_date is None: alarm_date = self.getAlarmDate()
start_date = next_start_date if current_date is None:
periodicity_day = self.getPeriodicityDay() # This is usefull to set the current date as parameter for
periodicity_week = self.getPeriodicityWeek() # unit testing, by default it should be now
periodicity_month = self.getPeriodicityMonth() current_date = DateTime()
# Day period LOG('setNextAlarmDate: alarm_date > current_date',0,alarm_date > current_date)
if periodicity_day not in ('',None): LOG('setNextAlarmDate: alarm_date',0,alarm_date)
next_start_date = next_start_date + periodicity_day LOG('setNextAlarmDate: current_date',0,current_date)
while start_date > next_start_date: LOG('setNextAlarmDate: next_start_date',0,next_start_date)
next_start_date = next_start_date + periodicity_day if alarm_date > current_date:
return
periodicity_day_frequency = self.getPeriodicityDayFrequency()
periodicity_week_frequency = self.getPeriodicityWeekFrequency()
periodicity_month_frequency = self.getPeriodicityMonthFrequency()
# Month period
if periodicity_month_frequency not in ('',None):
month_week = self.getPeriodicityMonthWeek()
month_week_day = self.getPeriodicityMonthWeekDay()
found = 0
month = int(next_start_date.strftime('%m'))
next_start_date = next_start_date + 1
week = 0
while int(next_start_date.strftime('%m')) == month:
next_start_date = next_start_date + 1
while not found:
if next_start_date.strftime('%A') == month_week_day:
week += 1
if month_week == week:
break
next_start_date = next_start_date + 1
# Week period # Week period
elif periodicity_week not in ('',None): if periodicity_week_frequency not in ('',None):
next_start_date = next_start_date + 1 next_start_date = next_start_date + 1
periodicity_week_day = self.getPeriodicityWeekDay() periodicity_week_day = self.getPeriodicityWeekDay()
while (next_start_date.strftime('%A').lower() not in periodicity_week_day) and \ while (next_start_date.strftime('%A') not in periodicity_week_day) and \
start_date > next_start_date: current_date >= next_start_date:
next_start_date = next_start_date + 1 next_start_date = next_start_date + 1
# If we are at the beginning of a new week, make sure that # If we are at the beginning of a new week, make sure that
# we take into account the number of week between two periods # we take into account the number of week between two periods
if next_start_date.strftime('%A').lower() == periodicity_week_day[0]: if next_start_date.strftime('%A') == periodicity_week_day[0]:
next_start_date = next_start_date + 7 * (periodicity_week-1) next_start_date = next_start_date + 7 * (periodicity_week-1)
# Month period # Day period
elif periodicity_month not in ('',None): if periodicity_day_frequency not in ('',None):
pass while current_date >= next_start_date:
# XXX to be implemented next_start_date = next_start_date + periodicity_day_frequency
#next_start_date = next_start_date + 1 # Hour period
#periodicity_month_day = self.getPeriodicityWeekDay() periodicity_hour_frequency = self.getPeriodicityHourFrequency()
#if periodicity_month_day is not None: LOG('setNextAlarmDate: periodicity_hour_frequency',0,periodicity_hour_frequency)
# while periodicity_hour_list = self.getPeriodicityHourList()
# XXX never store setattr(self,'_next_start_date',next_start_date) LOG('setNextAlarmDate: periodicity_hour_list',0,periodicity_hour_list)
return next_start_date if periodicity_hour_frequency not in ('',None):
LOG('setNextAlarmDate: before adding hour next_start_date',0,next_start_date)
while current_date >= next_start_date:
next_start_date = addToDate(next_start_date,hour=periodicity_hour_frequency)
LOG('setNextAlarmDate: hour added next_start_date',0,next_start_date)
elif periodicity_hour_list not in ('',None) and len(periodicity_hour_list)>0:
while current_date >= next_start_date:
next_start_date = addToDate(next_start_date,hour=1)
while next_start_date.hour() not in periodicity_hour_list:
next_start_date = addToDate(next_start_date,hour=1)
LOG('setNextAlarmDate: hour added next_start_date',0,next_start_date)
LOG('setNextAlarmDate: added next_start_date',0,next_start_date)
self.setAlarmDate(next_start_date)
security.declareProtected(Permissions.View, 'getWeekDayList')
def getAlarmDate(self):
"""
returns something like ['Sunday','Monday',...]
"""
alarm_date = self._baseGetAlarmDate()
if alarm_date is None:
alarm_date = self.getPeriodicityStartDate()
return alarm_date
# XXX May be we should create a Date class for following methods ??? # XXX May be we should create a Date class for following methods ???
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment