Added pictograms table
authorThierry Florac <thierry.florac@onf.fr>
Mon, 12 Mar 2018 17:10:31 +0100
changeset 470 2b552177c142
parent 469 66563c8433cf
child 471 979e84932b9f
Added pictograms table
src/pyams_content/generations/__init__.py
src/pyams_content/reference/pictograms/__init__.py
src/pyams_content/reference/pictograms/interfaces/__init__.py
src/pyams_content/reference/pictograms/zmi/__init__.py
src/pyams_content/root/__init__.py
src/pyams_content/root/interfaces/__init__.py
--- a/src/pyams_content/generations/__init__.py	Mon Mar 12 16:45:23 2018 +0100
+++ b/src/pyams_content/generations/__init__.py	Mon Mar 12 17:10:31 2018 +0100
@@ -33,6 +33,7 @@
 from pyams_catalog.nltk import NltkFullTextProcessor
 from pyams_catalog.site import check_required_indexes
 from pyams_content.reference import ReferencesManager
+from pyams_content.reference.pictograms import PictogramTable
 from pyams_content.shared.common.manager import SharedToolContainer
 from pyams_content.shared.form.manager import FormsManager
 from pyams_content.shared.imagemap.manager import ImageMapsManager
@@ -53,7 +54,9 @@
 
 REQUIRED_UTILITIES = ()
 
