""" Project: PARTS Website Author: Edward Middleton-Smith Precision And Research Technology Systems Limited Technology: Business Objects Feature: Image Business Object """ # internal from business_objects.base import Base from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base from business_objects.file_type import File_Type from business_objects.dog.dog import Dog import lib.argument_validation as av from extensions import db # from forms.dog.image import Filters_Image from helpers.helper_app import Helper_App # external from dataclasses import dataclass from typing import ClassVar class Image(SQLAlchemy_ABC, Base): ATTR_ID_IMAGE: ClassVar[str] = 'id_image' FLAG_IMAGE: ClassVar[str] = 'image' NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_ID_IMAGE NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_NAME __tablename__ = 'DOG_Image' __table_args__ = { 'extend_existing': True } id_image = db.Column(db.Integer, primary_key=True) id_file_type = db.Column(db.Integer) id_dog = db.Column(db.Integer) path = db.Column(db.String(1024)) name = db.Column(db.String(1024)) active = db.Column(db.Boolean) created_on = db.Column(db.DateTime) def __init__(self): self.id_image = 0 self.is_new = False super().__init__() """ @classmethod def from_db_image(cls, query_row): _m = f'{cls.__qualname__}.from_db_image' image = cls() image.id_image = query_row[0] image.code = query_row[3] image.name = query_row[4] image.active = av.input_bool(query_row[5], 'active', _m) # image.created_on = query_row[7] return image """ @classmethod def from_db_button_icon(cls, query_row): _m = f'{cls.__qualname__}.from_db_button_icon' image = cls() image.id_image = query_row[1] image.path = query_row[2] image.name = query_row[3] image.active = True return image @classmethod def from_db_command_button_link(cls, query_row): _m = f'{cls.__qualname__}.from_db_command_button_link' image = cls() image.id_image = query_row[11] image.path = query_row[12] image.name = query_row[13] image.active = True return image @classmethod def from_json(cls, json): _m = f'{cls.__qualname__}.from_json' image = cls() if json is None: return image # Helper_App.console_log(f'{_m}\njson: {json}') image.id_image = json.get(Image.ATTR_ID_IMAGE, -1) image.id_file_type = json[File_Type.FLAG_FILE_TYPE] image.id_dog = json[Dog.FLAG_DOG] image.path = json[cls.FLAG_PATH] image.name = json[cls.FLAG_NAME] image.active = av.input_bool(json[cls.FLAG_ACTIVE], cls.FLAG_ACTIVE, _m) image.created_on = json.get(cls.FLAG_CREATED_ON, None) # Helper_App.console_log(f'Image: {image}') return image def to_json(self): as_json = { **self.get_shared_json_attributes(self) , self.ATTR_ID_IMAGE: self.id_image , self.FLAG_CODE: self.code , self.FLAG_NAME: self.name , self.FLAG_ACTIVE: self.active , self.FLAG_CREATED_ON: self.created_on } # Helper_App.console_log(f'as_json: {as_json}') return as_json def __repr__(self): return f''' {self.__class__.__name__}( {self.FLAG_IMAGE}: {self.id_image} {self.FLAG_CODE}: {self.code} {self.FLAG_NAME}: {self.name} {self.FLAG_ACTIVE}: {self.active} {self.FLAG_CREATED_ON}: {self.created_on} ) ''' class Image_Temp(db.Model, Base): __tablename__ = 'DOG_Image_Temp' __table_args__ = { 'extend_existing': True } id_temp = db.Column(db.Integer, primary_key=True) id_image = db.Column(db.Integer) code = db.Column(db.String(250)) name = db.Column(db.String(250)) active = db.Column(db.Boolean) # created_on = db.Column(db.DateTime) guid: str = db.Column(db.String(36)) def __init__(self): super().__init__() @classmethod def from_image(cls, image): _m = 'Image_Temp.from_image' temp = cls() temp.id_image = image.id_image temp.id_image = image.id_image temp.code = image.code temp.name = image.name temp.active = image.active # temp.created_on = image.created_on return temp def __repr__(self): return f''' {self.__class__.__name__}( {Image.FLAG_IMAGE}: {self.id_image} {Image.FLAG_IMAGE}: {self.id_image} {self.FLAG_CODE}: {self.code} {self.FLAG_NAME}: {self.name} {self.FLAG_ACTIVE}: {self.active} ) ''' class Parameters_Image(Get_Many_Parameters_Base): get_all_file_type: bool get_inactive_file_type: bool ids_file_type: str names_file_type: str get_all_dog: bool get_inactive_dog: bool ids_dog: str names_dog: str get_all_image: bool get_inactive_image: bool ids_image: str names_image: str require_all_id_search_filters_met: bool require_any_id_search_filters_met: bool require_all_non_id_search_filters_met: bool require_any_non_id_search_filters_met: bool @classmethod def get_default(cls): return cls( get_all_file_type = True , get_inactive_file_type = False , ids_file_type = '' , names_file_type = '' , get_all_dog = True , get_inactive_dog = False , ids_dog = '' , names_dog = '' , get_all_image = True , get_inactive_image = False , ids_image = '' , names_image = '' , require_all_id_search_filters_met = True , require_any_id_search_filters_met = True , require_all_non_id_search_filters_met = False , require_any_non_id_search_filters_met = True ) @classmethod def from_json(cls, json): return cls( get_all_file_type = json.get('a_get_all_file_type', False) , get_inactive_file_type = json.get('a_get_inactive_file_type', False) , ids_file_type = json.get('a_ids_file_type', '') , names_file_type = json.get('a_names_file_type', '') , get_all_dog = json.get('a_get_all_dog', False) , get_inactive_dog = json.get('a_get_inactive_dog', False) , ids_dog = json.get('a_ids_dog', '') , names_dog = json.get('a_names_dog', '') , get_all_image = json.get('a_get_all_image', False) , get_inactive_image = json.get('a_get_inactive_image', False) , ids_image = json.get('a_ids_image', '') , names_image = json.get('a_names_image', '') , require_all_id_search_filters_met = json.get('a_require_all_id_search_filters_met', True) , require_any_id_search_filters_met = json.get('a_require_any_id_search_filters_met', True) , require_all_non_id_search_filters_met = json.get('a_require_all_non_id_search_filters_met', False) , require_any_non_id_search_filters_met = json.get('a_require_any_non_id_search_filters_met', True) ) """ @classmethod def from_form_filters_image(cls, form): av.val_instance(form, 'form', 'Parameters_Image.from_form_filters_image', Filters_Image) has_filter_search_text = not (form.search.data == '' or form.search.data is None) active_only = av.input_bool(form.active_only.data, "active", "Parameters_Image.from_form_filters_image") filters = cls.get_default() filters.get_all_image = True filters.get_inactive_image = not active_only filters.ids_image = '' filters.names_image = form.search.data if has_filter_search_text else '' return filters """ def to_json(self): return { 'a_get_all_image': self.get_all_image , 'a_get_inactive_image': self.get_inactive_image , 'a_ids_image': self.ids_image , 'a_names_image': self.names_image , 'a_require_all_id_search_filters_met': self.require_all_id_search_filters_met , 'a_require_any_id_search_filters_met': self.require_any_id_search_filters_met , 'a_require_all_non_id_search_filters_met': self.require_all_non_id_search_filters_met , 'a_require_any_non_id_search_filters_met': self.require_any_non_id_search_filters_met , 'a_output_images': self.output_images }