work in progress: grid field
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3003 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
2848522923
commit
13d5d43021
5 changed files with 124 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -3,7 +3,9 @@
|
|||
<body>
|
||||
|
||||
|
||||
<metal:input define-macro="input_grid">
|
||||
<metal:input define-macro="input_grid"
|
||||
tal:define="fieldInstance python: field.getFieldInstance(view.instance);
|
||||
columns fieldInstance/columnTypes">
|
||||
|
||||
<span dojoType="dojo.data.ItemFileWriteStore"
|
||||
tal:attributes="jsId string:store_$name;
|
||||
|
@ -15,7 +17,7 @@
|
|||
store string:store_$name">
|
||||
<thead>
|
||||
<tr>
|
||||
<tal:header repeat="column field/columns">
|
||||
<tal:header repeat="column columns">
|
||||
<th tal:attributes="field column/name"
|
||||
tal:content="column/title">Column Title</th>
|
||||
</tal:header>
|
||||
|
|
42
composer/schema/grid/interfaces.py
Normal file
42
composer/schema/grid/interfaces.py
Normal file
|
@ -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 = []
|
|
@ -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):
|
||||
|
|
Loading…
Add table
Reference in a new issue