preserve information on client context and - for records/grid - row index in field instances

This commit is contained in:
Helmut Merz 2013-08-16 15:44:45 +02:00
parent 707d7abf6d
commit 2c09a682db
3 changed files with 20 additions and 12 deletions

View file

@ -1,5 +1,5 @@
# #
# Copyright (c) 2011 Helmut Merz helmutm@cy55.de # Copyright (c) 2013 Helmut Merz helmutm@cy55.de
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
@ -185,8 +185,10 @@ class FieldInstance(object):
adapts(IField) adapts(IField)
clientInstance = None clientInstance = None
clientContext = None
value = undefined value = undefined
request = None request = None
index = None
def __init__(self, context): def __init__(self, context):
self.context = context self.context = context

View file

@ -50,8 +50,11 @@ class GridFieldInstance(ListFieldInstance):
for f in self.columnTypes: for f in self.columnTypes:
instanceName = (f.instance_name or instanceName = (f.instance_name or
f.getFieldTypeInfo().instanceName) f.getFieldTypeInfo().instanceName)
result.append(component.getAdapter(f, IFieldInstance, fi = component.getAdapter(f, IFieldInstance, name=instanceName)
name=instanceName)) fi.clientInstance = self.clientInstance
fi.clientContext = self.clientContext
fi.request = self.request
result.append(fi)
return result return result
def marshall(self, value): def marshall(self, value):
@ -89,8 +92,8 @@ class GridFieldInstance(ListFieldInstance):
return [] return []
result = [] result = []
rows = json.loads(value)['items'] rows = json.loads(value)['items']
for row in rows: for idx, row in enumerate(rows):
item = self.unmarshallRow(row) item = self.unmarshallRow(row, idx)
if item: if item:
result.append(item) result.append(item)
return result return result
@ -111,9 +114,11 @@ class GridFieldInstance(ListFieldInstance):
result.append(item) result.append(item)
return result return result
def unmarshallRow(self, row): def unmarshallRow(self, row, idx=None):
item = {} item = {}
for fi in self.columnFieldInstances: for fi in self.columnFieldInstances:
if idx is not None:
fi.index = idx
value = fi.unmarshall(row.get(fi.name) or u'') value = fi.unmarshall(row.get(fi.name) or u'')
if isinstance(value, basestring): if isinstance(value, basestring):
value = value.strip() value = value.strip()
@ -143,8 +148,8 @@ class RecordsFieldInstance(GridFieldInstance):
if not value: if not value:
value = [] value = []
result = [] result = []
for row in value: for idx, row in enumerate(value):
item = self.unmarshallRow(row) item = self.unmarshallRow(row, idx)
if item: if item:
result.append(item) result.append(item)
return result return result
@ -187,8 +192,8 @@ class KeyTableFieldInstance(RecordsFieldInstance):
if not value: if not value:
value = {} value = {}
result = {} result = {}
for row in value: for idx, row in enumerate(value):
item = self.unmarshallRow(row) item = self.unmarshallRow(row, idx)
if item: if item:
result[item.pop(self.keyName)] = [item.get(name) or u'' result[item.pop(self.keyName)] = [item.get(name) or u''
for name in self.dataNames] for name in self.dataNames]

View file

@ -68,7 +68,8 @@ class Instance(BaseInstance):
template = self.template template = self.template
if template is not None: if template is not None:
for f in template.components: for f in template.components:
fieldInstances[f.name] = f.getFieldInstance(self) fieldInstances[f.name] = f.getFieldInstance(self,
context=self.context)
return fieldInstances return fieldInstances
@Lazy @Lazy
@ -122,7 +123,7 @@ class Editor(BaseInstance):
for f in self.template.components: for f in self.template.components:
if f.readonly: if f.readonly:
continue continue
fi = f.getFieldInstance(self) fi = f.getFieldInstance(self, context=self.context)
#value = data.get(f.name) #value = data.get(f.name)
value = fi.getRawValue(data, f.name) value = fi.getRawValue(data, f.name)
fi.validate(value, data) fi.validate(value, data)