Commit c45773cd authored by Arnaud Fontaine's avatar Arnaud Fontaine

ZODB Components: Migrate Products.ERP5Type.DateUtils and its Unit Tests from filesystem.

Remove strange hacks with get_request() and manual monkey-patching in
testDateUtils. Not sure at all why it was needed in the first place but
it does not seem relevant anymore.
parent 4a31baf4
from Products.ZSQLCatalog.SQLCatalog import Query
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
from DateTime import DateTime
portal = context.getPortalObject()
......
from DateTime import DateTime
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
from Products.ERP5Type.Message import translateString
month_added = 1
......
......@@ -3,7 +3,7 @@
"""
from DateTime import DateTime
from Products.ZSQLCatalog.SQLCatalog import Query
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
portal = context.getPortalObject()
portal_preferences = portal.portal_preferences
......
from Products.DCWorkflow.DCWorkflow import ValidationFailed
from Products.ERP5Type.Message import Message
from Products.ERP5Type.DateUtils import getIntervalBetweenDates
from erp5.component.module.DateUtils import getIntervalBetweenDates
transaction = state_change['object']
......
from Products.DCWorkflow.DCWorkflow import ValidationFailed
from Products.ERP5Type.Message import Message
from Products.ERP5Type.DateUtils import getIntervalBetweenDates
from erp5.component.module.DateUtils import getIntervalBetweenDates
if stop_date is None:
from DateTime import DateTime
......
......@@ -27,7 +27,7 @@
##############################################################################
from erp5.component.document.MovementGroup import MovementGroup
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
from DateTime import DateTime
class DayMovementGroup(MovementGroup):
......
......@@ -28,7 +28,7 @@
from erp5.component.document.MovementGroup import MovementGroup
from DateTime import DateTime
from Products.ERP5Type.DateUtils import atTheEndOfPeriod
from erp5.component.module.DateUtils import atTheEndOfPeriod
class MonthlyRangeMovementGroup(MovementGroup):
......
......@@ -3,7 +3,7 @@ If `year` is true, return the integer value, otherwise returns a translated
string.
"""
from DateTime import DateTime
from Products.ERP5Type.DateUtils import getIntervalBetweenDates
from erp5.component.module.DateUtils import getIntervalBetweenDates
Base_translateString = context.Base_translateString
birthday = context.getBirthday()
......
from Products.ERP5Type.DateUtils import atTheEndOfPeriod
from erp5.component.module.DateUtils import atTheEndOfPeriod
request = container.REQUEST
from_date = request.get('from_date', None)
to_date = request.get('at_date', None)
......
from Products.PythonScripts.standard import Object
from Products.ERP5Type.DateUtils import atTheEndOfPeriod
from erp5.component.module.DateUtils import atTheEndOfPeriod
request = container.REQUEST
from_date = request.get('from_date', None)
to_date = request.get('at_date', None)
......
from Products.ERP5Type.DateUtils import atTheEndOfPeriod
from erp5.component.module.DateUtils import atTheEndOfPeriod
request = container.REQUEST
portal = context.getPortalObject()
from_date = request.get('from_date', None)
......
from Products.ERP5Form.Report import ReportSection
from Products.ERP5Type.DateUtils import getIntervalListBetweenDates
from erp5.component.module.DateUtils import getIntervalListBetweenDates
from DateTime import DateTime
result=[]
# civil status
......
if context.getPortalType() != 'Person':
raise TypeError('Person object is required')
from DateTime import DateTime
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
key = context.Base_getBearerTokenKey()
if not key:
......
......@@ -36,7 +36,7 @@ from DateTime import DateTime
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5.mixin.periodicity import PeriodicityMixin
from erp5.component.document.Movement import Movement
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
class PresencePeriod(Movement, PeriodicityMixin):
"""
......
......@@ -34,7 +34,7 @@ from AccessControl.SecurityManagement import newSecurityManager, \
getSecurityManager, setSecurityManager
from AccessControl import Unauthorized
from DateTime import DateTime
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
class TestAlarm(ERP5TypeTestCase):
"""
......
......@@ -29,33 +29,8 @@
import unittest
# Make it possible to use Globals.get_request
class DummyRequest(dict):
__allow_access_to_unprotected_subobjects__ = 1
def set(self, k, v):
self[k] = v
global request
request = DummyRequest()
def get_request():
global request
return request
# apply patch (before it's imported by other modules)
from Products.ERP5Type import Globals
Globals.get_request = get_request
# Initialize ERP5Form Product to load monkey patches
from Testing import ZopeTestCase
ZopeTestCase.installProduct('CMFCategory')
ZopeTestCase.installProduct('ERP5Type')
from DateTime import DateTime
from Products.ERP5Type.DateUtils import addToDate, getIntervalListBetweenDates, \
from erp5.component.module.DateUtils import addToDate, getIntervalListBetweenDates, \
atTheEndOfPeriod, getClosestDate
class TestDateUtils(unittest.TestCase):
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Test Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>default_reference</string> </key>
<value> <string>testDateUtils</string> </value>
</item>
<item>
<key> <string>default_source_reference</string> </key>
<value> <string>Products.ERP5Type.tests.testDateUtils</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>test.erp5.testDateUtils</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Test Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_log</string> </key>
<value>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -12,6 +12,7 @@ test.erp5.testConstraint
test.erp5.testContributionRegistryTool
test.erp5.testCookieCrumbler
test.erp5.testCopySupport
test.erp5.testDateUtils
test.erp5.testERP5Base
test.erp5.testERP5Category
test.erp5.testERP5Coordinate
......
......@@ -4,7 +4,7 @@ is based on calculation of the frequency_index and creation_date_index.
Documents which their frequency_index and creation_date_index are the
same as those calculated, are updated.
"""
from Products.ERP5Type.DateUtils import convertDateToHour
from erp5.component.module.DateUtils import convertDateToHour
date_dict = {}
# Shared function
......
......@@ -3,7 +3,7 @@
# automatically set a good start and stop date to make generic code working
from DateTime import DateTime
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
time_table_line = state_change["object"]
day_of_week = time_table_line.getDayOfWeek()
......
......@@ -30,8 +30,8 @@ from AccessControl import ClassSecurityInfo
from DateTime import DateTime
from string import capitalize
from Products.ERP5Type.DateUtils import centis, getClosestDate, addToDate
from Products.ERP5Type.DateUtils import getDecimalNumberOfYearsBetween
from erp5.component.module.DateUtils import centis, getClosestDate, addToDate
from erp5.component.module.DateUtils import getDecimalNumberOfYearsBetween
from Products.ERP5Type import Permissions
from erp5.component.mixin.RuleMixin import RuleMixin
from Products.CMFCore.utils import getToolByName
......
from Products.ERP5Type.DateUtils import getClosestDate
from erp5.component.module.DateUtils import getClosestDate
portal = context.getPortalObject()
......
......@@ -29,7 +29,7 @@
from Products.ERP5Type import Permissions, PropertySheet
from erp5.component.document.SubscriptionItem import SubscriptionItem
from AccessControl import ClassSecurityInfo
from Products.ERP5Type.DateUtils import addToDate, atTheEndOfPeriod
from erp5.component.module.DateUtils import addToDate, atTheEndOfPeriod
from DateTime import DateTime
class EmploymentContract(SubscriptionItem):
......
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
if context.getSourceAdministration() is None \
or context.getEffectiveDate() is None \
......
from Products.ERP5Type.DateUtils import getNumberOfDayInMonth
from erp5.component.module.DateUtils import getNumberOfDayInMonth
def getLastDateOfMonth(date):
return DateTime(date.year(), date.month(), getNumberOfDayInMonth(date))
......
from Products.ERP5Type.DateUtils import addToDate, getIntervalBetweenDates, getNumberOfDayInMonth
from erp5.component.module.DateUtils import addToDate, getIntervalBetweenDates, getNumberOfDayInMonth
portal = context.getPortalObject()
portal_categories = context.portal_categories
......
from Products.ERP5Type.DateUtils import getNumberOfDayInMonth
from erp5.component.module.DateUtils import getNumberOfDayInMonth
portal = context.getPortalObject()
portal_categories = context.portal_categories
......
from Products.ERP5Type.DateUtils import getNumberOfDayInMonth
from erp5.component.module.DateUtils import getNumberOfDayInMonth
portal = context.getPortalObject()
......
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
from Products.ZSQLCatalog.SQLCatalog import Query
portal = context.getPortalObject()
......
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
portal = context.getPortalObject()
......
from Products.ERP5Type.DateUtils import getNumberOfDayInMonth
from erp5.component.module.DateUtils import getNumberOfDayInMonth
if context.getSourceAdministration() is None \
or context.getEffectiveDate() is None \
......
from Products.ERP5Type.DateUtils import addToDate, getNumberOfDayInMonth
from erp5.component.module.DateUtils import addToDate, getNumberOfDayInMonth
if context.getSourceAdministration() is None \
or context.getEffectiveDate() is None \
......
from Products.ERP5Type.DateUtils import getNumberOfDayInMonth
from erp5.component.module.DateUtils import getNumberOfDayInMonth
portal = context.getPortalObject()
portal_categories = portal.portal_categories
......
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
Base_translateString = context.Base_translateString
......
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
task_portal_type = 'Task'
task_module = context.getDefaultModule(task_portal_type)
......
......@@ -59,7 +59,7 @@ is based on calculation of the frequency_index and creation_date_index.\n
Documents which their frequency_index and creation_date_index are the\n
same as those calculated, are updated.\n
"""\n
from Products.ERP5Type.DateUtils import convertDateToHour\n
from erp5.component.module.DateUtils import convertDateToHour\n
date_dict = {}\n
\n
#Step1: convert the alarm date into hours\n
......@@ -121,7 +121,7 @@ for frequency_reference, creation_date in date_dict.items():\n
<key> <string>co_varnames</string> </key>
<value>
<tuple>
<string>Products.ERP5Type.DateUtils</string>
<string>erp5.component.module.DateUtils</string>
<string>convertDateToHour</string>
<string>date_dict</string>
<string>alarm_date</string>
......
from Products.ERP5Type.DateUtils import atTheEndOfPeriod
from erp5.component.module.DateUtils import atTheEndOfPeriod
return atTheEndOfPeriod(context.getStopDate(), 'month') - 1
from Products.ERP5Type.DateUtils import getIntervalListBetweenDates
from erp5.component.module.DateUtils import getIntervalListBetweenDates
from DateTime import DateTime
request = container.REQUEST
......
from Products.ERP5Type.DateUtils import getClosestDate
from erp5.component.module.DateUtils import getClosestDate
portal = context.getPortalObject()
......
from Products.ERP5Type.DateUtils import getClosestDate
from erp5.component.module.DateUtils import getClosestDate
portal = context.getPortalObject()
......
......@@ -28,7 +28,6 @@
##############################################################################
from TargetSolver import TargetSolver
from Products.ERP5Type.DateUtils import createDateTimeFromMillis
class CopyToTarget(TargetSolver):
"""
......@@ -87,6 +86,7 @@ class CopyToTarget(TargetSolver):
"""
Generate values to save on simulation movement.
"""
from erp5.component.module.DateUtils import createDateTimeFromMillis
value_dict = {}
# Modify quantity, start_date, stop_date
start_date = simulation_movement.getStartDate()
......
......@@ -34,9 +34,9 @@ from string import capitalize
from Products.ERP5Type import Permissions, PropertySheet
from erp5.component.interface.IImmobilisationItem import IImmobilisationItem
from Products.ERP5Type.DateUtils import addToDate, getClosestDate, roundDate
from Products.ERP5Type.DateUtils import getRoundedMonthBetween, millis
from Products.ERP5Type.DateUtils import getAccountableYearFraction
from erp5.component.module.DateUtils import addToDate, getClosestDate, roundDate
from erp5.component.module.DateUtils import getRoundedMonthBetween, millis
from erp5.component.module.DateUtils import getAccountableYearFraction
from erp5.component.document.Amount import Amount
from erp5.component.document.Item import Item
from Products.CMFCore.utils import getToolByName
......
......@@ -26,7 +26,7 @@
##############################################################################
from DateTime import DateTime
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
import re
def render_date_range(date_range):
......
......@@ -33,9 +33,8 @@ from AccessControl import ModuleSecurityInfo
from DateTime import DateTime
from datetime import datetime
from string import zfill
from zLOG import LOG
security = ModuleSecurityInfo('Products.ERP5Type.DateUtils')
security = ModuleSecurityInfo(__name__)
security.declarePublic('addToDate', 'getClosestDate',
'getIntervalBetweenDates', 'getMonthAndDaysBetween',
'getCompletedMonthBetween', 'getRoundedMonthBetween',
......@@ -166,8 +165,6 @@ def getClosestDate(date=None, target_date=None,
date = DateTime('2000/01/01')
date._tz = target_date._tz
earlier_target_date = target_date - millis
to_check = { 'day':{'year':1, 'month':1, 'day':1}, 'month':{'year':1, 'month':1}, 'year':{'year':1} }
diff_value = {}
diff_value = getIntervalBetweenDates(from_date = date, to_date = target_date, keys=to_check[precision])
......@@ -182,6 +179,7 @@ def getClosestDate(date=None, target_date=None,
return return_date
# pylint: disable=dangerous-default-value
def getIntervalBetweenDates(from_date=None, to_date=None,
keys={'year':1, 'month':1, 'day':1}):
"""
......@@ -222,6 +220,7 @@ def getIntervalBetweenDates(from_date=None, to_date=None,
returned_value[key] = value
return returned_value
# pylint: disable=dangerous-default-value
def getIntervalListBetweenDates(from_date=None, to_date=None,
keys={'year':1, 'month':1, 'week' : 1, 'day':1}):
"""
......@@ -451,10 +450,10 @@ def convertDateToHour(date=None):
# calculate the ordinal date of the creation date and the reference date
ordinal_date = datetime.toordinal(formatted_creation_date)
ordinal_reference_date = datetime.toordinal(reference_date)
hour = (ordinal_date - ordinal_reference_date) * number_of_hours_in_day + number_of_hours_in_day + date.hour()
return int(hour)
hour_ = (ordinal_date - ordinal_reference_date) * number_of_hours_in_day + number_of_hours_in_day + date.hour()
return int(hour_)
def createDateTimeFromMillis(millis):
def createDateTimeFromMillis(millis): # pylint: disable=redefined-outer-name
"""
Returns a DateTime object, build from the number of milliseconds since epoch.
Parameter should be a int or long.
......@@ -530,7 +529,7 @@ def atTheEndOfPeriod(date, period):
return end
def copyDate(date, year=None, month=None, day=None,
hour=None, minute=None, second=None, timezone=None):
hour=None, minute=None, second=None, timezone=None): # pylint: disable=redefined-outer-name
if year is None:
year = date.year()
if month is None:
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Module Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>default_reference</string> </key>
<value> <string>DateUtils</string> </value>
</item>
<item>
<key> <string>default_source_reference</string> </key>
<value> <string>Products.ERP5Type.DateUtils</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>module.erp5.DateUtils</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Module Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_log</string> </key>
<value>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -2909,7 +2909,7 @@ class SimulationTool(BaseTool):
activate_kw=kw)
return active_process
from Products.ERP5Type.DateUtils import addToDate
from erp5.component.module.DateUtils import addToDate
class SequenceItem:
"""
......
module.erp5.DateUtils
module.erp5.ExpandPolicy
module.erp5.GeneratedAmountList
module.erp5.MovementCollectionDiff
......
......@@ -31,8 +31,6 @@ from AccessControl import ClassSecurityInfo
from Products.ERP5Type.Globals import InitializeClass
from Products.ERP5Type import Permissions
from Products.ERP5Type.Utils import normaliseUrl
from Products.ERP5Type.DateUtils import convertDateToHour,\
number_of_hours_in_day, number_of_hours_in_year
from urlparse import urlsplit, urlunsplit
from lxml import html as etree_html
......@@ -65,6 +63,9 @@ class CrawlableMixin:
"""
frequency_index = self.getFrequencyIndex()
if not frequency_index: return -1 # If not update frequency is provided, make sure we never update
from erp5.component.module.DateUtils import convertDateToHour,\
number_of_hours_in_day, number_of_hours_in_year
hour = convertDateToHour(date=self.getCreationDate())
creation_date_index = hour % frequency_index
# in the case of bisextile year, we substract 24 hours from the creation date,
......
  • I did not know this module was used so much. I never use it, I think it's buggy and that doing date arithmetics with datetime.timedelta is better.

    If we break compatibility, my personal opinion (but that's only my opinion) is that we should have just removed this module and rewrite it to use python standard library instead.

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