Commit cd5e6ad6 authored by Romain Courteaud's avatar Romain Courteaud

Add method getNextAlarmDate.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11778 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent fed875e5
...@@ -37,8 +37,6 @@ from Products.ERP5Type.DateUtils import addToDate ...@@ -37,8 +37,6 @@ from Products.ERP5Type.DateUtils import addToDate
from zLOG import LOG from zLOG import LOG
class Periodicity(Base): class Periodicity(Base):
""" """
An Alarm is in charge of checking anything (quantity of a certain An Alarm is in charge of checking anything (quantity of a certain
...@@ -66,127 +64,143 @@ class Periodicity(Base): ...@@ -66,127 +64,143 @@ class Periodicity(Base):
, PropertySheet.Periodicity , PropertySheet.Periodicity
) )
security.declareProtected(Permissions.View, 'setNextAlarmDate') def validateMinute(self, date, previous_date):
def setNextAlarmDate(self,current_date=None):
"""
Get the next date where this periodic event should start.
We have to take into account the start date, because
sometimes an event may be started by hand. We must be
sure to never forget to start an event, event with some
delays.
Here are some rules :
- if the periodicity start date is in the past and we never starts
this periodic event, then return the periodicity start date.
- if the periodicity start date is in the past but we already
have started the periodic event, then see
"""
if self.getPeriodicityStartDate() is None:
return
next_start_date = self.getAlarmDate()
if current_date is None:
# This is usefull to set the current date as parameter for
# unit testing, by default it should be now
current_date = DateTime()
if next_start_date is None:
next_start_date = current_date
if next_start_date > current_date:
return
else:
# Make sure the old date is not too far away
nb_days = int(current_date-next_start_date)
next_start_date = next_start_date + nb_days
def validateMinute(self,date, previous_date):
periodicity_minute_frequency = self.getPeriodicityMinuteFrequency() periodicity_minute_frequency = self.getPeriodicityMinuteFrequency()
periodicity_minute_list = self.getPeriodicityMinuteList() periodicity_minute_list = self.getPeriodicityMinuteList()
if periodicity_minute_frequency is None and periodicity_minute_list in ([],None,()): if (periodicity_minute_frequency is None) and \
# in this case, we may want to have an periodicity every hour based on the start date (periodicity_minute_list in ([], None, ())):
# without defining anything about minutes periodicity, so we compare with # in this case, we may want to have an periodicity every hour
# minutes with the one defined in the previous alarm date # based on the start date
# without defining anything about minutes periodicity,
# so we compare with minutes with the one defined
# in the previous alarm date
return (date.minute() == previous_date.minute()) return (date.minute() == previous_date.minute())
if periodicity_minute_frequency not in ('',None): if periodicity_minute_frequency not in ('', None):
return (date.minute() % periodicity_minute_frequency) == 0 return (date.minute() % periodicity_minute_frequency) == 0
elif len(periodicity_minute_list)>0: elif len(periodicity_minute_list) > 0:
return date.minute() in periodicity_minute_list return date.minute() in periodicity_minute_list
def validateHour(self,date): def validateHour(self, date):
periodicity_hour_frequency = self.getPeriodicityHourFrequency() periodicity_hour_frequency = self.getPeriodicityHourFrequency()
periodicity_hour_list = self.getPeriodicityHourList() periodicity_hour_list = self.getPeriodicityHourList()
if periodicity_hour_frequency is None and periodicity_hour_list in ([],None,()): if (periodicity_hour_frequency is None) and \
(periodicity_hour_list in ([], None, ())):
return 1 return 1
if periodicity_hour_frequency not in ('',None): if periodicity_hour_frequency not in ('', None):
return (date.hour() % periodicity_hour_frequency) == 0 return (date.hour() % periodicity_hour_frequency) == 0
elif len(periodicity_hour_list)>0: elif len(periodicity_hour_list) > 0:
return date.hour() in periodicity_hour_list return date.hour() in periodicity_hour_list
def validateDay(self,date): def validateDay(self, date):
periodicity_day_frequency = self.getPeriodicityDayFrequency() periodicity_day_frequency = self.getPeriodicityDayFrequency()
periodicity_month_day_list = self.getPeriodicityMonthDayList() periodicity_month_day_list = self.getPeriodicityMonthDayList()
if periodicity_day_frequency is None and periodicity_month_day_list in ([],None,()): if (periodicity_day_frequency is None) and \
(periodicity_month_day_list in ([], None, ())):
return 1 return 1
if periodicity_day_frequency not in ('',None): if periodicity_day_frequency not in ('', None):
return (date.day() % periodicity_day_frequency) == 0 return (date.day() % periodicity_day_frequency) == 0
elif len(periodicity_month_day_list)>0: elif len(periodicity_month_day_list) > 0:
return date.day() in periodicity_month_day_list return date.day() in periodicity_month_day_list
def validateWeek(self,date): def validateWeek(self, date):
periodicity_week_frequency = self.getPeriodicityWeekFrequency() periodicity_week_frequency = self.getPeriodicityWeekFrequency()
periodicity_week_day_list = self.getPeriodicityWeekDayList() periodicity_week_day_list = self.getPeriodicityWeekDayList()
periodicity_week_list = self.getPeriodicityWeekList() periodicity_week_list = self.getPeriodicityWeekList()
if periodicity_week_frequency is None and periodicity_week_day_list in ([],None,()) \ if (periodicity_week_frequency is None) and \
and periodicity_week_list is None: (periodicity_week_day_list in ([], None, ())) and \
(periodicity_week_list is None):
return 1 return 1
if periodicity_week_frequency not in ('',None): if periodicity_week_frequency not in ('', None):
if not((date.week() % periodicity_week_frequency) == 0): if not((date.week() % periodicity_week_frequency) == 0):
return 0 return 0
if periodicity_week_day_list not in (None,(),[]): if periodicity_week_day_list not in (None, (), []):
if not (date.Day() in periodicity_week_day_list): if not (date.Day() in periodicity_week_day_list):
return 0 return 0
if periodicity_week_list not in (None,(),[]): if periodicity_week_list not in (None, (), []):
if not (date.week() in periodicity_week_list): if not (date.week() in periodicity_week_list):
return 0 return 0
return 1 return 1
def validateMonth(self,date): def validateMonth(self, date):
periodicity_month_frequency = self.getPeriodicityMonthFrequency() periodicity_month_frequency = self.getPeriodicityMonthFrequency()
periodicity_month_list = self.getPeriodicityMonthList() periodicity_month_list = self.getPeriodicityMonthList()
if periodicity_month_frequency is None and periodicity_month_list in ([],None,()): if (periodicity_month_frequency is None) and \
(periodicity_month_list in ([], None, ())):
return 1 return 1
if periodicity_month_frequency not in ('',None): if periodicity_month_frequency not in ('', None):
return (date.month() % periodicity_month_frequency) == 0 return (date.month() % periodicity_month_frequency) == 0
elif len(periodicity_month_list)>0: elif len(periodicity_month_list) > 0:
return date.month() in periodicity_month_list return date.month() in periodicity_month_list
def getNextAlarmDate(self, current_date, next_start_date=None):
"""
Get the next date where this periodic event should start.
We have to take into account the start date, because
sometimes an event may be started by hand. We must be
sure to never forget to start an event, even with some
delays.
Here are some rules :
- if the periodicity start date is in the past and we never starts
this periodic event, then return the periodicity start date.
- if the periodicity start date is in the past but we already
have started the periodic event, then see
"""
if next_start_date is None:
next_start_date = current_date
if next_start_date > current_date:
return
else:
# Make sure the old date is not too far away
nb_days = int(current_date-next_start_date)
next_start_date = next_start_date + nb_days
previous_date = next_start_date previous_date = next_start_date
next_start_date = addToDate(next_start_date,minute=1) next_start_date = addToDate(next_start_date, minute=1)
while 1: while 1:
validate_minute = validateMinute(self,next_start_date, previous_date) validate_minute = self.validateMinute(next_start_date, previous_date)
validate_hour = validateHour(self,next_start_date) validate_hour = self.validateHour(next_start_date)
validate_day = validateDay(self,next_start_date) validate_day = self.validateDay(next_start_date)
validate_week = validateWeek(self,next_start_date) validate_week = self.validateWeek(next_start_date)
validate_month = validateMonth(self,next_start_date) validate_month = self.validateMonth(next_start_date)
if (next_start_date >= current_date \ if (next_start_date >= current_date \
and validate_minute and validate_hour and validate_day \ and validate_minute and validate_hour and validate_day \
and validate_week and validate_month): and validate_week and validate_month):
break break
else: else:
if not(validate_minute): if not(validate_minute):
next_start_date = addToDate(next_start_date,minute=1) next_start_date = addToDate(next_start_date, minute=1)
else: else:
if not(validate_hour): if not(validate_hour):
next_start_date = addToDate(next_start_date,hour=1) next_start_date = addToDate(next_start_date, hour=1)
else: else:
if not(validate_day and validate_week and validate_month): if not(validate_day and validate_week and validate_month):
next_start_date = addToDate(next_start_date,day=1) next_start_date = addToDate(next_start_date, day=1)
else: else:
# Everything is right, but the date is still not bigger # Everything is right, but the date is still not bigger
# than the current date, so we must continue # than the current date, so we must continue
next_start_date = addToDate(next_start_date,minute=1) next_start_date = addToDate(next_start_date, minute=1)
self.Alarm_zUpdateAlarmDate(uid=self.getUid(),alarm_date=next_start_date) return next_start_date
security.declareProtected(Permissions.View, 'setNextAlarmDate')
def setNextAlarmDate(self, current_date=None):
"""
Save the next alarm date
"""
if self.getPeriodicityStartDate() is None:
return
next_start_date = self.getAlarmDate()
if current_date is None:
# This is usefull to set the current date as parameter for
# unit testing, by default it should be now
current_date = DateTime()
next_start_date = self.getNextAlarmDate(current_date,
next_start_date=next_start_date)
if next_start_date is not None:
self.Alarm_zUpdateAlarmDate(uid=self.getUid(),
alarm_date=next_start_date)
security.declareProtected(Permissions.View, 'getAlarmDate') security.declareProtected(Permissions.View, 'getAlarmDate')
def getAlarmDate(self): def getAlarmDate(self):
...@@ -205,14 +219,13 @@ class Periodicity(Base): ...@@ -205,14 +219,13 @@ class Periodicity(Base):
alarm_date = periodicity_start_date alarm_date = periodicity_start_date
return alarm_date 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 ???
security.declareProtected(Permissions.View, 'getWeekDayList') security.declareProtected(Permissions.View, 'getWeekDayList')
def getWeekDayList(self): def getWeekDayList(self):
""" """
returns something like ['Sunday','Monday',...] returns something like ['Sunday','Monday',...]
""" """
# XXX Currently, it's not translated in the UI
return DateTime._days return DateTime._days
# XXX This look like to not works, so override the getter # XXX This look like to not works, so override the getter
...@@ -242,6 +255,3 @@ class Periodicity(Base): ...@@ -242,6 +255,3 @@ class Periodicity(Base):
if day in day_list: if day in day_list:
new_list += [day] new_list += [day]
return new_list return new_list
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