Added "truncate" TALES extension
authorThierry Florac <tflorac@ulthar.net>
Wed, 14 Nov 2018 17:56:13 +0100
changeset 280 f37f19b7fff3
parent 279 1506cc3bc32e (diff)
parent 278 5ef130497172 (current diff)
child 281 b04690a8d80f
Added "truncate" TALES extension
src/pyams_utils/text.py
--- a/src/pyams_utils/text.py	Wed Nov 14 15:52:57 2018 +0100
+++ b/src/pyams_utils/text.py	Wed Nov 14 17:56:13 2018 +0100
@@ -55,6 +55,22 @@
     return text
 
 
+@adapter_config(name='truncate', context=(Interface, Interface, Interface), provides=ITALESExtension)
+class TruncateCharsTalesExtension(ContextRequestViewAdapter):
+    """extension:truncate(value, length, max) TALES expression
+
+    Truncates a sentence if it is longer than the specified 'length' characters.
+    Truncated strings will end with an ellipsis character (“…”)
+    See also 'get_text_start'
+    """
+
+    @staticmethod
+    def render(value, length=50, max=0):
+        if not value:
+            return ''
+        return get_text_start(value, length, max=max)
+
+
 @adapter_config(name='raw', context=(str, IRequest), provides=IHTMLRenderer)
 class BaseHTMLRenderer(ContextRequestAdapter):
     """Raw text HTML renderer
@@ -185,18 +201,3 @@
         if end_tag:
             elements[-1] = '<{0}>{1}</{0}>'.format(end_tag, elements[-1])
         return br.join(elements)
-
-@adapter_config(name='truncatechars', context=(Interface, Interface, Interface), provides=ITALESExtension)
-class TruncateCharsTalesExtension(ContextRequestViewAdapter):
-    """extension:truncatechars(value, length, max) TALES expression
-
-    Truncates a sentence if it is longer than the specified 'length' of characters.
-    Truncated strings will end with a ellipsis character (“…”)
-    see also 'get_text_start'
-    """
-
-    @staticmethod
-    def render(value, length=50, max=0):
-        if not value:
-            return ''
-        return get_text_start(value, length, max=max)
--- a/src/pyams_utils/zmi/intids.py	Wed Nov 14 15:52:57 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-#
-# 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_skin.layer import IPyAMSLayer
-from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
-from pyams_utils.interfaces.intids import IIndexLength
-from zope.intid.interfaces import IIntIds
-
-# import packages
-from pyams_pagelet.pagelet import pagelet_config
-from pyams_utils.adapter import ContextAdapter, adapter_config
-from pyams_zmi.form import AdminDialogDisplayForm
-from z3c.form import field
-
-from pyams_utils import _
-
-
-@adapter_config(context=IIntIds, provides=IIndexLength)
-class IntIdsLengthAdapter(ContextAdapter):
-    """IntIds length adapter"""
-
-    @property
-    def count(self):
-        return len(self.context)
-
-
-@pagelet_config(name='properties.html', context=IIntIds, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
-class IntIdsPropertiesDisplayForm(AdminDialogDisplayForm):
-    """intIds properties edit form"""
-
-    legend = _("Display indexer properties")
-    fields = field.Fields(IIndexLength)
-
-    @property
-    def title(self):
-        return self.context.__name__
--- a/src/pyams_utils/zmi/templates/connection-test.pt	Wed Nov 14 15:52:57 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-<div class="no-widget-toolbar">
-	<pre tal:attributes="id string:${view.__parent__.id}_test_result"></pre>
-</div>
--- a/src/pyams_utils/zmi/timezone.py	Wed Nov 14 15:52:57 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,57 +0,0 @@
-#
-# 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_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION
-from pyams_utils.interfaces.timezone import IServerTimezone
-from pyams_skin.layer import IPyAMSLayer
-
-# import packages
-from pyams_form.form import AJAXEditForm
-from pyams_pagelet.pagelet import pagelet_config
-from pyams_zmi.form import AdminDialogEditForm
-from pyramid.view import view_config
-from z3c.form import field
-
-from pyams_utils import _
-
-
-@pagelet_config(name='properties.html', context=IServerTimezone, layer=IPyAMSLayer,
-                permission=VIEW_SYSTEM_PERMISSION)
-class ServerTimezonePropertiesEditForm(AdminDialogEditForm):
-    """Server timezone properties edit form"""
-
-    prefix = 'timezone_properties.'
-
-    legend = _("Update server timezone properties")
-    fields = field.Fields(IServerTimezone)
-    ajax_handler = 'properties.json'
-    edit_permission = MANAGE_SYSTEM_PERMISSION
-
-    @property
-    def title(self):
-        return self.context.__name__
-
-    def updateWidgets(self, prefix=None):
-        super(ServerTimezonePropertiesEditForm, self).updateWidgets()
-        self.widgets['timezone'].addClass('select2')
-
-
-@view_config(name='properties.json', context=IServerTimezone, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class ServerTimezonePropertiesAJAXEditForm(AJAXEditForm, ServerTimezonePropertiesEditForm):
-    """Server timezone properties edit form, AJAX renderer"""
--- a/src/pyams_utils/zmi/zeo.py	Wed Nov 14 15:52:57 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,237 +0,0 @@
-#
-# 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 pprint
-import sys
-import traceback
-
-# import interfaces
-from pyams_form.interfaces.form import IWidgetsSuffixViewletsManager
-from pyams_skin.interfaces.container import ITableElementName
-from pyams_skin.interfaces.viewlet import IToolbarAddingMenu, ITableItemColumnActionsMenu
-from pyams_skin.layer import IPyAMSLayer
-from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION
-from pyams_utils.interfaces.zeo import IZEOConnection
-from pyams_zmi.layer import IAdminLayer
-from z3c.form.interfaces import IDataExtractedEvent, DISPLAY_MODE
-from zope.component.interfaces import ISite
-
-# import packages
-from pyams_form.form import AJAXAddForm, AJAXEditForm
-from pyams_form.schema import CloseButton
-from pyams_pagelet.pagelet import pagelet_config
-from pyams_skin.viewlet.toolbar import ToolbarMenuItem
-from pyams_template.template import template_config
-from pyams_utils.adapter import adapter_config, ContextRequestAdapter
-from pyams_utils.registry import query_utility
-from pyams_utils.url import absolute_url
-from pyams_utils.zodb import ZEOConnectionUtility
-from pyams_viewlet.viewlet import viewlet_config, Viewlet
-from pyams_zmi.form import AdminDialogAddForm, AdminDialogEditForm
-from pyams_zmi.zmi.control_panel import UtilitiesTable
-from pyramid.events import subscriber
-from pyramid.view import view_config
-from z3c.form import field, button
-from zope.interface import Interface, Invalid
-
-from pyams_utils import _
-
-
-@adapter_config(context=(IZEOConnection, IAdminLayer), provides=ITableElementName)
-class ZEOConnectionNameAdapter(ContextRequestAdapter):
-    """ZEO connection name adapter"""
-
-    @property
-    def name(self):
-        translate = self.request.localizer.translate
-        return translate(_("ZEO: {0}")).format(self.context.name)
-
-
-@viewlet_config(name='add-zeo-connection.menu', context=ISite, layer=IAdminLayer,
-                view=UtilitiesTable, manager=IToolbarAddingMenu, permission=MANAGE_SYSTEM_PERMISSION)
-class ZEOConnectionAddMenu(ToolbarMenuItem):
-    """ZEO connection add menu"""
-
-    label = _("Add ZEO connection...")
-    label_css_class = 'fa fa-fw fa-database'
-    url = 'add-zeo-connection.html'
-    modal_target = True
-
-
-@pagelet_config(name='add-zeo-connection.html', context=ISite, layer=IPyAMSLayer, permission=MANAGE_SYSTEM_PERMISSION)
-class ZEOConnectionAddForm(AdminDialogAddForm):
-    """ZEO connection add form"""
-
-    title = _("Utilities")
-    legend = _("Add ZEO connection")
-    icon_css_class = 'fa fa-fw fa-database'
-
-    fields = field.Fields(IZEOConnection)
-    autocomplete = 'off'
-    ajax_handler = 'add-zeo-connection.json'
-    edit_permission = None
-
-    def create(self, data):
-        return ZEOConnectionUtility()
-
-    def add(self, object):
-        manager = self.context.getSiteManager()
-        manager['zeo::{0}'.format(object.name.lower())] = object
-
-    def nextURL(self):
-        return absolute_url(self.context, self.request, 'utilities.html')
-
-
-@subscriber(IDataExtractedEvent, form_selector=ZEOConnectionAddForm)
-def handle_new_connection_data_extraction(event):
-    """Handle new connection data extraction"""
-    manager = event.form.context.getSiteManager()
-    name = event.data['name']
-    if 'zeo::{0}'.format(name.lower()) in manager:
-        event.form.widgets.errors += (Invalid(_("Specified connection name is already used!")), )
-    connection = query_utility(IZEOConnection, name=name)
-    if connection is not None:
-        event.form.widgets.errors += (Invalid(_("A ZEO connection is already registered with this name!")), )
-
-
-@view_config(name='add-zeo-connection.json', context=ISite, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class ZEOConnectionAJAXAddForm(AJAXAddForm, ZEOConnectionAddForm):
-    """ZEO connection add form, AJAX view"""
-
-
-@pagelet_config(name='properties.html', context=IZEOConnection, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
-class ZEOConnectionPropertiesEditForm(AdminDialogEditForm):
-    """ZEO connection properties edit form"""
-
-    prefix = 'zeo_properties.'
-
-    @property
-    def title(self):
-        translate = self.request.localizer.translate
-        return translate(_("ZEO connection: {0}")).format(self.context.name)
-
-    legend = _("Update ZEO connection properties")
-    icon_css_class = 'fa fa-fw fa-database'
-
-    fields = field.Fields(IZEOConnection)
-    autocomplete = 'off'
-    ajax_handler = 'properties.json'
-    edit_permission = MANAGE_SYSTEM_PERMISSION
-
-    def updateWidgets(self, prefix=None):
-        super(ZEOConnectionPropertiesEditForm, self).updateWidgets(prefix)
-        self.widgets['name'].mode = DISPLAY_MODE
-
-
-@view_config(name='properties.json', context=IZEOConnection, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class ZEOConnectionPropertiesAJAXEditForm(AJAXEditForm, ZEOConnectionPropertiesEditForm):
-    """ZEO connection properties edit form, AJAX view"""
-
-
-#
-# ZEO connection test form
-#
-
-@viewlet_config(name='test-connection.menu', context=IZEOConnection, layer=IAdminLayer,
-                view=UtilitiesTable, manager=ITableItemColumnActionsMenu, permission=MANAGE_SYSTEM_PERMISSION)
-class ZEOConnectionTestMenu(ToolbarMenuItem):
-    """ZEO connection test menu"""
-
-    label = _("Test ZEO connection...")
-    label_css_class = 'fa fa-fw fa-database'
-    url = 'test-connection.html'
-    modal_target = True
-    stop_propagation = True
-
-
-class IZEOConnectionTestButtons(Interface):
-    """ZEO connection test buttons"""
-
-    close = CloseButton(name='close', title=_("Close"))
-    test = button.Button(name='test', title=_("Test connection"))
-
-
-@pagelet_config(name='test-connection.html', context=IZEOConnection, layer=IPyAMSLayer,
-                permission=MANAGE_SYSTEM_PERMISSION)
-class ZEOConnectionTestForm(AdminDialogAddForm):
-    """ZEO connection test form"""
-
-    @property
-    def title(self):
-        translate = self.request.localizer.translate
-        return translate(_("ZEO connection: {0}")).format(self.context.name)
-
-    legend = _("Test ZEO database connection")
-    icon_css_class = 'fa fa-fw fa-database'
-
-    prefix = 'test_form.'
-    fields = field.Fields(Interface)
-    buttons = button.Buttons(IZEOConnectionTestButtons)
-    ajax_handler = 'test-connection.json'
-    edit_permission = MANAGE_SYSTEM_PERMISSION
-
-    @property
-    def form_target(self):
-        return '#{0}_test_result'.format(self.id)
-
-    def updateActions(self):
-        super(ZEOConnectionTestForm, self).updateActions()
-        if 'test' in self.actions:
-            self.actions['test'].addClass('btn-primary')
-
-    def createAndAdd(self, data):
-        try:
-            self.context.get_connection()
-        except:
-            typ, value, tb = sys.exc_info()
-            return 500, (typ, value, tb)
-        else:
-            return 200, 'OK - ZEO connection successful'
-
-
-@viewlet_config(name='test-connection.suffix', layer=IAdminLayer, manager=IWidgetsSuffixViewletsManager,
-                view=ZEOConnectionTestForm, weight=50)
-@template_config(template='templates/connection-test.pt')
-class ZEOConnectionTestFormSuffix(Viewlet):
-    """ZEO connection test form suffix"""
-
-
-@view_config(name='test-connection.json', context=IZEOConnection, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class ZEOConnectionAJAXTestForm(AJAXAddForm, ZEOConnectionTestForm):
-    """ZEO connection test form, JSON renderer"""
-
-    def get_ajax_output(self, changes):
-        settings = '{0}\n\n'.format(pprint.pformat(self.context.get_settings()))
-        status, arguments = changes
-        if status == 200:
-            return {
-                'status': 'success',
-                'content': {'html': '{0}<br />{1}'.format(settings, arguments)},
-                'close_form': False
-            }
-        else:
-            typ, value, tb = arguments
-            message = 'An error occurred: {type}<br />{message}'.format(type=typ,
-                                                                        message='<br />'.join(
-                                                                            traceback.format_exception(typ, value, tb)))
-            return {
-                'status': 'info',
-                'content': {'html': '{0}<br />{1}'.format(settings, message)},
-                'close_form': False
-            }