import pathlib from .model import model from .proxy import proxy from .field_generator import field_generator from .constants import constants from .validators import validators from .attachment_file import attachment_file from .exceptions import resources_not_exists class attachment(model): """ That model store attachment to the item. Attachment is file, which could be downloaded by the user, like instruction in PDF, or used in frontend like images. Fields ------ name : str Name of the attachment visible for user. description : str Description of the attachment visible for user. """ name = field_generator.name() description = field_generator.description() resources = field_generator.path() def _validators(self) -> dict: """ That function return dict of validators which would be used to validate text fields of the model. Returns ------- dict Validators which would be used to validate text fields. """ return { "name": validators.name, "description": validators.description, "resources": validators.path } class attachment_proxy(proxy): """ That class is proxy for the attachment. It could be used to working witch attachments in cleaner and better way. Methods ------- @classmethod create : proxy That create new attachment from that name. set_name : None That change visible name of the attachment. set_description : None That change description of the attachment. """ @classmethod def create(cls, name: str, resources: attachment_file) -> proxy: """ That create new proxy with new object, which does not exists in database inside. Parameters ---------- name : str Name of the new attachment. resources : str Path in the resources directory to the attachment file. Raises ------ resources_not_exists When attachment file does not exists on disk. Returns ------- proxy New proxy with new attachment inside it. """ if not resources.exists(): raise resources_not_exists(resources.path) return cls(attachment( name = name, description = constants.empty_text(), resources = resources.name )) def set_resources(self, target: attachment_file) -> None: """ That change path to the attachment file in the resources directory. Parameters ---------- target : pathlib.Path Path to new attachment file in the resources directory. Raises ------ resources_not_exists When attachment file does not exists on disk. """ if not target.exists(): raise resources_not_exists(target.path) self._target.resources = target.name def set_name(self, target: str) -> None: """ That change name of the attachment Parameters ---------- target : str New name of the attachment to set. """ self._target.name = target def set_description(self, target: str) -> None: """ That chanhe description of the attachment. Parameters ---------- target : str New description of the attachment to set. """ self._target.description = target