-
-
Notifications
You must be signed in to change notification settings - Fork 105
Handle start_timezone and end_timezone during (de)serialization #2018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
338ab4e
deecfd0
198e31b
a5494c9
563bb02
2811085
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add `start_timezone` and `end_timezone` to serialized Events. | ||
| During deserialization, store values in these timezones if specified. | ||
| @davisagli |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from plone.restapi.interfaces import IFieldDeserializer | ||
| from plone.restapi.services.content.tus import TUSUpload | ||
| from pytz import timezone | ||
| from pytz import UnknownTimeZoneError | ||
| from pytz import utc | ||
| from z3c.form.interfaces import IDataManager | ||
| from zope.component import adapter | ||
|
|
@@ -89,6 +90,9 @@ def __call__(self, value): | |
| @implementer(IFieldDeserializer) | ||
| @adapter(IDatetime, IDexterityContent, IBrowserRequest) | ||
| class DatetimeFieldDeserializer(DefaultFieldDeserializer): | ||
|
|
||
| requested_timezone = None | ||
|
|
||
| def __call__(self, value): | ||
| # This happens when a 'null' is posted for a non-required field. | ||
| if value is None: | ||
|
|
@@ -119,6 +123,14 @@ def __call__(self, value): | |
| # The IPublication adapter is a special case that expects | ||
| # a timezone-naive local datetime | ||
| value = dt.astimezone().replace(tzinfo=None) | ||
| elif self.requested_timezone is not None: | ||
| # Use the requested timezone if set | ||
| try: | ||
| tz = timezone(self.requested_timezone) | ||
| except UnknownTimeZoneError: | ||
| raise ValueError(f"Unknown timezone: {self.requested_timezone}") | ||
| else: | ||
| value = tz.normalize(dt.astimezone(tz)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davisagli I was chewing on this, as I told you at the Buschenschanksprint - but checking the rest api and your PR there, I recognize that all datetimes are always converted to UTC for serialization/deserialization. |
||
| else: | ||
| # Otherwise let's check what is currently stored. | ||
| dm = queryMultiAdapter((self.context, self.field), IDataManager) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that the original issue requests adding timezone serialization for events, but shouldn't we do this for all datetime fields?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't assume that. We've only been thinking about events as we design the solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I'll keep this in mind, but first I want to make sure everything works end to end with the frontend widgets.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, we should do it for all at one point. But for now out of scope I think.