""" Project: PARTS Website Author: Edward Middleton-Smith Precision And Research Technology Systems Limited Technology: Business Objects Feature: Location Business Object """ # internal from business_objects.base import Base from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base import lib.argument_validation as av from extensions import db from forms.dog.location import Filters_Location from helpers.helper_app import Helper_App # external from dataclasses import dataclass from typing import ClassVar class Location(SQLAlchemy_ABC, Base): ATTR_ID_LOCATION: ClassVar[str] = 'id_location' FLAG_LOCATION: ClassVar[str] = 'location' FLAG_LOCATION_PARENT: ClassVar[str] = 'location_parent' NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_ID_LOCATION NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_NAME __tablename__ = 'DOG_Location' __table_args__ = { 'extend_existing': True } id_location = db.Column(db.Integer, primary_key=True) id_location_parent = 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) def __init__(self): self.id_location = 0 self.location_parent = None self.is_new = False super().__init__() @classmethod def from_db_location(cls, query_row): _m = f'{cls.__qualname__}.from_db_location' location = cls() location.id_location = query_row[0] location.id_location_parent = query_row[1] location.code = query_row[3] location.name = query_row[4] location.active = av.input_bool(query_row[5], 'active', _m) # location.created_on = query_row[7] location.location_parent = cls() location.location_parent.id_location = location.id_location_parent location.location_parent.name = query_row[2] return location @classmethod def from_db_command_button_link(cls, query_row): _m = f'{cls.__qualname__}.from_db_command_button_link' location = cls() location.id_location = query_row[14] location.name = query_row[15] location.active = True return location @classmethod def from_db_assessment(cls, query_row): _m = f'{cls.__qualname__}.from_db_assessment' location = cls() location.id_location = query_row[5] location.name = query_row[6] location.active = True return location @classmethod def from_json(cls, json): _m = f'{cls.__qualname__}.from_json' location = cls() if json is None: return location # Helper_App.console_log(f'{_m}\njson: {json}') location.id_location = json.get(Location.ATTR_ID_LOCATION, -1) location.id_location_parent = json[Location.FLAG_LOCATION_PARENT] location.name = json[cls.FLAG_NAME] location.code = json.get(cls.FLAG_CODE, location.name.upper().replace(" ", "_")) location.active = av.input_bool(json[cls.FLAG_ACTIVE], cls.FLAG_ACTIVE, _m) location.created_on = json.get(cls.FLAG_CREATED_ON, None) # Helper_App.console_log(f'Location: {location}') return location def to_json(self): as_json = { **self.get_shared_json_attributes(self) , self.ATTR_ID_LOCATION: self.id_location , Location.FLAG_LOCATION_PARENT: self.id_location_parent , 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_LOCATION}: {self.id_location} {Location.FLAG_LOCATION_PARENT}: {self.id_location_parent} {self.FLAG_CODE}: {self.code} {self.FLAG_NAME}: {self.name} {self.FLAG_ACTIVE}: {self.active} {self.FLAG_CREATED_ON}: {self.created_on} ) ''' class Location_Temp(db.Model, Base): __tablename__ = 'DOG_Location_Temp' __table_args__ = { 'extend_existing': True } id_temp = db.Column(db.Integer, primary_key=True) id_location = db.Column(db.Integer) id_location_parent = 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_location(cls, location): _m = 'Location_Temp.from_location' temp = cls() temp.id_location = location.id_location temp.id_location_parent = location.id_location_parent temp.code = location.code temp.name = location.name temp.active = location.active # temp.created_on = location.created_on return temp def __repr__(self): return f''' {self.__class__.__name__}( {Location.FLAG_LOCATION}: {self.id_location} {Location.FLAG_LOCATION_PARENT}: {self.id_location_parent} {self.FLAG_CODE}: {self.code} {self.FLAG_NAME}: {self.name} {self.FLAG_ACTIVE}: {self.active} ) ''' class Parameters_Location(Get_Many_Parameters_Base): get_all_location: bool get_inactive_location: bool ids_location: str names_location: str get_all_user: bool get_inactive_user: bool ids_user: str names_user: str emails_user: 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 output_locations: bool @classmethod def get_default(cls, id_user_session): return cls( get_all_location = True , get_inactive_location = False , ids_location = '' , names_location = '' , get_all_user = False , get_inactive_user = False , ids_user = str(id_user_session) , names_user = '' , emails_user = '' , 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 , output_locations = True ) @classmethod def from_json(cls, json): return cls( get_all_location = json.get('a_get_all_location', False) , get_inactive_location = json.get('a_get_inactive_location', False) , ids_location = json.get('a_ids_location', '') , names_location = json.get('a_names_location', '') , get_all_user = json.get('a_get_all_user', False) , get_inactive_user = json.get('a_get_inactive_user', False) , ids_user = json.get('a_ids_user', '') , names_user = json.get('a_names_user', '') , emails_user = json.get('a_emails_user', '') , 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) , output_location_categories = json.get('a_output_location_categories', False) , output_locations = json.get('a_output_locations', False) ) @classmethod def from_form_filters_location(cls, form, id_user_session): av.val_instance(form, 'form', 'Parameters_Location.from_form_filters_location', Filters_Location) 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_Location.from_form_filters_location") filters = cls.get_default(id_user_session) filters.get_all_location = True filters.get_inactive_location = not active_only filters.ids_location = '' filters.names_location = form.search.data if has_filter_search_text else '' return filters def to_json(self): return { 'a_get_all_location': self.get_all_location , 'a_get_inactive_location': self.get_inactive_location , 'a_ids_location': self.ids_location , 'a_names_location': self.names_location , 'a_get_all_user': self.get_all_user , 'a_get_inactive_user': self.get_inactive_user , 'a_ids_user': self.ids_user , 'a_names_user': self.names_user , 'a_emails_user': self.emails_user , '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 }