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
from zLOG import LOG
class ERP5Field(Field):
"""
The ERP5Field provides here, request,
container etc. names to TALES expressions. It is used to dynamically
patch the standard Formulator
"""
security = ClassSecurityInfo()
# this is a field
is_field = 1
security.declareProtected('Access contents information', 'get_value')
def get_value(self, id, **kw):
"""Get value for id."""
# FIXME: backwards compat hack to make sure tales dict exists
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)
def get_value(self, id, **kw):
"""Get value for id."""
# FIXME: backwards compat hack to make sure tales dict exists
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:
# FIXME: backwards compat hack to make sure overrides dict exists
if not hasattr(self, 'overrides'):
self.overrides = {}
override = self.overrides.get(id, "")
if override:
# call wrapped method to get answer
value = override.__of__(self)()
else:
# 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
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 = 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())
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:
# FIXME: backwards compat hack to make sure overrides dict exists
if not hasattr(self, 'overrides'):
self.overrides = {}
override = self.overrides.get(id, "")
if override:
# call wrapped method to get answer
value = override.__of__(self)()
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
Field.get_value = ERP5Field.get_value
Field._get_default = ERP5Field._get_default
Field.om_icons = ERP5Field.om_icons
Field.get_value = get_value
Field._get_default = _get_default
Field.om_icons = om_icons
# Constructors
......
......@@ -26,70 +26,63 @@ from Products.Formulator.Widget import Widget
from AccessControl import ClassSecurityInfo
from zLOG import LOG
class PatchedField(Field):
security = ClassSecurityInfo()
security.declareProtected('Access contents information',
'generate_field_key')
def generate_field_key(self, validation=0, key=None):
"""Generate the key Silva uses to render the field in the form.
"""
# Patched by JPS for ERP5 in order to
# dynamically change the name
if key is not None:
return 'field_%s' % key
if self.field_record is None:
return 'field_%s' % self.id
elif validation:
return self.id
elif isinstance(self.widget, MultiItemsWidget):
return "%s.%s:record:list" % (self.field_record, self.id)
else:
return '%s.%s:record' % (self.field_record, self.id)
security.declareProtected('View', 'render')
def render(self, value=None, REQUEST=None, key=None):
"""Render the field widget.
value -- the value the field should have (for instance
from validation).
REQUEST -- REQUEST can contain raw (unvalidated) field
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)
def Field_generate_field_key(self, validation=0, key=None):
"""Generate the key Silva uses to render the field in the form.
"""
# Patched by JPS for ERP5 in order to
# dynamically change the name
if key is not None:
return 'field_%s' % key
if self.field_record is None:
return 'field_%s' % self.id
elif validation:
return self.id
elif isinstance(self.widget, MultiItemsWidget):
return "%s.%s:record:list" % (self.field_record, self.id)
else:
return '%s.%s:record' % (self.field_record, self.id)
def Field_render(self, value=None, REQUEST=None, key=None):
"""Render the field widget.
value -- the value the field should have (for instance
from validation).
REQUEST -- REQUEST can contain raw (unvalidated) field
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 render_sub_field(self, id, value=None, REQUEST=None, key=None):
"""Render a sub field, as part of complete rendering of widget in
a form. Works like render() but for sub field.
Added key parameter for ERP5 in order to be compatible with listbox/matrixbox
"""
return self.sub_form.get_field(id)._render_helper(
self.generate_subfield_key(id, key=key), value, REQUEST)
def Field_render_sub_field(self, id, value=None, REQUEST=None, key=None):
"""Render a sub field, as part of complete rendering of widget in
a form. Works like render() but for sub field.
Added key parameter for ERP5 in order to be compatible with listbox/matrixbox
"""
return self.sub_form.get_field(id)._render_helper(
self.generate_subfield_key(id, key=key), value, REQUEST)
def generate_subfield_key(self, id, validation=0, key=None):
"""Generate the key Silva uses to render a sub field.
Added key parameter for ERP5
Added key parameter for ERP5 in order to be compatible with listbox/matrixbox
"""
if key is None: key = self.id
if self.field_record is None or validation:
return 'subfield_%s_%s'%(key, id)
return '%s.subfield_%s_%s:record' % (self.field_record, key, id)
def Field_generate_subfield_key(self, id, validation=0, key=None):
"""Generate the key Silva uses to render a sub field.
Added key parameter for ERP5
Added key parameter for ERP5 in order to be compatible with listbox/matrixbox
"""
if key is None: key = self.id
if self.field_record is None or validation:
return 'subfield_%s_%s'%(key, id)
return '%s.subfield_%s_%s:record' % (self.field_record, key, id)
def validate_sub_field(self, id, REQUEST, key=None):
"""Validates a subfield (as part of field validation).
"""
return self.sub_form.get_field(id)._validate_helper(
self.generate_subfield_key(id, validation=1, key=key), REQUEST)
def Field_validate_sub_field(self, id, REQUEST, key=None):
"""Validates a subfield (as part of field validation).
"""
return self.sub_form.get_field(id)._validate_helper(
self.generate_subfield_key(id, validation=1, key=key), REQUEST)
Field.generate_field_key = PatchedField.generate_field_key
Field.render = PatchedField.render
Field.render_sub_field = PatchedField.render_sub_field
Field.generate_subfield_key = PatchedField.generate_subfield_key
Field.validate_sub_field = PatchedField.validate_sub_field
Field.generate_field_key = Field_generate_field_key
Field.render = Field_render
Field.render_sub_field = Field_render_sub_field
Field.generate_subfield_key = Field_generate_subfield_key
Field.validate_sub_field = Field_validate_sub_field
from Products.Formulator.Validator import SelectionValidator
from Products.Formulator.Validator import StringBaseValidator
......@@ -564,25 +557,18 @@ MultiItemsWidget.render_items = MultiItemsWidget_render_items
# JPS - Subfield handling with listbox requires extension
from Products.Formulator.StandardFields import DateTimeField
class PatchedDateTimeField(DateTimeField):
"""
Make sur we test if this REQUEST parameter has a form
attribute. In ERP5, we sometimes use the REQUEST to pass
subobjects to forms.
"""
def _get_default(self, key, value, REQUEST):
if value is not None:
return value
# 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')
def DateTimeField_get_default(self, key, value, REQUEST):
if value is not None:
return value
# 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
......
......@@ -61,7 +61,7 @@ import FormulatorPatch
import psyco
psyco.bind(ListBox.ListBoxWidget.render)
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._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