Set timezone for creation and modification dates of comments

This commit is contained in:
Jon Pentland 2022-10-18 16:06:24 +02:00
parent dd0255fd5d
commit 16123e268b
5 changed files with 40 additions and 4 deletions

View File

@ -2,6 +2,7 @@ from AccessControl import getSecurityManager
from AccessControl import Unauthorized
from Acquisition import aq_inner
from datetime import datetime
from datetime import timezone
from DateTime import DateTime
from plone.app.discussion import _
from plone.app.discussion.browser.validator import CaptchaValidator
@ -192,8 +193,8 @@ class CommentForm(extensible.ExtensibleForm, form.Form):
setattr(comment, attribute, data[attribute])
# Set dates
comment.creation_date = datetime.utcnow()
comment.modification_date = datetime.utcnow()
comment.creation_date = datetime.now(timezone.utc)
comment.modification_date = datetime.now(timezone.utc)
# Get author name and email
comment.author_name, comment.author_email = self.get_author(data)

View File

@ -6,6 +6,7 @@ from Acquisition import aq_base
from Acquisition import aq_parent
from Acquisition import Implicit
from datetime import datetime
from datetime import timezone
from OFS.owner import Owned
from OFS.role import RoleManager
from OFS.Traversable import Traversable
@ -119,7 +120,7 @@ class Comment(
# IConversation.addComment().
def __init__(self):
self.creation_date = self.modification_date = datetime.utcnow()
self.creation_date = self.modification_date = datetime.now(timezone.utc)
self.mime_type = "text/plain"
user = getSecurityManager().getUser()

View File

@ -1,5 +1,5 @@
<metadata>
<version>2000</version>
<version>2001</version>
<dependencies>
<dependency>profile-plone.resource:default</dependency>
<dependency>profile-plone.app.registry:default</dependency>

View File

@ -2,6 +2,8 @@ from plone.app.discussion.interfaces import IDiscussionSettings
from plone.registry.interfaces import IRegistry
from Products.CMFCore.utils import getToolByName
from zope.component import getUtility
from plone import api
from datetime import timezone
import logging
@ -77,3 +79,24 @@ def add_js_to_plone_legacy(context):
def extend_review_workflow(context):
"""Apply changes made to review workflow."""
upgrade_comment_workflows_retain_current_workflow(context)
def set_timezone_on_dates(context):
"""Ensure timezone data is stored against all creation/modified dates"""
pc = api.portal.get_tool('portal_catalog')
creations = 0
modifieds = 0
logger.info('Setting timezone information on comment dates')
comments = pc.search({'Type': 'Comment'})
for cbrain in comments:
comment = cbrain.getObject()
if not comment.creation_date.tzinfo:
creations += 1
comment.creation_date = \
comment.creation_date.astimezone(timezone.utc)
if not comment.modification_date.tzinfo:
modifieds += 1
comment.modification_date = \
comment.modification_date.astimezone(timezone.utc)
logger.info('Updated %i creation dates and %i modification dates' %
(creations, modifieds))

View File

@ -88,4 +88,15 @@
handler=".upgrades.upgrade_comment_workflows" />
</genericsetup:upgradeSteps>
<genericsetup:upgradeSteps
source="2000"
destination="2001"
profile="plone.app.discussion:default">
<genericsetup:upgradeStep
title="Set timezone on comment dates"
description="Ensure the timezone is set for comment dates"
handler=".upgrades.set_timezone_on_dates"
/>
</genericsetup:upgradeSteps>
</configure>