"""Image Assets Model. Image Assets model for returning a path given a filename and image category """ from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import Enum from sqlalchemy import Integer from sqlalchemy import String from images import config from images import response from images.connectors import mysql from images.constants import errors from images.models import base MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/tiff'] DEFAULT_IMAGE_FORMAT = '/release/large_cover' class ImageAssets(base.BaseModel): """Class representing the image_assets table from art_relations. Attributes: id: primary key category_id: image category identifier path: relative path for images server filename: image filename named as release UPC height: image height width: image width mime_type: image file type file_size: image file size date_added: date image added date_modified: date image modified """ __tablename__ = 'image_assets' id = Column(Integer, primary_key=True) category_id = Column(Integer) path = Column(String) filename = Column(String) height = Column(Integer) width = Column(Integer) mime_type = Column(Enum(*MIME_TYPES)) file_size = Column(Integer) date_added = Column(DateTime) date_modified = Column(DateTime) class ImportAssetDetail(base.BaseModel): """Class representing the import_asset_detail table from art_relations. Attributes: id: primary key import_asset_id: id for asset in import asset table upc: release upc relating to asset track_id: id for track """ __tablename__ = 'import_asset_detail' id = Column(Integer, primary_key=True) import_asset_id = Column(Integer) upc = Column(Integer) track_id = Column(Integer) class ReleaseCoverImages(base.BaseModel): """Class representing the release_cover_images table from art_relations. Attributes: release_cover_images_id: primary key upc: release upc relating to asset image_asset_id: foreign key to id in import asset table """ __tablename__ = 'release_cover_images' release_cover_images_id = Column(Integer, primary_key=True) upc = Column(Integer) image_asset_id = Column(Integer) def get_file_path_for_image(import_asset_id): """Get file path for image asset from import_asset_id. Performs two queries, first retrieving the upc from import_asset_detail, then retrieving the filepath from image_assets. Finally, calls function to generate a complete URL based on the queried informaiton and env variables. Args: import_asset_id (int): import_asset_id for the requested image Returns: response.Response: with the url for this image """ asset_upc_response = _get_import_asset_upc(import_asset_id) if not asset_upc_response: return asset_upc_response return get_image_path_by_upc(asset_upc_response.message) def get_image_path_by_upc(upc): """Get file path for image asset from UPC. Retreives the filepath from image_assets and calls a function to generate a complete URL based on queried info. Args: upc (int): upc for the requested image Returns: response.Response: with the url for this image """ filename = ''.join([str(upc), '.jpg']) relative_path_response = _get_relative_path(upc) if not relative_path_response: return relative_path_response relative_path = relative_path_response.message asset_url = _get_asset_url_for_image_asset(relative_path, filename) return response.Response(message={'path': asset_url}) def _get_import_asset_upc(import_asset_id): """Get upc for image asset from import_asset_id. Query import_asset_detail table in art relations for upc Args: import_asset_id (integer) Returns: response.Response: upc for import_asset_id """ with mysql.db_session() as session: full_query = session.query(ImportAssetDetail).filter_by( import_asset_id=import_asset_id) import_asset_detail = full_query.first() if not import_asset_detail: return response.create_not_found_response( message=errors.IMPORT_UPC_NOT_FOUND) import_asset_upc = import_asset_detail.upc return response.Response(message=import_asset_upc) def _get_relative_path(upc): """Get relative path from upc. Query image_assets table in art relations for path based on upc from release_cover_images table Args: upc (str): the upc of the image Returns: response.Response: the relative file path of this image """ with mysql.db_session() as session: full_query = session.query(ImageAssets).join( ReleaseCoverImages, ImageAssets.id == ReleaseCoverImages.image_asset_id ).filter_by(upc=upc) image_asset = full_query.first() if not image_asset: return response.create_not_found_response( message=errors.IMAGE_PATH_NOT_FOUND) relative_path = image_asset.path return response.Response(message=relative_path) def _get_asset_url_for_image_asset(relative_path, filename, url_format=None): """Get an asset url from a relative path and filename. Args: relative_path (str): the relative file path from image_assets table filename (str): the filename for the image asset format (str): the format of the image to be returned Return: str: the path of the image """ url_format = url_format or DEFAULT_IMAGE_FORMAT return ''.join([ config.IMAGES_URL, url_format, relative_path, filename])