import tortoise.transactions from .container_blob import encoded from .attachment import attachment from .attachment import attachment_proxy from .attachments_directory import attachments_directory from .exceptions import id_is_invalid from .exceptions import not_found from .validators import validators class attachments_manager: def __init__(self, directory: attachments_directory) -> None: self.__directory = directory @property def directory(self) -> attachments_directory: return self.__directory async def get_all(self) -> tuple: items = await attachment.all() return tuple(items) async def upload( self, content: str, extension: str, name: str, description: str ) -> int: decoded = await encoded(content).decode() decoded = decoded.result() proxy = await self.directory.store(decoded, extension) proxy.set_name(name) proxy.set_description(description) created = proxy.result() async with tortoise.transactions.in_transaction(): await created.save() return created.get_identifier() async def edit( self, id: int, name: str, description: str ) -> int: async with tortoise.transactions.in_transaction(): target = await self.require_by_id(id) proxy = attachment_proxy(target) proxy.set_name(name) proxy.set_description(description) result = proxy.result() await result.save() return result.get_identifier() async def remove(self, id: int) -> None: target = await self.require_by_id(id) items = await target.get_related("item", "attachments") await target.delete() async def get_by_id(self, id: int) -> attachment | None: if validators.id(id) is False: raise id_is_invalid(id) async with tortoise.transactions.in_transaction(): return await attachment.filter(id = id).first() async def require_by_id(self, id: int) -> attachment: result = await self.get_by_id(id) if result is None: raise not_found("attachment", id = id) return result