Files
dog_training/business_objects/dog/lighting_level.py

181 lines
6.9 KiB
Python

"""
Project: PARTS Website
Author: Edward Middleton-Smith
Precision And Research Technology Systems Limited
Technology: Business Objects
Feature: Button Shape Business Object
"""
# internal
from business_objects.base import Base
from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base
from business_objects.dog.image import Image
import lib.argument_validation as av
from extensions import db
# from forms.dog.lighting_level import Filters_Lighting_Level
from helpers.helper_app import Helper_App
# external
from dataclasses import dataclass
from typing import ClassVar
class Lighting_Level(SQLAlchemy_ABC, Base):
ATTR_ID_LIGHTING_LEVEL: ClassVar[str] = 'id_lighting_level'
FLAG_LIGHTING_LEVEL: ClassVar[str] = 'lighting_level'
NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_ID_LIGHTING_LEVEL
NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_NAME
__tablename__ = 'DOG_Lighting_Level'
__table_args__ = { 'extend_existing': True }
id_lighting_level = db.Column(db.Integer, primary_key=True)
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_lighting_level = 0
self.is_new = False
super().__init__()
@classmethod
def from_db_lighting_level(cls, query_row):
_m = f'{cls.__qualname__}.from_db_lighting_level'
lighting_level = cls()
lighting_level.id_lighting_level = query_row[0]
lighting_level.code = query_row[1]
lighting_level.name = query_row[2]
lighting_level.active = av.input_bool(query_row[3], 'active', _m)
return lighting_level
@classmethod
def from_db_assessment(cls, query_row):
_m = f'{cls.__qualname__}.from_db_assessment'
lighting_level = cls()
lighting_level.id_lighting_level = query_row[3]
lighting_level.name = query_row[4]
lighting_level.active = True
return lighting_level
@classmethod
def from_json(cls, json):
_m = f'{cls.__qualname__}.from_json'
lighting_level = cls()
if json is None: return lighting_level
# Helper_App.console_log(f'{_m}\njson: {json}')
lighting_level.id_lighting_level = json.get(Lighting_Level.ATTR_ID_LIGHTING_LEVEL, -1)
lighting_level.name = json[cls.FLAG_NAME]
lighting_level.code = json.get(cls.FLAG_CODE, lighting_level.name.upper().replace(" ", "_"))
lighting_level.active = av.input_bool(json[cls.FLAG_ACTIVE], cls.FLAG_ACTIVE, _m)
lighting_level.created_on = json.get(cls.FLAG_CREATED_ON, None)
# Helper_App.console_log(f'Lighting_Level: {lighting_level}')
return lighting_level
def to_json(self):
as_json = {
**self.get_shared_json_attributes(self)
, self.ATTR_ID_LIGHTING_LEVEL: self.id_lighting_level
, 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_LIGHTING_LEVEL}: {self.id_lighting_level}
{self.FLAG_CODE}: {self.code}
{self.FLAG_NAME}: {self.name}
{self.FLAG_ACTIVE}: {self.active}
{self.FLAG_CREATED_ON}: {self.created_on}
)
'''
class Lighting_Level_Temp(db.Model, Base):
__tablename__ = 'DOG_Lighting_Level_Temp'
__table_args__ = { 'extend_existing': True }
id_temp = db.Column(db.Integer, primary_key=True)
id_lighting_level = 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_lighting_level(cls, lighting_level):
_m = 'Lighting_Level_Temp.from_lighting_level'
temp = cls()
temp.id_lighting_level = lighting_level.id_lighting_level
temp.code = lighting_level.code
temp.name = lighting_level.name
temp.active = lighting_level.active
# temp.created_on = lighting_level.created_on
return temp
def __repr__(self):
return f'''
{self.__class__.__name__}(
{Lighting_Level.FLAG_LIGHTING_LEVEL}: {self.id_lighting_level}
{self.FLAG_CODE}: {self.code}
{self.FLAG_NAME}: {self.name}
{self.FLAG_ACTIVE}: {self.active}
)
'''
class Parameters_Lighting_Level(Get_Many_Parameters_Base):
get_all_lighting_level: bool
get_inactive_lighting_level: bool
ids_lighting_level: str
names_lighting_level: 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_lighting_level = True
, get_inactive_lighting_level = False
, ids_lighting_level = ''
, names_lighting_level = ''
, 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_lighting_level = json.get('a_get_all_lighting_level', False)
, get_inactive_lighting_level = json.get('a_get_inactive_lighting_level', False)
, ids_lighting_level = json.get('a_ids_lighting_level', '')
, names_lighting_level = json.get('a_names_lighting_level', '')
, 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)
)
def to_json(self):
return {
'a_get_all_lighting_level': self.get_all_lighting_level
, 'a_get_inactive_lighting_level': self.get_inactive_lighting_level
, 'a_ids_lighting_level': self.ids_lighting_level
, 'a_names_lighting_level': self.names_lighting_level
, '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
}