Moved timezone module to a sub-package with utility and methods to handle server timezone
authorThierry Florac <tflorac@ulthar.net>
Fri, 05 Mar 2010 00:16:27 +0100
changeset 37 393fad193b6b
parent 36 14084f98ac50
child 38 9571328b05d2
Moved timezone module to a sub-package with utility and methods to handle server timezone
ztfy/utils/configure.zcml
ztfy/utils/timezone.py
ztfy/utils/timezone/__init__.py
ztfy/utils/timezone/configure.zcml
ztfy/utils/timezone/interfaces.py
ztfy/utils/timezone/schema.py
ztfy/utils/timezone/utility.py
--- a/ztfy/utils/configure.zcml	Tue Mar 02 23:36:04 2010 +0100
+++ b/ztfy/utils/configure.zcml	Fri Mar 05 00:16:27 2010 +0100
@@ -5,9 +5,7 @@
 
 	<i18n:registerTranslations directory="locales" />
 
-	<adapter
-		factory=".timezone.tzinfo" />
-
 	<include package=".tal" />
+	<include package=".timezone" />
 
 </configure>
--- a/ztfy/utils/timezone.py	Tue Mar 02 23:36:04 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,62 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2008 Thierry Florac <tflorac AT ulthar.net>
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""
-
-$Id: $
-"""
-
-__version__ = "$Revision: $"
-__release__ = "$Id: $"
-__docformat__ = "restructuredtext"
-
-# import standard packages
-import pytz
-
-# import Zope3 interfaces
-from zope.interface.common.idatetime import ITZInfo
-from zope.publisher.interfaces.browser import IBrowserRequest
-
-# import local interfaces
-
-# import Zope3 packages
-from zope.component import adapter
-from zope.interface import implementer
-
-# import local packages
-
-
-GMT = pytz.timezone('GMT')
-_tz = pytz.timezone('Europe/Paris')
-tz = _tz
-
-@implementer(ITZInfo)
-@adapter(IBrowserRequest)
-def tzinfo(request=None):
-    return tz
-
-
-def tztime(value):
-    if not value:
-        return None
-    if not value.tzinfo:
-        value = GMT.localize(value)
-    return value.astimezone(tz)
-
-
-def gmtime(value):
-    if not value:
-        return None
-    if not value.tzinfo:
-        value = GMT.localize(value)
-    return value.astimezone(GMT)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/timezone/__init__.py	Fri Mar 05 00:16:27 2010 +0100
@@ -0,0 +1,62 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+__docformat__ = "restructuredtext"
+
+# import standard packages
+import pytz
+
+# import Zope3 interfaces
+from zope.interface.common.idatetime import ITZInfo
+from zope.publisher.interfaces.browser import IBrowserRequest
+
+# import local interfaces
+from interfaces import IServerTimezone
+
+# import Zope3 packages
+from zope.app import zapi
+from zope.component import adapter
+from zope.interface import implementer
+
+# import local packages
+
+
+GMT = pytz.timezone('GMT')
+_tz = pytz.timezone('Europe/Paris')
+tz = _tz
+
+@implementer(ITZInfo)
+@adapter(IBrowserRequest)
+def tzinfo(request=None):
+    util = zapi.queryUtility(IServerTimezone)
+    if util is not None:
+        return pytz.timezone(util.timezone)
+    return GMT
+
+
+def tztime(value):
+    if not value:
+        return None
+    if not value.tzinfo:
+        value = GMT.localize(value)
+    return value.astimezone(tzinfo())
+
+
+def gmtime(value):
+    if not value:
+        return None
+    if not value.tzinfo:
+        value = GMT.localize(value)
+    return value.astimezone(GMT)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/timezone/configure.zcml	Fri Mar 05 00:16:27 2010 +0100
@@ -0,0 +1,39 @@
+<configure
+	xmlns="http://namespaces.zope.org/zope"
+	xmlns:browser="http://namespaces.zope.org/browser"
+	i18n_domain="ztfy.utils">
+
+	<adapter
+		factory=".tzinfo" />
+
+	<utility
+		name="ZTFY timezones"
+		component=".schema.TimezonesVocabulary" />
+
+	<class class=".utility.ServerTimezoneUtility">
+		<factory
+			id="ztfy.utils.timezone.ServerTimezone" />
+		<implements
+			interface="zope.annotation.interfaces.IAttributeAnnotatable" />
+		<require
+			interface=".interfaces.IServerTimezone"
+			permission="zope.View" />
+		<require
+			set_schema=".interfaces.IServerTimezone"
+			permission="zope.ManageServices" />
+	</class>
+
+	<browser:addMenuItem
+		title="ZTFY server timezone"
+		description="A server timezone utility is used to define default server timezone"
+		class=".utility.ServerTimezoneUtility"
+		permission="zope.ManageSite" />
+
+	<browser:editform
+		name="properties.html"
+		for=".interfaces.IServerTimezone"
+		schema=".interfaces.IServerTimezone"
+		permission="zope.ManageSite"
+		menu="zmi_views" title="Properties" />
+
+</configure>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/timezone/interfaces.py	Fri Mar 05 00:16:27 2010 +0100
@@ -0,0 +1,37 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+__docformat__ = "restructuredtext"
+
+# import standard packages
+
+# import Zope3 interfaces
+
+# import local interfaces
+
+# import Zope3 packages
+from zope.interface import Interface
+
+# import local packages
+from schema import Timezone
+
+from ztfy.utils import _
+
+
+class IServerTimezone(Interface):
+
+    timezone = Timezone(title=_("Server timezone"),
+                        description=_("Default server timezone"),
+                        required=True)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/timezone/schema.py	Fri Mar 05 00:16:27 2010 +0100
@@ -0,0 +1,57 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+__docformat__ = "restructuredtext"
+
+# import standard packages
+import pytz
+
+# import Zope3 interfaces
+from zope.schema.interfaces import IChoice, IVocabularyFactory
+
+# import local interfaces
+
+# import Zope3 packages
+from zope.interface import implements, classProvides
+from zope.schema import Choice
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
+
+# import local packages
+
+
+class TimezonesVocabulary(SimpleVocabulary):
+
+    classProvides(IVocabularyFactory)
+
+    def __init__(self, *args, **kw):
+        terms = [SimpleTerm(t, t, t) for t in pytz.all_timezones]
+        super(TimezonesVocabulary, self).__init__(terms)
+
+
+class ITimezone(IChoice):
+    """Marker interface for timezone field"""
+
+
+class Timezone(Choice):
+    """Timezone choice field"""
+
+    implements(ITimezone)
+
+    def __init__(self, **kw):
+        if 'vocabulary' in kw:
+            kw.pop('vocabulary')
+        if 'default' not in kw:
+            kw['default'] = u'GMT'
+        super(Timezone, self).__init__(vocabulary='ZTFY timezones', **kw)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/timezone/utility.py	Fri Mar 05 00:16:27 2010 +0100
@@ -0,0 +1,39 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008-2010 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+__docformat__ = "restructuredtext"
+
+# import standard packages
+from persistent import Persistent
+
+# import Zope3 interfaces
+
+# import local interfaces
+from interfaces import IServerTimezone
+
+# import Zope3 packages
+from zope.interface import implements
+from zope.schema.fieldproperty import FieldProperty
+
+# import local packages
+
+from ztfy.utils import _
+
+
+class ServerTimezoneUtility(Persistent):
+
+    implements(IServerTimezone)
+
+    timezone = FieldProperty(IServerTimezone['timezone'])