async def create_content_in_container(parent: Folder, type_: str, id_: str, request: IRequest = None, check_security=True, **kw) -> Resource: """Utility to create a content. This method is the one to use to create content. id_ can be None :param parent: where to create content inside of :param type_: content type to create :param id_: id to give content in parent object :param request: <optional> :param check_security: be able to disable security checks """ factory = get_cached_factory(type_) if check_security and factory.add_permission: if factory.add_permission in PERMISSIONS_CACHE: permission = PERMISSIONS_CACHE[factory.add_permission] else: permission = query_utility(IPermission, name=factory.add_permission) PERMISSIONS_CACHE[factory.add_permission] = permission if request is None: request = get_current_request() if permission is not None and \ not IInteraction(request).check_permission(permission.id, parent): raise NoPermissionToAdd(str(parent), type_) constrains = IConstrainTypes(parent, None) if constrains is not None: if not constrains.is_type_allowed(type_): raise NotAllowedContentType(str(parent), type_) # We create the object with at least the ID obj = factory(id=id_, parent=parent) for key, value in kw.items(): if key == 'id': # the factory sets id continue setattr(obj, key, value) txn = getattr(parent, '_p_jar', None) or get_transaction() if txn is None or not txn.storage.supports_unique_constraints: # need to manually check unique constraints if await parent.async_contains(obj.id): raise ConflictIdOnContainer(f'Duplicate ID: {parent} -> {obj.id}') obj.__new_marker__ = True await notify(BeforeObjectAddedEvent(obj, parent, id_)) await parent.async_set(obj.id, obj) return obj
async def create_content_in_container(container, type_, id_, request=None, check_security=True, **kw): """Utility to create a content. This method is the one to use to create content. id_ can be None """ factory = get_cached_factory(type_) if check_security and factory.add_permission: if factory.add_permission in PERMISSIONS_CACHE: permission = PERMISSIONS_CACHE[factory.add_permission] else: permission = query_utility(IPermission, name=factory.add_permission) PERMISSIONS_CACHE[factory.add_permission] = permission if request is None: request = get_current_request() if permission is not None and \ not IInteraction(request).check_permission(permission.id, container): raise NoPermissionToAdd(str(container), type_) constrains = IConstrainTypes(container, None) if constrains is not None: if not constrains.is_type_allowed(type_): raise NotAllowedContentType(str(container), type_) # We create the object with at least the ID obj = factory(id=id_, parent=container) for key, value in kw.items(): setattr(obj, key, value) txn = getattr(container, '_p_jar', None) or get_transaction() if txn is None or not txn.storage.supports_unique_constraints: # need to manually check unique constraints if await container.async_contains(obj.id): raise ConflictIdOnContainer( f'Duplicate ID: {container} -> {obj.id}') obj.__new_marker__ = True await notify(BeforeObjectAddedEvent(obj, container, id_)) await container.async_set(obj.id, obj) return obj
async def create_content_in_container(container, type_, id_, request=None, **kw): """Utility to create a content. This method is the one to use to create content. id_ can be None """ factory = get_cached_factory(type_) if factory.add_permission: if factory.add_permission in PERMISSIONS_CACHE: permission = PERMISSIONS_CACHE[factory.add_permission] else: permission = query_utility(IPermission, name=factory.add_permission) PERMISSIONS_CACHE[factory.add_permission] = permission if request is None: request = get_current_request() if permission is not None and \ not IInteraction(request).check_permission(permission.id, container): raise NoPermissionToAdd(str(container), type_) constrains = IConstrainTypes(container, None) if constrains is not None: if not constrains.is_type_allowed(type_): raise NotAllowedContentType(str(container), type_) # We create the object with at least the ID obj = factory(id=id_) obj.__parent__ = container obj.__name__ = obj.id for key, value in kw.items(): setattr(obj, key, value) if request is None or 'OVERWRITE' not in request.headers: if await container.async_contains(obj.id): raise ConflictIdOnContainer(str(container), obj.id) obj.__new_marker__ = True await notify(BeforeObjectAddedEvent(obj, container, id_)) await container.async_set(obj.id, obj) return obj