-REQUIRED_TABLES = ()
+REQUIRED_TABLES = (
+    ('pictograms_table_name', 'pictograms', PictogramTable),
+)
 
 REQUIRED_TOOLS = [
     ('views', ViewsManager),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/reference/pictograms/__init__.py	Mon Mar 12 17:10:31 2018 +0100
@@ -0,0 +1,71 @@
+#
+# 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_content.reference.pictograms.interfaces import IPictogramTable, IPictogram
+from pyams_i18n.interfaces import II18n
+from zope.component.interfaces import ISite
+from zope.lifecycleevent import IObjectAddedEvent
+
+# import packages
+from pyams_content.reference import ReferenceTable, ReferenceInfo
+from pyams_i18n.property import I18nFileProperty
+from pyams_utils.registry import query_utility
+from pyams_utils.request import check_request
+from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
+from pyramid.events import subscriber
+from zope.interface import implementer
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
+
+@implementer(IPictogramTable)
+class PictogramTable(ReferenceTable):
+    """Pictogram table"""
+
+
+@subscriber(IObjectAddedEvent, context_selector=IPictogramTable)
+def handle_added_pictogram_table(event):
+    """Handle new pictogram table"""
+    site = get_parent(event.object, ISite)
+    registry = site.getSiteManager()
+    if registry is not None:
+        registry.registerUtility(event.object, IPictogramTable)
+
+
+@implementer(IPictogram)
+class Pictogram(ReferenceInfo):
+    """Pictogram persistent class"""
+
+    image = I18nFileProperty(IPictogram['image'])
+
+
+@vocabulary_config(name='PyAMS pictograms')
+class PictogramsVocabulary(SimpleVocabulary):
+    """Pictograms vocabulary"""
+
+    def __init__(self, context=None):
+        table = query_utility(IPictogramTable)
+        if table is not None:
+            request = check_request()
+            terms = [SimpleTerm(v,
+                                token=v.__name__,
+                                title=II18n(v).query_attribute('title', request=request))
+                     for v in table.values()]
+        else:
+            terms = []
+        super(PictogramsVocabulary, self).__init__(terms)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/reference/pictograms/interfaces/__init__.py	Mon Mar 12 17:10:31 2018 +0100
@@ -0,0 +1,41 @@
+#
+# 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_content.reference.interfaces import IReferenceInfo, IReferenceTable
+
+# import packages
+from pyams_i18n.schema import I18nImageField
+from zope.container.constraints import containers, contains
+
+from pyams_content import _
+
+
+class IPictogram(IReferenceInfo):
+    """Pictogram interface"""
+
+    containers('.IPictogramTable')
+
+    image = I18nImageField(title=_("Image"),
+                           description=_("Pictogram content"),
+                           required=True)
+
+
+class IPictogramTable(IReferenceTable):
+    """Pictograms table interface"""
+
+    contains(IPictogram)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/reference/pictograms/zmi/__init__.py	Mon Mar 12 17:10:31 2018 +0100
@@ -0,0 +1,180 @@
+#
+# 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 sys
+
+from random import randint
+from uuid import uuid4
+
+# import interfaces
+from pyams_content.interfaces import MANAGE_SITE_ROOT_PERMISSION, IBaseContent
+from pyams_content.reference.pictograms.interfaces import IPictogramTable, IPictogram
+from pyams_content.reference.zmi.table import ReferenceTableContentsTable, ReferenceTableContentsView
+from pyams_form.form import AJAXAddForm, AJAXEditForm
+from pyams_i18n.interfaces import II18n
+from pyams_skin.interfaces.viewlet import IWidgetTitleViewletManager
+from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
+from pyams_zmi.layer import IAdminLayer
+from z3c.table.interfaces import IColumn
+
+# import packages
+from pyams_content.reference.pictograms import Pictogram
+from pyams_i18n.column import I18nAttrColumn
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.table import I18nColumn
+from pyams_skin.viewlet.toolbar import ToolbarAction
+from pyams_utils.adapter import adapter_config
+from pyams_utils.url import absolute_url
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_zmi.form import AdminDialogAddForm, AdminDialogEditForm
+from pyramid.view import view_config
+from z3c.form import field
+from z3c.table.column import GetAttrColumn
+
+from pyams_content import _
+
+
+@viewlet_config(name='add-pictogram.action', context=IPictogramTable, layer=IPyAMSLayer,
+                manager=IWidgetTitleViewletManager, view=ReferenceTableContentsTable,
+                permission=MANAGE_SITE_ROOT_PERMISSION)
+class PictogramAddAction(ToolbarAction):
+    """Pictogram add action"""
+
+    label = _("Add pictogram")
+
+    url = 'add-pictogram.html'
+    modal_target = True
+
+
+@pagelet_config(name='add-pictogram.html', context=IPictogramTable, layer=IPyAMSLayer,
+                permission=MANAGE_SITE_ROOT_PERMISSION)
+class PictogramAddForm(AdminDialogAddForm):
+    """Pictogram add form"""
+
+    legend = _("Add new pictogram")
+
+    fields = field.Fields(IPictogram).omit('__parent__', '__name__')
+    ajax_handler = 'add-pictogram.json'
+    edit_permission = MANAGE_SITE_ROOT_PERMISSION
+
+    def create(self, data):
+        return Pictogram()
+
+    def add(self, object):
+        name = str(uuid4())
+        self.context[name] = object
+
+    def nextURL(self):
+        return 'contents.html'
+
+
+@view_config(name='add-pictogram.json', context=IPictogramTable, request_type=IPyAMSLayer,
+             permission=MANAGE_SITE_ROOT_PERMISSION, renderer='json', xhr=True)
+class PictogramAJAXAddForm(AJAXAddForm, PictogramAddForm):
+    """Pictogram add form, JSON renderer"""
+
+
+@pagelet_config(name='properties.html', context=IPictogram, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
+class PictogramEditForm(AdminDialogEditForm):
+    """Pictogram properties edit form"""
+
+    legend = _("Edit pictogram properties")
+    dialog_class = 'modal-large'
+
+    fields = field.Fields(IPictogram).omit('__parent__', '__name__')
+    ajax_handler = 'properties.json'
+    edit_permission = MANAGE_SITE_ROOT_PERMISSION
+
+
+@view_config(name='properties.json', context=IPictogram, request_type=IPyAMSLayer,
+             permission=MANAGE_SITE_ROOT_PERMISSION, renderer='json', xhr=True)
+class PictogramAJAXEditForm(AJAXEditForm, PictogramEditForm):
+    """Pictogram edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        output = super(PictogramAJAXEditForm, self).get_ajax_output(changes)
+        if 'image' in changes.get(IPictogram, ()):
+            image = II18n(self.context).query_attribute('image', request=self.request)
+            timestamp = randint(0, sys.maxsize)
+            output.setdefault('events', []).append({
+                'event': 'myams.refresh',
+                'options': {
+                    'handler': 'MyAMS.skin.refreshRowCell',
+                    'object_id': '{0}::{1}'.format(ReferenceTableContentsTable.id, self.context.__name__),
+                    'col_name': 'image',
+                    'cell': '<img src="{0}?_={1}" />'.format(absolute_url(image, self.request, '++thumb++32x32'),
+                                                             timestamp)
+                }
+            })
+        if 'title' in changes.get(IBaseContent, ()):
+            output.setdefault('events', []).append({
+                'event': 'myams.refresh',
+                'options': {
+                    'handler': 'MyAMS.skin.refreshRowCell',
+                    'object_id': '{0}::{1}'.format(ReferenceTableContentsTable.id, self.context.__name__),
+                    'col_name': 'name',
+                    'cell': II18n(self.context).query_attribute('title', request=self.request)
+                }
+            })
+        return output
+
+
+#
+# Pictogram table
+#
+
+class PictogramTableContentsTable(ReferenceTableContentsTable):
+    """Pictograms table contents table"""
+
+    @property
+    def data_attributes(self):
+        attributes = super(PictogramTableContentsTable, self).data_attributes
+        attributes.setdefault('table', {}).update({
+            'data-ams-datatable-sorting': '1,asc'
+        })
+        return attributes
+
+
+@adapter_config(name='image', context=(IPictogramTable, IAdminLayer, PictogramTableContentsTable), provides=IColumn)
+class PictogramTableImageCOlumn(GetAttrColumn):
+    """Pictogram table image column"""
+
+    header = ''
+    weight = 1
+
+    cssClasses = {'td': 'text-center width-50'}
+    dt_sortable = 'false'
+
+    def getValue(self, obj):
+        image = II18n(obj).query_attribute('image', request=self.request)
+        if image:
+            return '<img src="{0}" />'.format(absolute_url(image, self.request, '++thumb++32x32'))
+
+
+@adapter_config(name='name', context=(IPictogramTable, IAdminLayer, PictogramTableContentsTable), provides=IColumn)
+class PictogramTableNameColumn(I18nColumn, I18nAttrColumn):
+    """Pictogram table name column"""
+
+    _header = _("Title")
+    weight = 10
+    attrName = 'title'
+
+
+@pagelet_config(name='contents.html', context=IPictogramTable, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
+class PictogramTableContentsView(ReferenceTableContentsView):
+    """Pictograms table contents view"""
+
+    table_class = PictogramTableContentsTable
--- a/src/pyams_content/root/__init__.py	Mon Mar 12 16:45:23 2018 +0100
+++ b/src/pyams_content/root/__init__.py	Mon Mar 12 17:10:31 2018 +0100
@@ -113,6 +113,8 @@
 
     tables_name = None
 
+    pictograms_table_name = None
+
     tools_name = None
 
     views_tool_name = None
--- a/src/pyams_content/root/interfaces/__init__.py	Mon Mar 12 16:45:23 2018 +0100
+++ b/src/pyams_content/root/interfaces/__init__.py	Mon Mar 12 17:10:31 2018 +0100
@@ -60,6 +60,8 @@
 
     tables_name = Attribute("References tables manager name")
 
+    pictograms_table_name = Attribute("Pictograms reference table name")
+
     tools_name = Attribute("Tools name")
 
     views_tool_name = Attribute("Views tool name")