From 13d5d4302171d7a682598ada5af1f47b454e6d7a Mon Sep 17 00:00:00 2001 From: helmutm Date: Fri, 21 Nov 2008 07:20:26 +0000 Subject: [PATCH] work in progress: grid field git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3003 fd906abe-77d9-0310-91a1-e0d9ade77398 --- composer/schema/factory.py | 22 ++++++++++++ composer/schema/grid/field.py | 55 +++++++++++++++++++++++++++++ composer/schema/grid/grid_macros.pt | 6 ++-- composer/schema/grid/interfaces.py | 42 ++++++++++++++++++++++ composer/schema/interfaces.py | 2 +- 5 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 composer/schema/grid/interfaces.py diff --git a/composer/schema/factory.py b/composer/schema/factory.py index b7b9318..183def5 100644 --- a/composer/schema/factory.py +++ b/composer/schema/factory.py @@ -99,3 +99,25 @@ class SchemaFactory(object): baseField=field,) fields.append(f) return Schema(name=interface.__name__, *fields, **kw) + + +def createField(field, info=None): + if info is None: + info = getattr(field, '__typeInfo__', ('textline',)) + voc = (getattr(field, 'vocabulary', ()) or + getattr(field, 'vocabularyName', None)) + f = Field(field.getName(), + fieldType=info[0], + fieldTypeInfo=len(info) > 1 and info[1] or None, + required=field.required, + default=field.default, + default_method=getattr(field, 'default_method', None), + vocabulary=voc, + title=field.title, + description=field.description, + readonly=field.readonly, + #value_type=getattr(field, 'value_type', None), + nostore=getattr(field, 'nostore', False), + multiple=getattr(field, 'multiple', False), + baseField=field,) + return f diff --git a/composer/schema/grid/field.py b/composer/schema/grid/field.py index 7f88ccc..aca06b6 100644 --- a/composer/schema/grid/field.py +++ b/composer/schema/grid/field.py @@ -28,8 +28,63 @@ from zope.cachedescriptors.property import Lazy from zope.component import adapts import zope.schema +from cybertools.composer.schema.factory import createField +from cybertools.composer.schema.field import ListFieldInstance from cybertools.composer.schema.interfaces import IField, IFieldInstance from cybertools.composer.schema.interfaces import fieldTypes, undefined from cybertools.util.format import toStr, toUnicode +from cybertools.util import json +class GridFieldInstance(ListFieldInstance): + + @Lazy + def columnTypes(self): + return [createField(t) for t in self.context.baseField.column_types] + + @Lazy + def columnFieldInstances(self): + result = [] + for f in self.columnTypes: + instanceName = (f.instance_name or + f.getFieldTypeInfo().instanceName) + result.append(component.getAdapter(f, IFieldInstance, + name=instanceName)) + return result + + def marshall(self, value): + if isinstance(value, basestring): + return value + # TODO: marshall values! + v = value or [] + empty = {} + for fi in self.columnFieldInstances: + default = fi.default + if default is None: + default = '' + empty[fi.name] = str(default) + for i in range(3): + v.append(empty) + return json.dumps(dict(items=v)) + + def display(self, value): + headers = [fi.context.title for fi in self.columnFieldInstances] + rows = [] + for item in value or []: + row = [] + for fi in self.columnFieldInstances: + row.append(fi.display(item[fi.name])) + rows.append(row) + return dict(headers=headers, rows=rows) + + def unmarshall(self, value): + if not value: + return [] + result = [] + rows = json.loads(value)['items'] + for row in rows: + item = {} + for fi in self.columnFieldInstances: + item[fi.name] = fi.unmarshall(row[fi.name]) + result.append(item) + return result diff --git a/composer/schema/grid/grid_macros.pt b/composer/schema/grid/grid_macros.pt index 0bc2f49..b7db2da 100755 --- a/composer/schema/grid/grid_macros.pt +++ b/composer/schema/grid/grid_macros.pt @@ -3,7 +3,9 @@ - + - + Column Title diff --git a/composer/schema/grid/interfaces.py b/composer/schema/grid/interfaces.py new file mode 100644 index 0000000..20d5bb6 --- /dev/null +++ b/composer/schema/grid/interfaces.py @@ -0,0 +1,42 @@ +# +# Copyright (c) 2007 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Grid field definition. + +$Id$ +""" + +from zope import schema +from zope.interface import Interface, Attribute +from zope.i18nmessageid import MessageFactory +from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm + +from cybertools.composer.schema.interfaces import FieldType + +_ = MessageFactory('cybertools.composer.schema') + + +class Grid(schema.List): + + __typeInfo__ = ('grid', + FieldType('grid', 'grid', + u'Grid for representing a series of records or rows.', + instanceName='grid')) + + column_types = [] diff --git a/composer/schema/interfaces.py b/composer/schema/interfaces.py index a42d0d1..77aa477 100644 --- a/composer/schema/interfaces.py +++ b/composer/schema/interfaces.py @@ -30,7 +30,7 @@ from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm from cybertools.composer.interfaces import ITemplate, IComponent from cybertools.composer.interfaces import IInstance as IBaseInstance -_ = MessageFactory('zope') +_ = MessageFactory('cybertools.composer.schema') class ISchema(ITemplate):