def create_collection_items( user: InternalUserDTO, collection_uuid: UUID, items: List[ItemDTO], replace: bool = False, ) -> List[ItemDTO]: collection = Collection.find_writeable_or_fail(user, collection_uuid) if replace: Item.where(collection_uuid=collection.uuid).delete() new_items: List[Item] = [ Item(**{ **item.to_dict(), **{ "uuid": uuid4() } }) for item in items ] Item.session.bulk_save_objects(new_items) Item.session.commit() uuids = [] for item in new_items: if isinstance(item.uuid, str): uuids.append(UUID(item.uuid)) else: uuids.append(item.uuid) fetched_items = Item.find_writeable(user, uuids) return to_models(fetched_items, ItemDTO)
def create_collection_item(user: InternalUserDTO, collection_uuid: UUID, item_dto: ItemDTO) -> ItemDTO: coll = Collection.find_writeable_or_fail(user, collection_uuid) item_dto.collection_uuid = coll.uuid # type: ignore item = Item(**item_dto.to_dict()) item.save() item.session.commit() return to_model(item, ItemDTO)
def update_collection_by_uuid( user: InternalUserDTO, collection_uuid: UUID, collection_update: CollectionDTO ) -> CollectionDTO: collection = Collection.find_writeable_or_fail(user, collection_uuid) collection.name = collection_update.name collection.is_public = collection_update.is_public collection.save() collection.session.commit() return to_model(collection, CollectionDTO)
def create_acl(user: InternalUserDTO, acl: ACLDTO) -> ACLDTO: if acl.collection_uuid: collection = Collection.find_writeable_or_fail(user, acl.collection_uuid) if collection.provider_uuid != user.provider_uuid: raise PermissionError if acl.item_uuid: item = Item.find_writeable_or_fail(user, acl.item_uuid) if item.collection.provider_uuid != user.provider_uuid: raise PermissionError if acl.granted_provider_uuid: Provider.find_or_fail(acl.granted_provider_uuid) if acl.granted_user_uuid: User.find_or_fail(acl.granted_user_uuid) acl.provider_uuid = user.provider_uuid acl = ACL(**acl.to_dict()) acl.save() acl.session.commit() return to_model(acl, ACLDTO)
def delete_collection_by_uuid(user: InternalUserDTO, collection_uuid: UUID) -> None: collection = Collection.find_writeable_or_fail(user, collection_uuid) collection.delete() collection.session.commit()