[z3-five] using getToolByName in utility

Dennis Schulz d.schulz81 at gmx.net
Wed Jan 31 14:56:29 CET 2007


**Hello Yuppie,**

**No matter where I print context (all places that you suggested) , 
aq_base(context) it is always the same, only my object without 
acquisition chain:**

<PurchaseRequisition at 3>
[<PurchaseRequisition at 3>]


I am using the applychanges of alchemist and the alchmist bind method.
Could this be the reason?

Here is the applychanges

def applyChanges(context, form_fields, data, adapters=None):
    """
    the default applyChanges from formlib, doesn't play nice with subobjects
    unless a new object is created on edit, we want to edit the value in 
place
    so to detect value changes on subobject changes we manually introspect
    the state of the mapped object to detect changes.
    """
    if adapters is None:
        adapters = {}

    changed = False

    for form_field in form_fields:
        field = form_field.field
        # Adapt context, if necessary
        interface = field.interface
        adapter = adapters.get(interface)
        if adapter is None:
            if interface is None:
                adapter = context
            else:
                adapter = interface(context)
            adapters[interface] = adapter

        name = form_field.__name__
        newvalue = data.get(name, form_field) # using form_field as marker
      
        if IObject.providedBy( field ) and ITableSchema.providedBy( 
newvalue ):
            # we expunge objects from session, to prevent spurious db 
changes on caught
            # form exceptions. if we get to the application of form 
data, then we passed
            # through validation, and values should be reattached to the 
session, for
            # persistence.
            if not attribute_manager.is_modified( newvalue ):
                continue
           
            newvalue = aq_base( newvalue )
            session_id = getattr( newvalue, '_sa_session_id', None)
           
            if session_id is None:
                session = get_session()
                if getattr( newvalue, '_instance_key', None):
                    session._attach( newvalue )
                    # this doesn't work as well we would expect, it 
resets state on updates
                    # session.save_or_update( newvalue )
                    assert newvalue in session.dirty
                    changed = True
                    continue
                # else its new
                session.save( newvalue )                   
                changed = True

        if (newvalue is not form_field) and (field.get(adapter) != 
newvalue):
            changed = True
            field.set(adapter, newvalue)

    return changed


****

** **

** **

**Here the exception I get:**

**ConstraintNotSatisfied**

Sorry, a site error occurred.

Traceback (innermost last):

    * Module ZPublisher.Publish, line 196, in publish_module_standard
    * Module Products.PlacelessTranslationService.PatchStringIO, line
      34, in new_publish
    * Module ZPublisher.Publish, line 146, in publish
    * Module Zope2.App.startup, line 222, in zpublisher_exception_hook
    * Module ZPublisher.Publish, line 115, in publish
    * Module ZPublisher.mapply, line 88, in mapply
    * Module ZPublisher.Publish, line 41, in call_object
    * Module zope.formlib.form, line 757, in __call__
    * Module Products.PurchaseRequisition.browser.CELLSformlib, line
      571, in update
    * Module Products.Five.formlib.formbase, line 54, in update
    * Module zope.formlib.form, line 738, in update
    * Module zope.formlib.form, line 582, in success
    * Module Products.PurchaseRequisition.browser.CELLSformlib, line
      458, in handle_save_action
    * Module Products.PurchaseRequisition.browser.CELLSformlib, line
      1021, in applyChanges
    * Module zope.schema._bootstrapfields, line 183, in set
    * Module ore.alchemist.bind, line 53, in __set__
    * Module zope.schema._bootstrapfields, line 138, in validate
    * Module zope.schema._field, line 271, in _validate

ConstraintNotSatisfied: klora (Also, the following error occurred while 
attempting to render the standard error message, please see the event 
log for full details: 'str' object has no attribute 'args')
yuppie escribió:
> Hi Dennis!
>
>
> Dennis Schulz wrote:
>   
>> I made an utility (vocabulary factory) that returns all users. It 
>> queries the membership tool. (see code at the end)
>> That works fine for displaying it as a dropdown in a formlib form.
>> However, when change a value and submit the form, the utility gets 
>> called in a weird context by the formlib validation machinery.
>> When I print out aq_chain then I only have one object without base request.
>> Within this weird context the getToolByName loopup is not possible.
>>     
>
> IIRC the validation machinery uses the context bound to the field. 
> Something seems to be wrong with the bind() call.
>
>   
>> class UserVocabulary(object):
>>     implements(IVocabularyFactory)
>>     def __call__(self, context):
>>     
>
> What is context and aq_chain(context) here?
>
>   
>>             context = getattr(context, 'context', context)
>>     
>
> And what here?
>
>   
>>             context = aq_inner(context)
>>             print aq_chain(context)
>>             mt = getToolByName(context, 'portal_membership')
>>             memberids = mt.listMemberIds()
>>             result=[( mt.getMemberById(i).getProperty('fullname'),i) for 
>> i in memberids]
>>             return SimpleVocabulary.fromItems(result)
>> UserVocabularyFactory = UserVocabulary()
>>     
>
>
> HTH,
>
> 	Yuppie
>
> _______________________________________________
> z3-five mailing list
> z3-five at codespeak.net
> http://codespeak.net/mailman/listinfo/z3-five
>
>
>   




More information about the z3-five mailing list