Added duration and age range to resources
authorThierry Florac <tflorac@ulthar.net>
Fri, 18 Jan 2019 15:31:28 +0100
changeset 1244 4ad4c1cf673a
parent 1243 837612af4dc3
child 1245 971b77c0cbc4
Added duration and age range to resources
src/pyams_content/shared/resource/__init__.py
src/pyams_content/shared/resource/interfaces.py
src/pyams_content/shared/resource/schema.py
src/pyams_content/shared/resource/zmi/interfaces.py
src/pyams_content/shared/resource/zmi/widget.py
--- a/src/pyams_content/shared/resource/__init__.py	Thu Jan 17 16:42:32 2019 +0100
+++ b/src/pyams_content/shared/resource/__init__.py	Fri Jan 18 15:31:28 2019 +0100
@@ -27,6 +27,7 @@
 from pyams_content.shared.common.types import WfTypedSharedContent
 from pyams_content.shared.resource.interfaces import IResource, IResourceInfo, IWfResource, IWfResourceFactory, \
     RESOURCE_CONTENT_NAME, RESOURCE_CONTENT_TYPE, RESOURCE_INFO_ANNOTATIONS_KEY
+from pyams_content.shared.resource.schema import IAgeRange
 from pyams_utils.adapter import adapter_config, get_annotation_adapter
 from pyams_utils.factory import factory_config
 
@@ -65,6 +66,8 @@
     volume = FieldProperty(IResourceInfo['volume'])
     format = FieldProperty(IResourceInfo['format'])
     nb_pages = FieldProperty(IResourceInfo['nb_pages'])
+    duration = FieldProperty(IResourceInfo['duration'])
+    age_range = FieldProperty(IResourceInfo['age_range'])
     release_year = FieldProperty(IResourceInfo['release_year'])
     awards = FieldProperty(IResourceInfo['awards'])
     editor_reference = FieldProperty(IResourceInfo['editor_reference'])
--- a/src/pyams_content/shared/resource/interfaces.py	Thu Jan 17 16:42:32 2019 +0100
+++ b/src/pyams_content/shared/resource/interfaces.py	Fri Jan 18 15:31:28 2019 +0100
@@ -21,6 +21,7 @@
 from pyams_content.shared.common import ISharedContent
 from pyams_content.shared.common.interfaces.types import ITypedSharedToolPortalContext, \
     IWfTypedSharedContentPortalContext
+from pyams_content.shared.resource.schema import AgeRangeField
 from pyams_i18n.schema import I18nHTMLField, I18nTextField
 from pyams_sequence.interfaces import IInternalReferencesList
 
@@ -107,6 +108,12 @@
                    min=0,
                    required=False)
 
+    duration = TextLine(title=_("Duration"),
+                        required=False)
+
+    age_range = AgeRangeField(title=_("Age range"),
+                              required=False)
+
     release_year = Choice(title=_("Release year"),
                           values=range(datetime.today().year, 1970, -1),
                           required=False)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/resource/schema.py	Fri Jan 18 15:31:28 2019 +0100
@@ -0,0 +1,55 @@
+#
+# Copyright (c) 2008-2019 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'
+
+from zope.interface import Interface, implementer
+from zope.schema import Int, Object
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.interfaces import IObject
+
+from pyams_content import _
+
+
+class IAgeRange(Interface):
+    """Age range attribute interface"""
+
+    min_value = Int(title=_("Minimum age"),
+                    required=False)
+
+    max_value = Int(title=_("Maximum age"),
+                    required=False)
+
+    def __bool__(self):
+        return self.min_value or self.max_value
+
+
+@implementer(IAgeRange)
+class AgeRange(object):
+    """Age range attribute object"""
+
+    min_value = FieldProperty(IAgeRange['min_value'])
+    max_value = FieldProperty(IAgeRange['max_value'])
+
+
+class IAgeRangeField(IObject):
+    """Age range schema field interface"""
+
+
+@implementer(IAgeRangeField)
+class AgeRangeField(Object):
+    """Age range schema field class"""
+
+    def __init__(self, **kwargs):
+        if 'schema' in kwargs:
+            del kwargs['schema']
+        super(AgeRangeField, self).__init__(IAgeRange, **kwargs)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/resource/zmi/interfaces.py	Fri Jan 18 15:31:28 2019 +0100
@@ -0,0 +1,19 @@
+#
+# Copyright (c) 2008-2019 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'
+
+from z3c.form.interfaces import IObjectWidget
+
+
+class IAgeRangeWidget(IObjectWidget):
+    """Age range widget interface"""
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/resource/zmi/widget.py	Fri Jan 18 15:31:28 2019 +0100
@@ -0,0 +1,64 @@
+#
+# Copyright (c) 2008-2019 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'
+
+from z3c.form.browser.object import ObjectWidget
+from z3c.form.interfaces import IFieldWidget, IObjectFactory
+from z3c.form.object import getIfName
+from z3c.form.widget import FieldWidget
+from zope.interface import Interface, alsoProvides, implementer_only
+
+from pyams_content.shared.resource import IAgeRange
+from pyams_content.shared.resource.schema import AgeRange, IAgeRangeField
+from pyams_content.shared.resource.zmi.interfaces import IAgeRangeWidget
+from pyams_form.interfaces import IForm, IFormLayer
+from pyams_utils.adapter import adapter_config
+from pyams_utils.interfaces.data import IObjectData
+
+
+@adapter_config(name=getIfName(IAgeRange),
+                context=(Interface, IFormLayer, IForm, IAgeRangeWidget), provides=IObjectFactory)
+class AgeRangeObjectFactory(object):
+    """Age range object factory"""
+
+    def __init__(self, context, request, form, widget):
+        self.context = context
+        self.request = request
+        self.form = form
+        self.widget = widget
+
+    def __call__(self, data):
+        return AgeRange()
+
+
+@implementer_only(IAgeRangeWidget)
+class AgeRangeWidget(ObjectWidget):
+    """Age range widget"""
+
+    def updateWidgets(self, setErrors=True):
+        super(AgeRangeWidget, self).updateWidgets(setErrors)
+        widgets = self.subform.widgets
+        for name in ('min_value', 'max_value'):
+            widget = widgets[name]
+            widget.label_css_class = 'control-label col-md-2'
+            widget.input_css_class = 'col-md-1'
+            widget.object_data = {
+                'input-mask': '9{1,3}'
+            }
+            alsoProvides(widget, IObjectData)
+
+
+@adapter_config(context=(IAgeRangeField, IFormLayer), provides=IFieldWidget)
+def AgeRangeFieldWidget(field, request):
+    """Age range field widget factory"""
+    return FieldWidget(field, AgeRangeWidget(request))