Commit 2f54ba04 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Fix some dynamic patches. They should not create new classes.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@3565 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent cccbd8cd
...@@ -49,127 +49,116 @@ from Products.Formulator.Field import Field ...@@ -49,127 +49,116 @@ from Products.Formulator.Field import Field
from zLOG import LOG from zLOG import LOG
class ERP5Field(Field): def get_value(self, id, **kw):
""" """Get value for id."""
The ERP5Field provides here, request, # FIXME: backwards compat hack to make sure tales dict exists
container etc. names to TALES expressions. It is used to dynamically if not hasattr(self, 'tales'):
patch the standard Formulator self.tales = {}
"""
security = ClassSecurityInfo() tales_expr = self.tales.get(id, "")
if tales_expr:
# this is a field form = self.aq_parent
is_field = 1 object = getattr(form, 'aq_parent', None)
if object:
security.declareProtected('Access contents information', 'get_value') # NEEDS TO BE CHECKED
def get_value(self, id, **kw): # container = object.aq_inner.aq_parent ORIGINAL VERSION - not so good ?
"""Get value for id.""" container = object.aq_parent
# FIXME: backwards compat hack to make sure tales dict exists #container = object.getParentNode()
if not hasattr(self, 'tales'):
self.tales = {}
tales_expr = self.tales.get(id, "")
if tales_expr:
form = self.aq_parent
object = getattr(form, 'aq_parent', None)
if object:
# NEEDS TO BE CHECKED
# container = object.aq_inner.aq_parent ORIGINAL VERSION - not so good ?
container = object.aq_parent
#container = object.getParentNode()
else:
container = None
kw['field'] = self
kw['form'] = form
kw['here'] = object
kw['container'] = container
# This allows to pass some pointer to the local object
# through the REQUEST parameter. Not very clean.
# Used by ListBox to render different items in a list
if kw.has_key('REQUEST') and not kw.get('cell'): kw['cell'] = kw['REQUEST']
try:
value = tales_expr.__of__(self)(**kw)
except:
# We add this safety exception to make sure we always get
# something reasonable rather than generate plenty of errors
LOG('ERP5Form.get_value ( %s/%s [%s]), exception on tales_expr: '%(
self.aq_parent.getId(), self.getId(), id) ,0,'', error=sys.exc_info())
value = self.get_orig_value(id)
else: else:
# FIXME: backwards compat hack to make sure overrides dict exists container = None
if not hasattr(self, 'overrides'): kw['field'] = self
self.overrides = {} kw['form'] = form
kw['here'] = object
override = self.overrides.get(id, "") kw['container'] = container
if override: # This allows to pass some pointer to the local object
# call wrapped method to get answer # through the REQUEST parameter. Not very clean.
value = override.__of__(self)() # Used by ListBox to render different items in a list
else: if kw.has_key('REQUEST') and not kw.get('cell'): kw['cell'] = kw['REQUEST']
# get normal value
value = self.get_orig_value(id)
# Only for the default value
if id == 'default':
if (value is None or value == '' or value == [] or value == ()) \
and self.meta_type != 'MethodField' :
# If nothing was provided then try to
# find a default method to get the value
# for that field
# NEEDS TO BE CLEANED UP
try:
form = self.aq_parent
object = getattr(form, 'aq_parent', None)
key = self.id
key = key[3:]
value = object.getProperty(key, d=value)
except:
value = None
# if normal value is a callable itself, wrap it
if callable(value):
value = value.__of__(self)
#value=value() # Mising call ??? XXX Make sure compatible with listbox methods
if id == 'default':
if self.meta_type != 'DateTimeField':
# We make sure we convert values to empty strings
# for most fields (so that we do not get a 'value'
# message on screeen)
# This can be overriden by useing TALES in the field
if value is None: value = ''
return value
def om_icons(self):
"""Return a list of icon URLs to be displayed by an ObjectManager"""
icons = ({'path': self.icon,
'alt': self.meta_type, 'title': self.meta_type},)
return icons
psyco.bind(get_value)
def _get_default(self, key, value, REQUEST):
if value is not None:
return value
try: try:
value = REQUEST.form[key] value = tales_expr.__of__(self)(**kw)
except (KeyError, AttributeError): except:
# fall back on default # We add this safety exception to make sure we always get
return self.get_value('default',REQUEST=REQUEST) # It was missing on Formulator # something reasonable rather than generate plenty of errors
LOG('ERP5Form.get_value ( %s/%s [%s]), exception on tales_expr: '%(
# if we enter a string value while the field expects unicode, self.aq_parent.getId(), self.getId(), id) ,0,'', error=sys.exc_info())
# convert to unicode first value = self.get_orig_value(id)
# this solves a problem when re-rendering a sticky form with else:
# values from request # FIXME: backwards compat hack to make sure overrides dict exists
if (self.has_value('unicode') and self.get_value('unicode') and if not hasattr(self, 'overrides'):
type(value) == type('')): self.overrides = {}
return unicode(value, self.get_form_encoding())
override = self.overrides.get(id, "")
if override:
# call wrapped method to get answer
value = override.__of__(self)()
else: else:
return value # get normal value
value = self.get_orig_value(id)
# Only for the default value
if id == 'default':
if (value is None or value == '' or value == [] or value == ()) \
and self.meta_type != 'MethodField' :
# If nothing was provided then try to
# find a default method to get the value
# for that field
# NEEDS TO BE CLEANED UP
try:
form = self.aq_parent
object = getattr(form, 'aq_parent', None)
key = self.id
key = key[3:]
value = object.getProperty(key, d=value)
except:
value = None
# if normal value is a callable itself, wrap it
if callable(value):
value = value.__of__(self)
#value=value() # Mising call ??? XXX Make sure compatible with listbox methods
if id == 'default':
if self.meta_type != 'DateTimeField':
# We make sure we convert values to empty strings
# for most fields (so that we do not get a 'value'
# message on screeen)
# This can be overriden by useing TALES in the field
if value is None: value = ''
return value
psyco.bind(get_value)
def om_icons(self):
"""Return a list of icon URLs to be displayed by an ObjectManager"""
icons = ({'path': self.icon,
'alt': self.meta_type, 'title': self.meta_type},)
return icons
def _get_default(self, key, value, REQUEST):
if value is not None:
return value
try:
value = REQUEST.form[key]
except (KeyError, AttributeError):
# fall back on default
return self.get_value('default',REQUEST=REQUEST) # It was missing on Formulator
# if we enter a string value while the field expects unicode,
# convert to unicode first
# this solves a problem when re-rendering a sticky form with
# values from request
if (self.has_value('unicode') and self.get_value('unicode') and
type(value) == type('')):
return unicode(value, self.get_form_encoding())
else:
return value
# Dynamic Patch # Dynamic Patch
Field.get_value = ERP5Field.get_value Field.get_value = get_value
Field._get_default = ERP5Field._get_default Field._get_default = _get_default
Field.om_icons = ERP5Field.om_icons Field.om_icons = om_icons
# Constructors # Constructors
......
...@@ -26,70 +26,63 @@ from Products.Formulator.Widget import Widget ...@@ -26,70 +26,63 @@ from Products.Formulator.Widget import Widget
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from zLOG import LOG from zLOG import LOG
class PatchedField(Field): def Field_generate_field_key(self, validation=0, key=None):
"""Generate the key Silva uses to render the field in the form.
security = ClassSecurityInfo() """
security.declareProtected('Access contents information', # Patched by JPS for ERP5 in order to
'generate_field_key') # dynamically change the name
def generate_field_key(self, validation=0, key=None): if key is not None:
"""Generate the key Silva uses to render the field in the form. return 'field_%s' % key
""" if self.field_record is None:
# Patched by JPS for ERP5 in order to return 'field_%s' % self.id
# dynamically change the name elif validation:
if key is not None: return self.id
return 'field_%s' % key elif isinstance(self.widget, MultiItemsWidget):
if self.field_record is None: return "%s.%s:record:list" % (self.field_record, self.id)
return 'field_%s' % self.id else:
elif validation: return '%s.%s:record' % (self.field_record, self.id)
return self.id
elif isinstance(self.widget, MultiItemsWidget): def Field_render(self, value=None, REQUEST=None, key=None):
return "%s.%s:record:list" % (self.field_record, self.id) """Render the field widget.
else: value -- the value the field should have (for instance
return '%s.%s:record' % (self.field_record, self.id) from validation).
REQUEST -- REQUEST can contain raw (unvalidated) field
security.declareProtected('View', 'render') information. If value is None, REQUEST is searched
def render(self, value=None, REQUEST=None, key=None): for this value.
"""Render the field widget. if value and REQUEST are both None, the 'default' property of
value -- the value the field should have (for instance the field will be used for the value.
from validation). """
REQUEST -- REQUEST can contain raw (unvalidated) field return self._render_helper(self.generate_field_key(key=key), value, REQUEST)
information. If value is None, REQUEST is searched
for this value.
if value and REQUEST are both None, the 'default' property of
the field will be used for the value.
"""
return self._render_helper(self.generate_field_key(key=key), value, REQUEST)
security.declareProtected('View', 'render_sub_field') def Field_render_sub_field(self, id, value=None, REQUEST=None, key=None):
def render_sub_field(self, id, value=None, REQUEST=None, key=None): """Render a sub field, as part of complete rendering of widget in
"""Render a sub field, as part of complete rendering of widget in a form. Works like render() but for sub field.
a form. Works like render() but for sub field. Added key parameter for ERP5 in order to be compatible with listbox/matrixbox
Added key parameter for ERP5 in order to be compatible with listbox/matrixbox """
""" return self.sub_form.get_field(id)._render_helper(
return self.sub_form.get_field(id)._render_helper( self.generate_subfield_key(id, key=key), value, REQUEST)
self.generate_subfield_key(id, key=key), value, REQUEST)
def generate_subfield_key(self, id, validation=0, key=None): def Field_generate_subfield_key(self, id, validation=0, key=None):
"""Generate the key Silva uses to render a sub field. """Generate the key Silva uses to render a sub field.
Added key parameter for ERP5 Added key parameter for ERP5
Added key parameter for ERP5 in order to be compatible with listbox/matrixbox Added key parameter for ERP5 in order to be compatible with listbox/matrixbox
""" """
if key is None: key = self.id if key is None: key = self.id
if self.field_record is None or validation: if self.field_record is None or validation:
return 'subfield_%s_%s'%(key, id) return 'subfield_%s_%s'%(key, id)
return '%s.subfield_%s_%s:record' % (self.field_record, key, id) return '%s.subfield_%s_%s:record' % (self.field_record, key, id)
def validate_sub_field(self, id, REQUEST, key=None): def Field_validate_sub_field(self, id, REQUEST, key=None):
"""Validates a subfield (as part of field validation). """Validates a subfield (as part of field validation).
""" """
return self.sub_form.get_field(id)._validate_helper( return self.sub_form.get_field(id)._validate_helper(
self.generate_subfield_key(id, validation=1, key=key), REQUEST) self.generate_subfield_key(id, validation=1, key=key), REQUEST)
Field.generate_field_key = PatchedField.generate_field_key Field.generate_field_key = Field_generate_field_key
Field.render = PatchedField.render Field.render = Field_render
Field.render_sub_field = PatchedField.render_sub_field Field.render_sub_field = Field_render_sub_field
Field.generate_subfield_key = PatchedField.generate_subfield_key Field.generate_subfield_key = Field_generate_subfield_key
Field.validate_sub_field = PatchedField.validate_sub_field Field.validate_sub_field = Field_validate_sub_field
from Products.Formulator.Validator import SelectionValidator from Products.Formulator.Validator import SelectionValidator
from Products.Formulator.Validator import StringBaseValidator from Products.Formulator.Validator import StringBaseValidator
...@@ -564,25 +557,18 @@ MultiItemsWidget.render_items = MultiItemsWidget_render_items ...@@ -564,25 +557,18 @@ MultiItemsWidget.render_items = MultiItemsWidget_render_items
# JPS - Subfield handling with listbox requires extension # JPS - Subfield handling with listbox requires extension
from Products.Formulator.StandardFields import DateTimeField from Products.Formulator.StandardFields import DateTimeField
class PatchedDateTimeField(DateTimeField): def DateTimeField_get_default(self, key, value, REQUEST):
""" if value is not None:
Make sur we test if this REQUEST parameter has a form return value
attribute. In ERP5, we sometimes use the REQUEST to pass # if there is something in the request then return None
subobjects to forms. # sub fields should pick up defaults themselves
""" if REQUEST is not None and hasattr(REQUEST, 'form') and \
REQUEST.form.has_key('subfield_%s_%s' % (self.id, 'year')):
def _get_default(self, key, value, REQUEST): return None
if value is not None: else:
return value return self.get_value('default')
# if there is something in the request then return None
# sub fields should pick up defaults themselves
if REQUEST is not None and hasattr(REQUEST, 'form') and \
REQUEST.form.has_key('subfield_%s_%s' % (self.id, 'year')):
return None
else:
return self.get_value('default')
DateTimeField._get_default = PatchedDateTimeField._get_default DateTimeField._get_default = DateTimeField_get_default
from Products.Formulator.Widget import DateTimeWidget from Products.Formulator.Widget import DateTimeWidget
......
...@@ -61,7 +61,7 @@ import FormulatorPatch ...@@ -61,7 +61,7 @@ import FormulatorPatch
import psyco import psyco
psyco.bind(ListBox.ListBoxWidget.render) psyco.bind(ListBox.ListBoxWidget.render)
psyco.bind(ListBox.ListBoxValidator.validate) psyco.bind(ListBox.ListBoxValidator.validate)
psyco.bind(Form.ERP5Field.get_value) #psyco.bind(Form.ERP5Field.get_value)
#psyco.bind(Form.ERP5Form.__call__) #psyco.bind(Form.ERP5Form.__call__)
#psyco.bind(Form.ERP5Form._exec) #psyco.bind(Form.ERP5Form._exec)
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment