I have a container content type that has the field 'end_of_week', which is by default set to the end of the current week.
The end of week field is set one of two ways:
-The user manually enters it
-The date is programatically set
The end_of_week field is defined as:
end_of_week = schema.Datetime(title='End of Week',
description='Enter date a week ends (on Sat)'
required=True,
)
Unfortunately, I discovered recently that the date programmatically set has tzinfo (set to UTC), whereas the value entered manually doesn't have tzinfo.
When I create the content type programatically, I use:
end_of_week = getWeekRange(myevent.start)['end']
#The 'myevent' object passed, which is to be stored in the container, uses the IEventBasic behavior.
ts_ref = createContentItem(timesheetmanager,
type=my.product.,mycontainertype,
title='concatenated'
end_of_week = getWeekRange(entry.start)['end'],
)
In a utils .py file, I have the function that I use to get the end of the week:
def getWeekRange(date_of_entry,offset=1, tz_to_localize=None):
"""Returna dict cotaining the starting and ending dates of a week"""
start = date_of_entry - timedelta(days = (date_of_entry.weekday() + offset) % 7)
end = start + timedelta(days=6)
if tz_to_localize is None:
return {'start':start.replace(hour=0,minute=0),
'end':end.replace(hour=23,minute=59)
}
else:
return {'start':tz_to_localize.localize(datetime(start.year,start.month,start.day,0,0)),
'end':tz_to_localize.localize(datetime(end.year,end.month,end.day,23,59))
}
When I view the portal_catalog in the ZMI, it does not break.
However, when I add the container type manually and then go into ZMI to see it, it breaks.
This is the error I see when I try to view the values for the 'end_of_week' index.
2017-12-01 12:36:35 ERROR Zope.SiteErrorLog 1512149795.840.529882828129 http://localhost:8080/Plone/portal_catalog/Indexes/end_of_week/manage_browse
Traceback (innermost last):
Module ZPublisher.Publish, line 138, in publish
Module ZPublisher.mapply, line 77, in mapply
Module ZPublisher.Publish, line 48, in call_object
Module Shared.DC.Scripts.Bindings, line 322, in __call__
Module Shared.DC.Scripts.Bindings, line 359, in _bindAndExec
Module App.special_dtml, line 185, in _exec
Module DocumentTemplate.DT_Let, line 77, in render
Module DocumentTemplate.DT_In, line 606, in renderwb
Module DocumentTemplate.DT_Util, line 210, in eval
- __traceback_info__: DateTime
Module <string>, line 1, in <module>
Module DateTime.DateTime, line 440, in __init__
Module DateTime.DateTime, line 812, in _parse_args
DateError: Invalid date: (2018, 0, 2, 23, 59, 0, 'UTC')
So right now I'm trying to make an upgrade step that would correct the indexes.
from pytz import utc
def _upgrade_step(context):
catalog = api.portal.get_tool(name='portal_catalog')
brains = catalog.searchResults({'portal_type':'my.product.containertype'})
for i in brains:
obj = i.getObject()
og_date = i['end_of_week']
if obj.end_of_week.tzinfo is None:
obj.end_of_week=utc.localize(datetime(og_date.year, og_date.month, og_date.day, 23, 59))
obj.reindexObject(idxs=['end_of_week'])
Unfortunately, this does not work as I still end up with the same error.
I changed the upgrade step to verify that all the end_of_week values had a timezone assigned.
for i in brains:
print i['end_of_week'], ' ', i['end_of_week'].tzinfo
In the print statements, the results all appear correct as they should, but when I go into the catalog, it breaks.
I noticed in the error message, the time tuple displays the incorrect value for the year parameter, when it should've been 2017 instead of 2018.
What can I do to fix the indexes? An upgrade step would be ideal at the moment.