src/pyams_sequence/site.py
changeset 0 980ffaa51a75
child 28 83dc4748dbbb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_sequence/site.py	Wed Jun 17 09:59:43 2015 +0200
@@ -0,0 +1,56 @@
+#
+# Copyright (c) 2008-2015 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 library
+
+# import interfaces
+from pyams_sequence.interfaces import ISequentialIntIds, ISequentialIdInfo
+from pyams_utils.interfaces.site import ISiteGenerations
+from zope.site.interfaces import INewLocalSite
+
+# import packages
+from pyams_catalog.index import FieldIndexWithInterface
+from pyams_catalog.site import check_required_indexes
+from pyams_sequence.utility import SequentialIntIds
+from pyams_utils.registry import utility_config
+from pyams_utils.site import check_required_utilities
+from pyramid.events import subscriber
+
+
+REQUIRED_UTILITIES = ((ISequentialIntIds, '', SequentialIntIds, 'Sequential IDs'), )
+
+
+REQUIRED_INDEXES = (('oid', FieldIndexWithInterface, {'interface': ISequentialIdInfo,
+                                                      'discriminator': 'hex_oid'}), )
+
+
+@subscriber(INewLocalSite)
+def handle_new_local_site(event):
+    """Check for required utilities when a site is created"""
+    site = event.manager.__parent__
+    check_required_utilities(site, REQUIRED_UTILITIES)
+    check_required_indexes(site, REQUIRED_INDEXES)
+
+
+@utility_config(name='PyAMS sequence', provides=ISiteGenerations)
+class SequenceGenerationsChecker(object):
+    """PyAMS sequence generations checker"""
+
+    generation = 1
+
+    def evolve(self, site, current=None):
+        """Check for required utilities"""
+        check_required_utilities(site, REQUIRED_UTILITIES)
+        check_required_indexes(site, REQUIRED_INDEXES)