[gen] Bugfix: ref field: empty index value [''] could be stored within the db, as the real value. Added field.setSlave allowing to lazy-define master/slave relationships.

This commit is contained in:
Gaetan Delannay 2014-10-06 12:51:40 +02:00
parent d9d93a650b
commit 5ac8e71a6f
2 changed files with 18 additions and 3 deletions

View file

@ -620,6 +620,19 @@ class Field:
if self.isEmptyValue(obj, value): return if self.isEmptyValue(obj, value): return
return value return value
def setSlave(self, slaveField, masterValue):
'''Sets p_slaveField as slave of this field. Normally, master/slave
relationships are defined when a slave field is defined. At this time
you specify parameters "master" and "masterValue" for this field and
that's all. This method is used to add a master/slave relationship
that was not initially foreseen.'''
slaveField.master = self
slaveField.masterValue = gutils.initMasterValue(masterValue)
if slaveField not in self.slaves:
self.slaves.append(slaveField)
# Master's init method may not have been called yet.
slaveField.masterName = getattr(self, 'name', None)
def getMasterData(self): def getMasterData(self):
'''Gets the master of this field (and masterValue) or, recursively, of '''Gets the master of this field (and masterValue) or, recursively, of
containing groups when relevant.''' containing groups when relevant.'''

View file

@ -947,14 +947,16 @@ class Ref(Field):
p_forSearch is True, it will return a list of the linked objects' p_forSearch is True, it will return a list of the linked objects'
titles instead.''' titles instead.'''
if not forSearch: if not forSearch:
res = getattr(obj.aq_base, self.name, []) res = getattr(obj.aq_base, self.name, None)
if res: if res:
# The index does not like persistent lists. # The index does not like persistent lists. Moreover, I don't
# want to give to anyone access to the persistent list in the
# DB.
res = list(res) res = list(res)
else: else:
# Ugly catalog: if I return an empty list, the previous value # Ugly catalog: if I return an empty list, the previous value
# is kept. # is kept.
res.append('') res = ['']
return res return res
else: else:
# For the global search: return linked objects' titles. # For the global search: return linked objects' titles.