Beispiel #1
0
def check_required_utilities(site, utilities):
    """Utility function to check for required utilities

    :param ISite site: the site manager into which configuration may be checked
    :param tuple utilities: each element of the tuple is another tuple made of the utility
        interface, the utility registration name, the utility factory and the object name when
        creating the utility, as in:

    .. code-block:: python

        REQUIRED_UTILITIES = ((ISecurityManager, '', SecurityManager, 'Security manager'),
                              (IPrincipalAnnotationUtility, '', PrincipalAnnotationUtility,
                               'User profiles'))
    """
    registry = get_current_registry()
    for interface, name, factory, default_id in utilities:
        utility = query_utility(interface, name=name)
        if utility is None:
            lsm = site.getSiteManager()
            if default_id in lsm:
                continue
            if factory is None:
                factory = get_object_factory(interface)
            utility = factory()
            registry.notify(ObjectCreatedEvent(utility))
            lsm[default_id] = utility
            lsm.registerUtility(utility, interface, name=name)
Beispiel #2
0
 def create(self, data):  # pylint: disable=unused-argument
     """Create new content from form data"""
     if self.content_factory is not None:
         factory = get_object_factory(self.content_factory) \
             if is_interface(self.content_factory) else self.content_factory
         return factory()  # pylint: disable=not-callable
     raise NotImplementedError
Beispiel #3
0
def get_annotation_adapter(context,
                           key,
                           factory=None,
                           markers=None,
                           notify=True,
                           locate=True,
                           parent=None,
                           name=None,
                           callback=None,
                           **kwargs):
    # pylint: disable=too-many-arguments
    """Get an adapter via object's annotations, creating it if not existent

    :param object context: context object which should be adapted
    :param str key: annotations key to look for
    :param factory: if annotations key is not found, this is the factory which will be used to
        create a new object; factory can be a class or callable object, or an interface for which
        a factory has been registered; if factory is None and is requested object can't be found,
        None is returned
    :param markers: if not None, list of marker interfaces which created adapter should provide
    :param bool=True notify: if 'False', no notification event will be sent on object creation
    :param bool=True locate: if 'False', the new object is not attached to any parent
    :param object=None parent: parent to which new object is attached; if None, object is
        attached to context
    :param str=None name: if locate is not False, this is the name with which the new object is
        attached to it's parent
    :param callback: if not None, callback function which will be called after object creation
    """
    annotations = IAnnotations(context, None)
    if annotations is None:
        return None
    adapter = annotations.get(key)  # pylint: disable=assignment-from-no-return
    if adapter is None:
        if 'default' in kwargs:
            return kwargs['default']
        if factory is None:
            return None
        if is_interface(factory):
            factory = get_object_factory(factory)
            assert factory is not None, "Missing object factory"
        adapter = annotations[key] = factory()
        if markers:
            if not isinstance(markers, (list, tuple, set)):
                markers = {markers}
            for marker in markers:
                alsoProvides(adapter, marker)
        if notify:
            get_current_registry().notify(ObjectCreatedEvent(adapter))
        if locate:
            zope_locate(adapter, context if parent is None else parent, name)
        if callback:
            callback(adapter)
    return adapter
Beispiel #4
0
def handle_workflow_version_transition(event):
    """Handle workflow version transition"""
    principal = event.principal
    factory = get_object_factory(IWorkflowStateHistoryItem)
    if factory is not None:
        item = factory(date=gmtime(datetime.utcnow()),
                       source_version=IWorkflowState(
                           event.old_object).version_id,
                       source_state=event.source,
                       target_state=event.destination,
                       transition_id=event.transition.transition_id,
                       principal=principal.id
                       if IPrincipalInfo.providedBy(principal) else principal,
                       comment=event.comment)
        IWorkflowState(event.object).history.append(item)  # pylint: disable=no-member
Beispiel #5
0
def handle_cloned_object(event):
    """Add comment when an object is cloned"""
    request = check_request()
    translate = request.localizer.translate
    source_state = IWorkflowState(event.source)
    factory = get_object_factory(IWorkflowStateHistoryItem)
    if factory is not None:
        item = factory(
            date=datetime.utcnow(),
            principal=request.principal.id,
            comment=translate(
                _("Clone created from version {source} (in « {state} » "
                  "state)")).format(source=source_state.version_id,
                                    state=translate(
                                        IWorkflow(
                                            event.source).get_state_label(
                                                source_state.state))))
        target_state = IWorkflowState(event.object)
        target_state.history.clear()  # pylint: disable=no-member
        target_state.history.append(item)  # pylint: disable=no-member
Beispiel #6
0
 def __init__(self, context, request):
     super().__init__(context, request)
     factory = get_object_factory(self.search_form) if is_interface(self.search_form) \
         else self.search_form
     self.search_form = factory(context, request)