def test_hit(self): from zope.interface import Interface from zope.interface import implementer from guillotina.component import get_global_components class IFoo(Interface): pass class IBar(Interface): pass @implementer(IBar) class Bar(object): pass @implementer(IFoo) class Baz(object): def __init__(self, context): self.context = context get_global_components().registerAdapter(Baz, (IBar, ), IFoo, "") bar = Bar() adapted = IFoo(bar) self.assertTrue(adapted.__class__ is Baz) self.assertTrue(adapted.context is bar)
def test_hit_registered_for_None(self): from zope.interface import Interface from zope.interface import implementer from guillotina.component import get_global_components class IFoo(Interface): pass @implementer(IFoo) class Baz(object): def __init__(self, context): self.context = context get_global_components().registerAdapter(Baz, (None,), IFoo, '') ctx = object() adapted = IFoo(ctx) self.assertTrue(adapted.__class__ is Baz) self.assertTrue(adapted.context is ctx)
def del_async_utility(self, config): self.cancel_async_utility(config['provides']) interface = import_class(config['provides']) utility = get_utility(interface) gsm = get_global_components() gsm.unregisterUtility(utility, provided=interface) del self._async_utilities[config['provides']]
def test_hit_registered_for_None(self): from zope.interface import Interface from zope.interface import implementer from guillotina.component import get_global_components class IFoo(Interface): pass @implementer(IFoo) class Baz(object): def __init__(self, context): self.context = context get_global_components().registerAdapter(Baz, (None, ), IFoo, "") ctx = object() adapted = IFoo(ctx) self.assertTrue(adapted.__class__ is Baz) self.assertTrue(adapted.context is ctx)
async def del_async_utility(self, key): self.cancel_async_utility(key) config = self._async_utilities[key]['config'] interface = import_class(config['provides']) utility = get_utility(interface) if hasattr(utility, 'finalize'): await lazy_apply(utility.finalize, app=self.app) gsm = get_global_components() gsm.unregisterUtility(utility, provided=interface) del self._async_utilities[key]
def test_hit(self): from zope.interface import Interface from zope.interface import implementer from guillotina.component import get_global_components class IFoo(Interface): pass class IBar(Interface): pass @implementer(IBar) class Bar(object): pass @implementer(IFoo) class Baz(object): def __init__(self, context): self.context = context get_global_components().registerAdapter(Baz, (IBar,), IFoo, '') bar = Bar() adapted = IFoo(bar) self.assertTrue(adapted.__class__ is Baz) self.assertTrue(adapted.context is bar)
async def del_async_utility(self, key: str): self.cancel_async_utility(key) config = self._async_utilities[key]["config"] interface = import_class(config["provides"]) if "name" in config: utility = get_utility(interface, name=config["name"]) else: utility = get_utility(interface) if hasattr(utility, "finalize"): await lazy_apply(utility.finalize, app=self.app) gsm = get_global_components() gsm.unregisterUtility(utility, provided=interface) del self._async_utilities[key]
async def get_all_subscribers(context, request): subscribers = {} sm = component.get_global_components() for registration in sm.registeredHandlers(): if len(registration.required) != 2: continue handler = get_dotted_name(registration.handler) event = get_dotted_name(registration.required[1]) resource = get_dotted_name(registration.required[0]) if resource not in subscribers: subscribers[resource] = {} if event not in subscribers[resource]: subscribers[resource][event] = [] subscribers[resource][event].append(handler) return subscribers
def create_content_factory(proto_name, proto_definition): parent_interface = import_class( proto_definition.get("inherited_interface", "guillotina.interfaces.content.IFolder")) parent_class = import_class( proto_definition.get("inherited_class", "guillotina.content.Folder")) schema_fields, tags = get_fields( properties=proto_definition.get("properties")) class_interface = InterfaceClass( "I" + proto_name, (parent_interface, ), schema_fields, __module__="guillotina.contrib.dyncontent.interfaces", ) for field_id, tag in tags.items(): for tag_id, tag_metadata in tag.items(): if tag_id in SUPPORTED_DIRECTIVES: if tag_metadata is None: SUPPORTED_DIRECTIVES[tag_id].apply(class_interface, field_id) elif isinstance(tag_metadata, dict): SUPPORTED_DIRECTIVES[tag_id].apply(class_interface, field_id, **tag_metadata) elif isinstance(tag_metadata, list): SUPPORTED_DIRECTIVES[tag_id].apply(class_interface, field_id, *tag_metadata) elif tag_id == "fieldset": SUPPORTED_DIRECTIVES[tag_id].apply(class_interface, field_id, tag_metadata) elif isinstance(tag_metadata, str): SUPPORTED_DIRECTIVES[tag_id].apply( class_interface, **{field_id: tag_metadata}) klass = type(proto_name, (parent_class, ), {}) klass.__module__ = "guillotina.contrib.dyncontent.contents" setattr(contents, proto_name, klass) behaviors = [] for bhr in proto_definition.get("behaviors", []): if bhr in BEHAVIOR_CACHE: behaviors.append(BEHAVIOR_CACHE[bhr]) else: raise Exception(f"Behavior not found {bhr}") contenttype = { "schema": class_interface, "type_name": proto_name, "allowed_types": proto_definition.get("allowed_types", []), "add_permission": proto_definition.get("add_permission", "guillotina.AddContent"), "behaviors": behaviors, } utility = query_utility(IResourceFactory, name=proto_name) if utility is not None: sm = get_global_components() sm.unregisterUtility(utility, IResourceFactory, proto_name) configure.register_configuration(klass, contenttype, "contenttype")
def create_content_factory(proto_name, proto_definition): parent_interface = import_class( proto_definition.get('inherited_interface', 'guillotina.interfaces.content.IFolder')) parent_class = import_class( proto_definition.get('inherited_class', 'guillotina.content.Folder')) schema_fields, tags = get_fields( properties=proto_definition.get('properties')) for fieldset_id, fieldset_list in proto_definition.get('fieldsets', {}).items(): for field_id in fieldset_list: tags.setdefault(field_id, {})['fieldset'] = fieldset_id class_interface = InterfaceClass('I' + proto_name, (parent_interface, ), schema_fields, __module__='guillotina_cms.interfaces') for field_id, tag in tags.items(): for tag_id, tag_metadata in tag.items(): if tag_id in SUPPORTED_DIRECTIVES: if tag_metadata is None: SUPPORTED_DIRECTIVES[tag_id].apply(class_interface, field_id) elif isinstance(tag_metadata, dict): SUPPORTED_DIRECTIVES[tag_id].apply(class_interface, field_id, **tag_metadata) elif isinstance(tag_metadata, list): SUPPORTED_DIRECTIVES[tag_id].apply(class_interface, field_id, *tag_metadata) elif tag_id == 'fieldset': SUPPORTED_DIRECTIVES[tag_id].apply(class_interface, field_id, tag_metadata) elif isinstance(tag_metadata, str): SUPPORTED_DIRECTIVES[tag_id].apply( class_interface, **{field_id: tag_metadata}) klass = type(proto_name, (parent_class, ), {}) klass.__module__ = 'guillotina_cms.dyncontent' setattr(dyncontent, proto_name, klass) behaviors = [] for bhr in proto_definition.get('behaviors', []): if bhr in BEHAVIOR_CACHE: behaviors.append(BEHAVIOR_CACHE[bhr]) else: raise Exception(f"Behavior not found {bhr}") contenttype = { 'schema': class_interface, 'type_name': proto_name, 'allowed_types': proto_definition.get('allowed_types', []), 'add_permission': proto_definition.get('add_permission', 'guillotina.AddContent'), 'behaviors': behaviors } utility = query_utility(IResourceFactory, name=proto_name) if utility is not None: sm = get_global_components() sm.unregisterUtility(utility, IResourceFactory, proto_name) configure.register_configuration(klass, contenttype, 'contenttype') root = get_utility(IApplication, name='root') configure.load_configuration(root.app.config, 'guillotina_cms.dyncontent', 'contenttype') root.app.config.execute_actions() configure.clear() load_cached_schema() # Verify its created if proto_name in FACTORY_CACHE: del FACTORY_CACHE[proto_name] get_cached_factory(proto_name)