context = ILocation(context) container = context.__parent__ if container is None: if IPublicationRoot.providedBy(context): return request.application_url raise LookupError( 'The path of the application root could not be resolved.') url = getMultiAdapter((container, request), IURL) name = getattr(context, '__name__', None) if name is None: raise KeyError(context, '__name__') if name: url += '/' + urllib.quote(name.encode('utf-8'), _safe) return url def get_absolute_url(context, request): return getMultiAdapter((context, request), IURL) try: from grokcore.component import global_adapter global_adapter(resolve_url, (Interface, IRequest)) except ImportError: pass
def newCollectionWidgetFactory(mode=u"", interface=IWidget): def collectionWidgetFactory(field, form, request): """A widget of a collection is a bit advanced. We have to adapt the sub-type of the field as well. """ widget = component.getMultiAdapter( (field, field.valueField, form, request), interface, name=mode) return widget return collectionWidgetFactory grok.global_adapter( newCollectionWidgetFactory(mode='input'), adapts=(ICollectionField, Interface, Interface), provides=IWidget, name='input') grok.global_adapter( newCollectionWidgetFactory(mode='input-list'), adapts=(ICollectionField, Interface, Interface), provides=IWidget, name='input-list') grok.global_adapter( newCollectionWidgetFactory(mode='display'), adapts=(ICollectionField, Interface, Interface), provides=IWidget, name='display')
# make some direct assertions implementer(IHome)(factory3) implementer(IHome)(factory4) implementer(IHome)(factory5) implementer(IHome)(factory6) implementer(IHome)(factory7a) implementer(IHome)(factory7b) grok.adapter(Fireplace)(factory5) grok.adapter(Fireplace)(factory7a) grok.adapter(Cave, Fireplace)(factory7b) # should accept single value for adapts grok.global_adapter(factory1, Cave, IHome) # should accept tuple for adapts grok.global_adapter(factory2, (Cave, ), IHome, name=u"two") # should look at the provided interface grok.global_adapter(factory3, Cave, name=u"three") # should pick the canonical context grok.global_adapter(factory4, name=u"four") # should use __component_adapts__ grok.global_adapter(factory5, name=u"five") # should work as multi-adapter grok.global_adapter(factory6, ( Cave, Fireplace, )) # should use __component_adapts__ adapting one object grok.global_adapter(factory7a, name=u"seven")
class CaveFireplaceHomeFactory(object): def __init__(self, id): self.id = id def __call__(self, cave, fireplace): return Home(self.id) factory1 = CaveHomeFactory(u"one") factory2 = CaveHomeFactory(u"two") factory3 = CaveHomeFactory(u"three") factory4 = CaveHomeFactory(u"four") factory5 = CaveHomeFactory(u"five") factory6 = CaveFireplaceHomeFactory(u"six") # make some direct assertions grok.implementer(IHome)(factory3) grok.implementer(IHome)(factory4) grok.implementer(IHome)(factory5) grok.implementer(IHome)(factory6) grok.adapter(Fireplace)(factory5) grok.global_adapter(factory1, Cave, IHome) # should accept single value for adapts grok.global_adapter(factory2, (Cave,), IHome, name="two") # should accept tuple for adapts grok.global_adapter(factory3, Cave, name="three") # should look at the provided interface grok.global_adapter(factory4, name=u"four") # should pick the canonical context grok.global_adapter(factory5, name="five") # should use __component_adapts__ grok.global_adapter(factory6, (Cave, Fireplace,)) # should work as multi-adapter
# make some direct assertions implementer(IHome)(factory3) implementer(IHome)(factory4) implementer(IHome)(factory5) implementer(IHome)(factory6) implementer(IHome)(factory7a) implementer(IHome)(factory7b) grok.adapter(Fireplace)(factory5) grok.adapter(Fireplace)(factory7a) grok.adapter(Cave, Fireplace)(factory7b) # should accept single value for adapts grok.global_adapter(factory1, Cave, IHome) # should accept tuple for adapts grok.global_adapter(factory2, (Cave,), IHome, name=u"two") # should look at the provided interface grok.global_adapter(factory3, Cave, name=u"three") # should pick the canonical context grok.global_adapter(factory4, name=u"four") # should use __component_adapts__ grok.global_adapter(factory5, name=u"five") # should work as multi-adapter grok.global_adapter(factory6, (Cave, Fireplace,)) # should use __component_adapts__ adapting one object grok.global_adapter(factory7a, name=u"seven") # should use __component_adapts__ adaping two objects grok.global_adapter(factory7b, name=u"seven")
import uvclight from grokcore import component as grok from dolmen.forms.ztk.interfaces import ICollectionField from zope.interface import Interface from dolmen.forms.base.interfaces import IWidget from dolmen.forms.ztk.widgets.collection import ( MultiChoiceFieldWidget, newCollectionWidgetFactory) grok.global_adapter( newCollectionWidgetFactory(mode='multidisabled'), adapts=(ICollectionField, Interface, Interface), provides=IWidget, name='multidisabled') class MultiSelectFieldWidget(MultiChoiceFieldWidget): grok.name('multidisabled') template = uvclight.get_template('disabled.cpt', __file__) def update(self): self._disabled = self.component.valueField.vocabularyFactory(self.form.context).disabled_items MultiChoiceFieldWidget.update(self) def disabled(self, token): return token in self._disabled