def __call__(self, parent=None, appstructs=None, run_after_creation=True, creator=None, registry=None, request=None, send_event=True, autoupdated=False, anonymized_creator=None, **kwargs): """Triggered when a ResourceFactory instance is called. Kwargs:: parent (IPool or None): Add the new resource to this pool. None value is allowed to create non persistent Resources (without OID/parent). Defaults to None. appstructs (dict): Key/Values of sheet appstruct data. Key is identifier of a sheet interface. Value is the data to set. after_creation (bool): Whether to invoke after_creation hooks, If parent is None you should set this False Default is True. creator (IResource or None): The resource of the creating user to set the right metadata. registry (Registry or None): Registry passed to creation eventes. If None :func:`pyramid.threadlocal.get_current_registry` is called. Default is None. request (Request or None): passed to :class:`adhocracy_core.interfaces.IResourceSheetModified'events send_event (bool): send :class:`adhocracy_core.interfaces.IResourceCreatedAndAdded` event. Default is True. autoupdated (bool): The creation is caused by a modified referenced resource, no real content is modified. Default is False. anonymized_creator (IResource or None): The resource of the anonymized user, if any. **kwargs: Arbitary keyword arguments. Will be passed along with `creator` and `autoupdated` to the `after_creation` hook as 3rd argument `options`. Returns: object (IResource): the newly created resource Raises: KeyError: if self.metadata.use_autonaming is False and the `resource name` is not given or already used in the `parent` pool. You can set the `resource name` with appstruct data for the name sheet (:mod:`adhocracy_core.sheets.name`). ComponentLookupError: if `appstructs` contains sheet data for non existing sheets. """ appstructs = appstructs or dict() resource = self.meta.content_class() directlyProvides(resource, self.meta.iresource) isheets = self.meta.basic_sheets + self.meta.extended_sheets alsoProvides(resource, isheets) if registry is None: registry = get_current_registry() if parent is not None: self._add(parent, resource, appstructs, registry) else: resource.__parent__ = None resource.__name__ = '' default_workflow = self.meta.default_workflow has_workflow = IWorkflowAssignment in isheets and default_workflow assignment = IWorkflowAssignment.__identifier__ if has_workflow: if assignment not in appstructs: appstructs[assignment] = \ {'workflow': default_workflow} elif 'workflow' not in appstructs[assignment]: # pragma: no branch appstructs[assignment]['workflow'] = default_workflow for key, struct in appstructs.items(): isheet = DottedNameResolver().maybe_resolve(key) sheet = registry.content.get_sheet(resource, isheet, request=request) if sheet.meta.creatable: sheet.set(struct, send_event=False, autoupdated=autoupdated) from adhocracy_core.resources.principal import IUser # prevent circles if IUser.providedBy(resource): # TODO Why? creator = resource self._set_local_role_creator(resource, creator, anonymized_creator, registry) if IMetadata.providedBy(resource): metadata = self._get_metadata(resource, creator, registry) sheet = registry.content.get_sheet(resource, IMetadata, request=request) sheet.set(metadata, send_event=False, omit_readonly=False, autoupdated=autoupdated) if run_after_creation: for call in self.meta.after_creation: kwargs['creator'] = creator kwargs['autoupdated'] = autoupdated call(resource, registry, options=kwargs) if send_event: self._notify_new_resource_created_and_added( resource, registry, creator, autoupdated, ) return resource
def __call__(self, parent=None, appstructs={}, run_after_creation=True, creator=None, registry=None, request=None, send_event=True, **kwargs ): """Triggered when a ResourceFactory instance is called. Kwargs:: parent (IPool or None): Add the new resource to this pool. None value is allowed to create non persistent Resources (without OID/parent). Defaults to None. appstructs (dict): Key/Values of sheet appstruct data. Key is identifier of a sheet interface. Value is the data to set. after_creation (bool): Whether to invoke after_creation hooks, If parent is None you should set this False Default is True. creator (IResource or None): The resource of the creating user to set the right metadata. registry (Registry or None): Registry passed to creation eventes. If None :func:`pyramid.threadlocal.get_current_registry` is called. Default is None. request (Request or None): passed to :class:`adhocracy_core.interfaces.IResourceSheetModified'events send_event (bool): send :class:`adhocracy_core.interfaces.IResourceCreatedAndAdded` event. Default is True. **kwargs: Arbitary keyword arguments. Will be passed along with 'creator' to the `after_creation` hook as 3rd argument `options`. Returns: object (IResource): the newly created resource Raises: KeyError: if self.metadata.use_autonaming is False and the `resource name` is not given or already used in the `parent` pool. You can set the `resource name` with appstruct data for the name sheet (:mod:`adhocracy_core.sheets.name`). ComponentLookupError: if `appstructs` contains sheet data for non existing sheets. """ resource = self.meta.content_class() directlyProvides(resource, self.meta.iresource) isheets = self.meta.basic_sheets + self.meta.extended_sheets alsoProvides(resource, isheets) if registry is None: registry = get_current_registry() if parent is not None: self._add(parent, resource, appstructs, registry) else: resource.__parent__ = None resource.__name__ = '' for key, struct in appstructs.items(): isheet = DottedNameResolver().maybe_resolve(key) sheet = get_sheet(resource, isheet, registry=registry) if sheet.meta.creatable: sheet.set(struct, send_event=False, request=request) # Fixme: Sideffect. We change here the passed creator because the # creator of user resources should always be the created user. # A better solution would be to have custom adapter to add # resources. # To prevent import circles we do not import at module level. from adhocracy_core.resources.principal import IUser if IUser.providedBy(resource): creator = resource if creator is not None: userid = resource_path(creator) set_local_roles(resource, {userid: {'role:creator'}}) if IMetadata.providedBy(resource): metadata = self._get_metadata(resource, creator, registry) sheet = get_sheet(resource, IMetadata, registry=registry) sheet.set(metadata, send_event=False, request=request, omit_readonly=False) if run_after_creation: for call in self.meta.after_creation: kwargs['creator'] = creator call(resource, registry, options=kwargs) if send_event: self._notify_new_resource_created_and_added(resource, registry, creator) return resource
def _get_users(root: IResource) -> [IUser]: users = find_service(root, 'principals', 'users') return [u for u in users.values() if IUser.providedBy(u)]
def __call__(self, parent=None, appstructs=None, run_after_creation=True, creator=None, registry=None, request=None, send_event=True, autoupdated=False, anonymized_creator=None, **kwargs ): """Triggered when a ResourceFactory instance is called. Kwargs:: parent (IPool or None): Add the new resource to this pool. None value is allowed to create non persistent Resources (without OID/parent). Defaults to None. appstructs (dict): Key/Values of sheet appstruct data. Key is identifier of a sheet interface. Value is the data to set. after_creation (bool): Whether to invoke after_creation hooks, If parent is None you should set this False Default is True. creator (IResource or None): The resource of the creating user to set the right metadata. registry (Registry or None): Registry passed to creation eventes. If None :func:`pyramid.threadlocal.get_current_registry` is called. Default is None. request (Request or None): passed to :class:`adhocracy_core.interfaces.IResourceSheetModified'events send_event (bool): send :class:`adhocracy_core.interfaces.IResourceCreatedAndAdded` event. Default is True. autoupdated (bool): The creation is caused by a modified referenced resource, no real content is modified. Default is False. anonymized_creator (IResource or None): The resource of the anonymized user, if any. **kwargs: Arbitary keyword arguments. Will be passed along with `creator` and `autoupdated` to the `after_creation` hook as 3rd argument `options`. Returns: object (IResource): the newly created resource Raises: KeyError: if self.metadata.use_autonaming is False and the `resource name` is not given or already used in the `parent` pool. You can set the `resource name` with appstruct data for the name sheet (:mod:`adhocracy_core.sheets.name`). ComponentLookupError: if `appstructs` contains sheet data for non existing sheets. """ appstructs = appstructs or dict() resource = self.meta.content_class() directlyProvides(resource, self.meta.iresource) isheets = self.meta.basic_sheets + self.meta.extended_sheets alsoProvides(resource, isheets) if registry is None: registry = get_current_registry() if parent is not None: self._add(parent, resource, appstructs, registry) else: resource.__parent__ = None resource.__name__ = '' default_workflow = self.meta.default_workflow has_workflow = IWorkflowAssignment in isheets and default_workflow assignment = IWorkflowAssignment.__identifier__ if has_workflow: if assignment not in appstructs: appstructs[assignment] = \ {'workflow': default_workflow} elif 'workflow' not in appstructs[assignment]: # pragma: no branch appstructs[assignment]['workflow'] = default_workflow for key, struct in appstructs.items(): isheet = DottedNameResolver().maybe_resolve(key) sheet = registry.content.get_sheet(resource, isheet, request=request) if sheet.meta.creatable: sheet.set(struct, send_event=False, autoupdated=autoupdated) from adhocracy_core.resources.principal import IUser # prevent circles if IUser.providedBy(resource): # TODO Why? creator = resource self._set_local_role_creator(resource, creator, anonymized_creator, registry) if IMetadata.providedBy(resource): metadata = self._get_metadata(resource, creator, registry) sheet = registry.content.get_sheet(resource, IMetadata, request=request) sheet.set(metadata, send_event=False, omit_readonly=False, autoupdated=autoupdated ) if run_after_creation: for call in self.meta.after_creation: kwargs['creator'] = creator kwargs['autoupdated'] = autoupdated call(resource, registry, options=kwargs) if send_event: self._notify_new_resource_created_and_added(resource, registry, creator, autoupdated, ) return resource