Commit 77faa9f3 authored by Romain Courteaud's avatar Romain Courteaud

Bug fix: make the ProxyField compatible with the RelationField.

Remove useless ProxyField properties.
Modify code indentation.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@5896 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 127cbb83
...@@ -36,6 +36,7 @@ from Products.ERP5Form import RelationField ...@@ -36,6 +36,7 @@ from Products.ERP5Form import RelationField
from Products.ERP5Form.RelationField import MAX_SELECT, new_content_prefix from Products.ERP5Form.RelationField import MAX_SELECT, new_content_prefix
from Globals import get_request from Globals import get_request
from Products.PythonScripts.Utility import allow_class from Products.PythonScripts.Utility import allow_class
from AccessControl import ClassSecurityInfo
import string import string
from zLOG import LOG from zLOG import LOG
...@@ -505,17 +506,25 @@ class MultiRelationStringFieldValidator(Validator.LinesValidator, RelationField ...@@ -505,17 +506,25 @@ class MultiRelationStringFieldValidator(Validator.LinesValidator, RelationField
# Can return editor # Can return editor
return MultiRelationEditor(field.id, base_category, portal_type, portal_type_item, catalog_index, relation_setter_id, relation_editor_list) return MultiRelationEditor(field.id, base_category, portal_type, portal_type_item, catalog_index, relation_setter_id, relation_editor_list)
MultiRelationStringFieldWidgetInstance = MultiRelationStringFieldWidget() MultiRelationStringFieldWidgetInstance = MultiRelationStringFieldWidget()
MultiRelationStringFieldValidatorInstance = MultiRelationStringFieldValidator() MultiRelationStringFieldValidatorInstance = MultiRelationStringFieldValidator()
class MultiRelationStringField(ZMIField): class MultiRelationStringField(ZMIField):
meta_type = "MultiRelationStringField" meta_type = "MultiRelationStringField"
is_relation_field = 1 security = ClassSecurityInfo()
widget = MultiRelationStringFieldWidgetInstance widget = MultiRelationStringFieldWidgetInstance
validator = MultiRelationStringFieldValidatorInstance validator = MultiRelationStringFieldValidatorInstance
security.declareProtected('Access contents information', 'get_value')
def get_value(self, id, **kw):
"""Get value for id.
Optionally pass keyword arguments that get passed to TALES
expression.
"""
if id == 'is_relation_field':
result = 1
else:
result = ZMIField.get_value(self, id, **kw)
return result
############################################################################## ##############################################################################
# #
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved. # Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
# Jean-Paul Smets <jp@nexedi.com> # Jean-Paul Smets <jp@nexedi.com>
# #
# WARNING: This program as such is intended to be used by professional # WARNING: This program as such is intended to be used by professional
...@@ -40,9 +40,11 @@ from Products.PythonScripts.Utility import allow_class ...@@ -40,9 +40,11 @@ from Products.PythonScripts.Utility import allow_class
from Products.PythonScripts.standard import url_quote_plus from Products.PythonScripts.standard import url_quote_plus
from AccessControl import ClassSecurityInfo
import string import string
from zLOG import LOG, WARNING from zLOG import LOG, WARNING
from Acquisition import aq_base, aq_inner, aq_acquire, aq_chain
class ProxyWidget(Widget.Widget): class ProxyWidget(Widget.Widget):
""" """
...@@ -52,7 +54,7 @@ class ProxyWidget(Widget.Widget): ...@@ -52,7 +54,7 @@ class ProxyWidget(Widget.Widget):
are defined in order to minimize code duplication. are defined in order to minimize code duplication.
""" """
property_names = Widget.Widget.property_names + [ property_names = [
'form_id', 'form_id',
'field_id', 'field_id',
'extra_context', 'extra_context',
...@@ -61,90 +63,58 @@ class ProxyWidget(Widget.Widget): ...@@ -61,90 +63,58 @@ class ProxyWidget(Widget.Widget):
form_id = fields.StringField( form_id = fields.StringField(
'form_id', 'form_id',
title='Form ID', title='Form ID',
description=( description= \
"ID of the master form."), "ID of the master form.",
default="", default="",
required=1) required=1)
field_id = fields.StringField( field_id = fields.StringField(
'field_id', 'field_id',
title='Field ID', title='Field ID',
description=( description= \
"ID of the field in the master form."), "ID of the field in the master form.",
default="", default="",
required=1) required=1)
default = fields.StringField( # XXX FIXME This seems against the definition of proxy field...
'default', # Remove it as soon as possible
title='Default',
description=(
"Default value."),
default="",
required=0)
extra_context = fields.ListTextAreaField( extra_context = fields.ListTextAreaField(
'extra_context', 'extra_context',
title='Extra Context', title='Extra Context',
description='Additional context variables.', description='Additional context variables.',
default=(), default=(),
required=0) required=0)
def render(self, field, key, value, REQUEST): def render(self, field, key, value, REQUEST):
""" """
Render proxy field Render proxy field
""" """
form = field.aq_parent result = ''
try: proxy_field = field.getTemplateField()
proxy_form = getattr(form, field.get_value('form_id')) if proxy_field is not None:
proxy_field = getattr(proxy_form, field.get_value('field_id')) REQUEST = field.updateContext(REQUEST)
except AttributeError: result = proxy_field.widget.render(proxy_field, key, value, REQUEST)
LOG('ProxyField', WARNING, return result
'could not get a field from a proxy field %s in %s' % \
(field.id, form.id))
return ''
extra_context = REQUEST.other.get('erp5_extra_context', {})
for k, v in field.get_value('extra_context'):
extra_context[k] = v
REQUEST.other['erp5_extra_context'] = extra_context
return proxy_field.widget.render(proxy_field, key, value, REQUEST)
def render_view(self, field, value): def render_view(self, field, value):
""" """
Display proxy field Display proxy field
""" """
if type(value) == type('') and value == '': result = ''
return '' proxy_field = field.getTemplateField()
form = field.aq_parent if proxy_field is not None:
try: result = proxy_field.widget.render_view(proxy_field, value)
proxy_form = getattr(form, field.get_value('form_id')) return result
proxy_field = getattr(proxy_form, field.get_value('field_id'))
except AttributeError:
LOG('ProxyField', WARNING,
'could not get a field from a proxy field %s in %s' % \
(field.id, form.id))
return ''
REQUEST = get_request()
extra_context = REQUEST.other.get('erp5_extra_context', {})
for k, v in field.get_value('extra_context'):
extra_context[k] = v
REQUEST.other['erp5_extra_context'] = extra_context
return proxy_field.widget.render_view(proxy_field, value)
class ProxyValidator(Validator.Validator): class ProxyValidator(Validator.Validator):
""" """
Validation of entered value through proxy field Validation of entered value through proxy field
""" """
property_names = Validator.Validator.property_names property_names = []
def validate(self, field, key, REQUEST): def validate(self, field, key, REQUEST):
form = field.aq_parent proxy_field = field.getTemplateField()
proxy_form = getattr(form, field.get_value('form_id')) REQUEST = field.updateContext(REQUEST)
proxy_field = getattr(proxy_form, field.get_value('field_id'))
extra_context = REQUEST.other.get('erp5_extra_context', {})
for k, v in field.get_value('extra_context'):
extra_context[k] = v
REQUEST.other['erp5_extra_context'] = extra_context
try: try:
result = proxy_field.validator.validate(proxy_field, key, REQUEST) result = proxy_field.validator.validate(proxy_field, key, REQUEST)
except ValidationError, error: except ValidationError, error:
...@@ -157,22 +127,49 @@ ProxyValidatorInstance = ProxyValidator() ...@@ -157,22 +127,49 @@ ProxyValidatorInstance = ProxyValidator()
class ProxyField(ZMIField): class ProxyField(ZMIField):
meta_type = "ProxyField" meta_type = "ProxyField"
security = ClassSecurityInfo()
widget = ProxyWidgetInstance widget = ProxyWidgetInstance
validator = ProxyValidatorInstance validator = ProxyValidatorInstance
def _get_default(self, key, value, REQUEST): def getTemplateField(self):
"""
Return template field of the proxy field.
""" """
Return default value of the field. form = self.aq_parent
object = form.aq_parent
try:
proxy_form = getattr(object, self.get_value('form_id'))
proxy_field = aq_base(getattr(proxy_form, self.get_value('field_id')))
proxy_field = proxy_field.__of__(form)
except AttributeError:
LOG('ProxyField', WARNING,
'Could not get a field from a proxy field %s in %s' % \
(self.id, object.id))
proxy_field = None
return proxy_field
def updateContext(self, REQUEST):
""" """
value = ZMIField._get_default(self, key, value, REQUEST) Update the REQUEST
if value in (None, ''): """
form = self.aq_parent extra_context = REQUEST.other.get('erp5_extra_context', {})
try: for k, v in self.get_value('extra_context'):
proxy_form = getattr(form, self.get_value('form_id')) extra_context[k] = v
proxy_self = getattr(proxy_form, self.get_value('field_id')) REQUEST.other['erp5_extra_context'] = extra_context
except AttributeError: return REQUEST
pass
else: security.declareProtected('Access contents information', 'get_value')
value = proxy_self.get_value('default', REQUEST=REQUEST) def get_value(self, id, **kw):
return value """Get value for id.
Optionally pass keyword arguments that get passed to TALES
expression.
"""
if id in self.widget.property_names:
result = ZMIField.get_value(self, id, **kw)
else:
proxy_field = self.getTemplateField()
if proxy_field is not None:
result = proxy_field.get_value(id, **kw)
return result
This diff is collapsed.
...@@ -920,11 +920,11 @@ class SelectionTool( UniqueObject, SimpleItem ): ...@@ -920,11 +920,11 @@ class SelectionTool( UniqueObject, SimpleItem ):
return object return object
# Related document searching # Related document searching
def viewSearchRelatedDocumentDialog(self, index, form_id, REQUEST=None, sub_index=None, **kw): def viewSearchRelatedDocumentDialog(self, index, form_id, REQUEST=None,
sub_index=None, **kw):
""" """
Returns a search related document dialog Returns a search related document dialog
A set of forwarders us defined to circumvent limitations of HTML
A set of forwarders us defined to circumvent limitations of HTML
""" """
if sub_index != None: if sub_index != None:
REQUEST.form['sub_index'] = sub_index REQUEST.form['sub_index'] = sub_index
...@@ -945,10 +945,12 @@ class SelectionTool( UniqueObject, SimpleItem ): ...@@ -945,10 +945,12 @@ class SelectionTool( UniqueObject, SimpleItem ):
o.immediateReindexObject() o.immediateReindexObject()
object_uid = o.getUid() object_uid = o.getUid()
else: else:
return "Sorrry, Error, the calling object was not catalogued. Do not know how to do ?" return "Sorrry, Error, the calling object was not catalogued. " \
"Do not know how to do ?"
# Find the field which was clicked on # Find the field which was clicked on
form = getattr(o, form_id) # Important to get from the object instead of self # Important to get from the object instead of self
form = getattr(o, form_id)
field = None field = None
relation_index = 0 relation_index = 0
...@@ -959,12 +961,38 @@ class SelectionTool( UniqueObject, SimpleItem ): ...@@ -959,12 +961,38 @@ class SelectionTool( UniqueObject, SimpleItem ):
for field in form.get_fields(include_disabled=0): for field in form.get_fields(include_disabled=0):
if field.get_value('editable',REQUEST=REQUEST): if field.get_value('editable',REQUEST=REQUEST):
field_list.append(field) field_list.append(field)
relation_field_found = 0
for field in field_list: for field in field_list:
if getattr(field, 'is_relation_field', None): try:
dumb = field.get_value('is_relation_field')
# XXX FIXME Exception name is not in locals.
# This can be related to a bad python file import
# I already had this kind of error with another python software,
# and the only solution I found was to use ihooks to
# import python files.
# I have to check this.
# except KeyError:
except:
pass
# relation_index += 1
else:
if index == relation_index: if index == relation_index:
relation_field_found = 1
break break
else: else:
relation_index += 1 relation_index += 1
# if getattr(field, 'is_relation_field', None):
# if index == relation_index:
# relation_field_found = 1
# break
# else:
# relation_index += 1
if not relation_field_found:
raise SelectionError, "SelectionTool: can not find the relation" \
" field %s" % index
field_value = REQUEST.form['field_%s' % field.id] field_value = REQUEST.form['field_%s' % field.id]
...@@ -973,7 +1001,9 @@ class SelectionTool( UniqueObject, SimpleItem ): ...@@ -973,7 +1001,9 @@ class SelectionTool( UniqueObject, SimpleItem ):
# reselt current selection # reselt current selection
self.portal_selections.setSelectionFor( selection_name, None) self.portal_selections.setSelectionFor( selection_name, None)
# XXX portal_status_message = "Please select one object to precise the value: '%s' in the field: '%s'" % ( field_value, field.get_orig_value('title') ) # XXX portal_status_message =
# "Please select one object to precise the value:
# '%s' in the field: '%s'" % (field_value, field.get_orig_value('title'))
portal_status_message = "Please select one object." portal_status_message = "Please select one object."
if field.meta_type == "MultiRelationStringField": if field.meta_type == "MultiRelationStringField":
...@@ -982,21 +1012,29 @@ class SelectionTool( UniqueObject, SimpleItem ): ...@@ -982,21 +1012,29 @@ class SelectionTool( UniqueObject, SimpleItem ):
# we need to facilitate user search # we need to facilitate user search
# first: store current field value in the selection # first: store current field value in the selection
base_category = field.get_value( 'base_category') base_category = field.get_value('base_category')
property_get_related_uid_method_name = "get"+ string.join( map( lambda x: string.upper(x[0]) + x[1:] ,string.split(base_category,'_') ) , '' ) + "UidList" property_get_related_uid_method_name = \
"get%sUidList" % ''.join(['%s%s' % (x[0].upper(), x[1:]) \
for x in base_category.split('_')])
current_uid_list = getattr( o, property_get_related_uid_method_name )( portal_type=map(lambda x:x[0],field.get_value('portal_type'))) current_uid_list = getattr(o, property_get_related_uid_method_name)\
(portal_type=[x[0] for x in \
field.get_value('portal_type')])
# Checked current uid # Checked current uid
kw ={} kw ={}
kw[field.get_value('catalog_index')] = field_value kw[field.get_value('catalog_index')] = field_value
self.portal_selections.setSelectionParamsFor(selection_name, kw.copy()) self.portal_selections.setSelectionParamsFor(selection_name,
self.portal_selections.setSelectionCheckedUidsFor(selection_name , current_uid_list ) kw.copy())
self.portal_selections.setSelectionCheckedUidsFor(
selection_name,
current_uid_list)
field_value = '' field_value = ''
REQUEST.form['field_%s' % field.id] = field_value REQUEST.form['field_%s' % field.id] = field_value
# XXX portal_status_message = "Please select one or more object to define the field: '%s'" % field.get_orig_value('title') # XXX portal_status_message =
# "Please select one or more object to define the field:
# '%s'" % field.get_orig_value('title')
portal_status_message = "Please select one (or more) object." portal_status_message = "Please select one (or more) object."
...@@ -1010,8 +1048,6 @@ class SelectionTool( UniqueObject, SimpleItem ): ...@@ -1010,8 +1048,6 @@ class SelectionTool( UniqueObject, SimpleItem ):
form_pickle, form_signature = self.getPickleAndSignature(**pickle_kw) form_pickle, form_signature = self.getPickleAndSignature(**pickle_kw)
REQUEST.form_pickle = form_pickle REQUEST.form_pickle = form_pickle
REQUEST.form_signature = form_signature REQUEST.form_signature = form_signature
base_category = None base_category = None
kw = {} kw = {}
...@@ -1101,29 +1137,26 @@ class SelectionTool( UniqueObject, SimpleItem ): ...@@ -1101,29 +1137,26 @@ class SelectionTool( UniqueObject, SimpleItem ):
def _aq_dynamic(self, name): def _aq_dynamic(self, name):
""" """
Generate viewSearchRelatedDocumentDialog0, viewSearchRelatedDocumentDialog1,... if necessary Generate viewSearchRelatedDocumentDialog0,
viewSearchRelatedDocumentDialog1,... if necessary
""" """
aq_base_name = getattr(aq_base(self), name, None) aq_base_name = getattr(aq_base(self), name, None)
if aq_base_name == None: if aq_base_name == None:
dynamic_method_name = 'viewSearchRelatedDocumentDialog' dynamic_method_name = 'viewSearchRelatedDocumentDialog'
zope_security = '__roles__' zope_security = '__roles__'
if (name[:len(dynamic_method_name)] == dynamic_method_name) and (name[-len(zope_security):] != zope_security) : if (name[:len(dynamic_method_name)] == dynamic_method_name) and \
(name[-len(zope_security):] != zope_security):
#method_count_string = name[len(dynamic_method_name):]
method_count_string_list = string.split( name[len(dynamic_method_name):] , '_' )
method_count_string_list = string.split(
name[len(dynamic_method_name):],
'_')
method_count_string = method_count_string_list[0] method_count_string = method_count_string_list[0]
# be sure that method name is correct # be sure that method name is correct
try: try:
method_count = string.atoi(method_count_string) method_count = string.atoi(method_count_string)
except TypeError: except TypeError:
return aq_base_name return aq_base_name
else: else:
if len(method_count_string_list) > 1: if len(method_count_string_list) > 1:
# be sure that method name is correct # be sure that method name is correct
try: try:
...@@ -1132,21 +1165,26 @@ class SelectionTool( UniqueObject, SimpleItem ): ...@@ -1132,21 +1165,26 @@ class SelectionTool( UniqueObject, SimpleItem ):
return aq_base_name return aq_base_name
else: else:
sub_index = None sub_index = None
# generate dynamicaly needed forwarder methods # generate dynamicaly needed forwarder methods
def viewSearchRelatedDocumentDialogWrapper(self, form_id, REQUEST=None, **kw): def viewSearchRelatedDocumentDialogWrapper(self, form_id,
REQUEST=None, **kw):
""" """
viewSearchRelatedDocumentDialog Wrapper viewSearchRelatedDocumentDialog Wrapper
""" """
LOG('SelectionTool.viewSearchRelatedDocumentDialogWrapper, kw',0,kw) LOG('SelectionTool.viewSearchRelatedDocumentDialogWrapper, kw',
0, kw)
if sub_index == None: if sub_index == None:
return self.viewSearchRelatedDocumentDialog(method_count, form_id, REQUEST=REQUEST, **kw) return self.viewSearchRelatedDocumentDialog(
method_count, form_id,
REQUEST=REQUEST, **kw)
else: else:
return self.viewSearchRelatedDocumentDialog(method_count, form_id, REQUEST=REQUEST, sub_index=sub_index, **kw) return self.viewSearchRelatedDocumentDialog(
method_count, form_id,
REQUEST=REQUEST, sub_index=sub_index, **kw)
setattr(self.__class__, name, viewSearchRelatedDocumentDialogWrapper) setattr(self.__class__, name,
viewSearchRelatedDocumentDialogWrapper)
klass = aq_base(self).__class__ klass = aq_base(self).__class__
if hasattr(klass, 'security'): if hasattr(klass, 'security'):
...@@ -1154,8 +1192,8 @@ class SelectionTool( UniqueObject, SimpleItem ): ...@@ -1154,8 +1192,8 @@ class SelectionTool( UniqueObject, SimpleItem ):
klass.security.declareProtected(ERP5Permissions.View, name) klass.security.declareProtected(ERP5Permissions.View, name)
else: else:
# XXX security declaration always failed.... # XXX security declaration always failed....
LOG('WARNING ERP5Form SelectionTool, security not defined on',0,klass.__name__) LOG('WARNING ERP5Form SelectionTool, security not defined on',
0, klass.__name__)
return getattr(self, name) return getattr(self, name)
else: else:
return aq_base_name return aq_base_name
......
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