Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Adds a timezone-aware datetime field
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Apr 29, 2019
1 parent 30a1403 commit 0a78cbb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
@@ -1,5 +1,9 @@
Changelog
---------

- Adds a timezone-aware datetime field.
[href]

0.46.1 (2019-04-22)
~~~~~~~~~~~~~~~~~~~

Expand Down
28 changes: 28 additions & 0 deletions onegov/form/fields.py
@@ -1,5 +1,6 @@
import inspect
import phonenumbers
import sedate

from onegov.core.html import sanitize_html
from onegov.core.utils import binary_to_dictionary
Expand All @@ -22,6 +23,7 @@
from wtforms.fields import Field
from wtforms.validators import DataRequired
from wtforms.validators import InputRequired
from wtforms.fields.html5 import DateTimeField


class MultiCheckboxField(SelectMultipleField):
Expand Down Expand Up @@ -246,3 +248,29 @@ def __init__(self, *args, **kwargs):

def populate_obj(self, obj, name):
pass


class TimezoneDateTimeField(DateTimeField):
""" A datetime field data returns the date with the given timezone
and expects dateime values with a timezone.
Used together with :class:`onegov.core.orm.types.UTCDateTime`.
"""

def __init__(self, *args, **kwargs):
self.timezone = kwargs.pop('timezone')
super().__init__(*args, **kwargs)

def process_data(self, value):
if value:
value = sedate.to_timezone(value, self.timezone)
value.replace(tzinfo=None)

super().process_data(value)

def process_formdata(self, valuelist):
super().process_formdata(valuelist)

if self.data:
self.data = sedate.replace_timezone(self.data, self.timezone)

0 comments on commit 0a78cbb

Please sign in to comment.