"""Module saves metadata about uploaded asset.""" import os from typing import Any from werkzeug.exceptions import NotFound from assets.constants import api, error from assets.models import asset_upload as asset_upload_model def update_asset_upload( filename: str, upc: int | None, product_id: int | None, track_unique_id: int | None, original_filename: str | None, is_correction: bool = False, ) -> dict[str, str]: """Save metadata about uploaded asset into asset_upload table. Args: filename (str): Filename of uploaded asset. upc (int | None): UPC. product_id (int | None): Product id. track_unique_id (int | None): Track unique id. original_filename (str | None): Original asset filename. is_correction (bool): Is correction flag. Returns: dict: Success or error message. """ clean_filename, ext = os.path.splitext(filename) asset_upload_model.get_asset_upload(clean_filename, api_version=api.API_VERSION_V2) asset_upload_model.update_asset_upload( filename=clean_filename, upc=upc, track_unique_id=track_unique_id, product_id=product_id, original_filename=original_filename, is_correction=is_correction, api_version=api.API_VERSION_V2, ) return {"status": error.SUCCESS_CODE} def apply_asset_corrections(product_id: int) -> list[dict[str, Any]]: """Apply asset corrections for product_id. Args: product_id (int): Product id. Returns: list: apply result """ approve_response = asset_upload_model.apply_asset_corrections(product_id) if not approve_response: raise NotFound("No rows updated.") return approve_response def get_asset_upload_by_filename(filename: str) -> dict[str, Any]: """Get asset upload by filename.""" clean_filename, ext = os.path.splitext(filename) return asset_upload_model.get_asset_upload( clean_filename, api_version=api.API_VERSION_V2 )