Skip to content

Commit

Permalink
Corrected index problem when plone.app.contentypes is installed.
Browse files Browse the repository at this point in the history
Used a different datewidget in plone 5.
Indexed dates in plone 4 are timezoned.
  • Loading branch information
sgeulette committed Sep 13, 2018
1 parent 33e98dc commit bf0037f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
install_requires=[
'collective.behavior.talcondition',
'plone.api',
'plone.app.contenttypes',
'plone.app.dexterity',
'plone.app.lockingbehavior',
'plone.formwidget.datetime >= 1.2',
Expand Down
51 changes: 36 additions & 15 deletions src/collective/messagesviewlet/message.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# -*- coding: utf-8 -*-
from collective.messagesviewlet import _
from collective.messagesviewlet import HAS_PLONE_5
from DateTime import DateTime
from datetime import datetime
from zope import schema
from zope.interface import invariant, Invalid, alsoProvides
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
from z3c.form.browser.checkbox import CheckBoxFieldWidget
from z3c.form.browser.radio import RadioFieldWidget

from plone.app.event.base import default_timezone
from plone.app.event.base import localized_now
from plone.app.textfield import RichText
from plone.app.z3cform.widget import DatetimeFieldWidget as dtfw5
from plone.autoform import directives as form
from plone.formwidget.datetime.z3cform.widget import DatetimeFieldWidget
from plone.formwidget.datetime.z3cform.widget import DatetimeFieldWidget as dtfw4
from plone.indexer import indexer
from plone.supermodel import model

from collective.messagesviewlet import _
from z3c.form.browser.checkbox import CheckBoxFieldWidget
from z3c.form.browser.radio import RadioFieldWidget
from zope import schema
from zope.interface import alsoProvides
from zope.interface import Invalid
from zope.interface import invariant
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary


def msg_types(context):
Expand Down Expand Up @@ -42,7 +46,8 @@ def generate_uid():


def default_start():
return datetime.now()
now = localized_now()
return now.replace(minute=(now.minute - now.minute % 5), second=0, microsecond=0)


class IMessage(model.Schema):
Expand All @@ -69,6 +74,7 @@ class IMessage(model.Schema):
can_hide = schema.Bool(
title=_(u"Can be marked as read"),
description=_(u"If checked, the user can hide the message"),
default=False
)

start = schema.Datetime(
Expand All @@ -77,15 +83,21 @@ class IMessage(model.Schema):
description=_(u"Specify start date message appearance"),
defaultFactory=default_start,
)
form.widget('start', DatetimeFieldWidget)
if HAS_PLONE_5:
form.widget('start', dtfw5, default_timezone=default_timezone)
else:
form.widget('start', dtfw4)

end = schema.Datetime(
title=_(u"End date"),
required=False,
description=_(u"Specify end date message appearance. If nothing specified, this is infinite. "
"If you pick a date, <span class=warning-formHelp>dont't forget hours !</span>"),
)
form.widget('end', DatetimeFieldWidget)
if HAS_PLONE_5:
form.widget('end', dtfw5, default_timezone=default_timezone)
else:
form.widget('end', dtfw4)

required_roles = schema.Set(
title=_(u'Required roles'),
Expand Down Expand Up @@ -120,18 +132,27 @@ def validateStartEnd(data):
if data.start > data.end:
raise Invalid(_(u"The start date must precede the end date."))

if not HAS_PLONE_5:
TZ = default_timezone(as_tzinfo=True)


def add_timezone(dt):
if not HAS_PLONE_5:
return TZ.localize(dt)
return dt


@indexer(IMessage)
def start_index(obj):
if obj.start is None:
return obj.created()
else:
return obj.start
return add_timezone(obj.start)


@indexer(IMessage)
def end_index(obj):
if obj.end is None:
return DateTime(2099, 01, 01)
else:
return obj.end
return add_timezone(obj.end)
8 changes: 8 additions & 0 deletions src/collective/messagesviewlet/profiles.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:upgradeStep
title="Go to collective.messagesviewlet 2000"
description="Update message start and end dates to add site timezone"
source="*"
destination="2000"
handler="collective.messagesviewlet.upgrades.upgrade_to_2000"
profile="collective.messagesviewlet:default" />

<utility factory=".setuphandlers.HiddenProfiles" name="collective.messagesviewlet" />

</configure>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<metadata>
<version>1000</version>
<version>2000</version>
<dependencies>
<dependency>profile-collective.messagesviewlet:install-base</dependency>
</dependencies>
Expand Down
29 changes: 29 additions & 0 deletions src/collective/messagesviewlet/upgrades.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
import logging
from plone import api
from plone.app.event.base import default_timezone

logger = logging.getLogger('collective.messagesviewlet: upgrade. ')


def upgrade_to_2000(context):
"""
Add timezone to start and end
"""
tzinfo = default_timezone(as_tzinfo=True)
catalog = api.portal.get_tool('portal_catalog')
brains = catalog(portal_type='Message')
logger.info("Found %d messages" % len(brains))
count = 0
import ipdb; ipdb.set_trace()
for brain in brains:
obj = brain.getObject()
correction = False
for attr in ('start', 'end'):
if getattr(obj, attr, False):
setattr(obj, attr, tzinfo.localize(getattr(obj, attr)))
correction = True
if correction:
count += 1
obj.reindexObject(['start', 'end'])
logger.info("Corrected %d messages" % count)

0 comments on commit bf0037f

Please sign in to comment.