Feat(SQL): Location Get Many and Calc Stored Procedures created with logic using Location Link table.

This commit is contained in:
2025-07-15 15:07:40 +01:00
parent 7573f329e9
commit a3fd8b12fe
44 changed files with 2590 additions and 873 deletions

View File

@@ -0,0 +1,274 @@
"""
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.dog.location_category import Location_Category
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 forms.dog.location_category import Filters_Location_Category
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_category = db.Column(db.Integer)
name = db.Column(db.String(250))
hand_signal_default_description = db.Column(db.Text)
can_have_button = db.Column(db.Boolean)
notes = db.Column(db.Text)
active = db.Column(db.Boolean)
created_on = db.Column(db.DateTime)
def __init__(self):
self.id_location = 0
self.location_category = None
self.is_new = False
self.has_button = 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_category = query_row[1]
location.name = query_row[2]
location.hand_signal_default_description = query_row[3]
location.can_have_button = av.input_bool(query_row[4], 'can_have_button', _m)
# location.has_button = av.input_bool(query_row[7], 'has_button', _m)
location.notes = query_row[5]
location.active = av.input_bool(query_row[6], 'active', _m)
# location.created_on = query_row[7]
# location.location_category = Location_Category.from_db_location(query_row)
return location
@classmethod
def from_db_dog_location_link(cls, query_row):
_m = f'{cls.__qualname__}.from_db_dog_location_link'
location = cls()
location.id_location = query_row[5]
location.id_location_category = query_row[3]
location.name = query_row[6]
# location.hand_signal_default_description = query_row[2]
location.can_have_button = av.input_bool(query_row[8], 'can_have_button', _m)
# location.has_button = av.input_bool(query_row[7], 'has_button', _m)
# location.notes = query_row[4]
location.active = True # av.input_bool(True, 'active', _m)
# location.created_on = query_row[7]
location.location_category = Location_Category.from_db_dog_location_link(query_row) # this is done in datastore get many method using category dictionary
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 = -1
location.id_location_category = json[Location_Category.ATTR_ID_LOCATION_CATEGORY]
location.name = json[cls.FLAG_NAME]
location.hand_signal_default_description = json[cls.FLAG_HAND_SIGNAL_DEFAULT_DESCRIPTION]
location.can_have_button = json[cls.FLAG_CAN_HAVE_BUTTON]
location.notes = json[cls.FLAG_NOTES]
location.active = json[cls.FLAG_ACTIVE]
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_Category.ATTR_ID_LOCATION_CATEGORY: self.id_location_category
, self.FLAG_NAME: self.name
, self.FLAG_HAND_SIGNAL_DEFAULT_DESCRIPTION: self.hand_signal_default_description
, self.FLAG_CAN_HAVE_BUTTON: self.can_have_button
, self.FLAG_NOTES: self.notes
, 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_Category.FLAG_LOCATION_CATEGORY}: {self.id_location_category}
{self.FLAG_NAME}: {self.name}
{self.FLAG_HAND_SIGNAL_DEFAULT_DESCRIPTION}: {self.hand_signal_default_description}
{self.FLAG_CAN_HAVE_BUTTON}: {self.can_have_button}
{self.FLAG_NOTES}: {self.notes}
{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_category = db.Column(db.Integer)
name = db.Column(db.String(250))
hand_signal_default_description = db.Column(db.Text)
can_have_button = db.Column(db.Boolean)
notes = db.Column(db.Text)
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_category = location.id_location_category
temp.name = location.name
temp.hand_signal_default_description = location.hand_signal_default_description
temp.can_have_button = location.can_have_button
temp.notes = location.notes
temp.active = location.active
temp.created_on = location.created_on
return temp
class Parameters_Location(Get_Many_Parameters_Base):
get_all_location_category: bool
get_inactive_location_category: bool
ids_location_category: str
names_location_category: str
get_all_location: bool
get_inactive_location: bool
ids_location: str
names_location: str
hand_signal_default_descriptions_location: str
notes_location: 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_location_categories: bool
output_locations: bool
@classmethod
def get_default(cls):
return cls(
get_all_location_category = True
, get_inactive_location_category = False
, ids_location_category = ''
, names_location_category = ''
, get_all_location = True
, get_inactive_location = False
, ids_location = ''
, names_location = ''
, hand_signal_default_descriptions_location = ''
, notes_location = ''
, 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_location_categories = True
, output_locations = True
)
@classmethod
def from_json(cls, json):
return cls(
get_all_location_category = json.get('a_get_all_location_category', False)
, get_inactive_location_category = json.get('a_get_inactive_location_category', False)
, ids_location_category = json.get('a_ids_location_category', '')
, names_location_category = json.get('a_names_location_category', '')
, 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', '')
, hand_signal_default_descriptions_location = json.get('a_hand_signal_default_descriptions_location', '')
, notes_location = json.get('a_notes_location', '')
, 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):
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)
has_filter_location_category = not (has_filter_search_text or form.id_location_category.data == '0' or form.id_location_category.data == '' or form.id_location_category.data is None)
active_only = av.input_bool(form.active_only.data, "active", "Parameters_Location.from_form_filters_location")
filters = cls.get_default()
filters.get_all_location_category = not has_filter_location_category
filters.get_inactive_location_category = not active_only
filters.ids_location_category = form.id_location_category.data if has_filter_location_category else ''
filters.names_location_category = form.search.data if has_filter_search_text else ''
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
@classmethod
def from_form_filters_location_category(cls, form):
av.val_instance(form, 'form', 'Parameters_Location.from_form_filters_location_category', Filters_Location_Category)
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()
filters.get_all_location_category = True
filters.get_inactive_location_category = not active_only
filters.ids_location_category = ''
filters.names_location_category = form.search.data if has_filter_search_text else ''
filters.get_all_location = False
filters.get_inactive_location = False
filters.ids_location = ''
filters.names_location = ''
filters.require_all_id_search_filters_met = False
filters.output_locations = False
return filters
def to_json(self):
return {
'a_get_all_location_category': self.get_all_location_category
, 'a_get_inactive_location_category': self.get_inactive_location_category
, 'a_ids_location_category': self.ids_location_category
, 'a_names_location_category': self.names_location_category
, '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_hand_signal_default_descriptions_location': self.hand_signal_default_descriptions_location
, 'a_notes_location': self.notes_location
, '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_location_categories': self.output_location_categories
, 'a_output_locations': self.output_locations
}