287 lines
13 KiB
Python
287 lines
13 KiB
Python
"""
|
|
Project: PARTS Website
|
|
Author: Edward Middleton-Smith
|
|
Precision And Research Technology Systems Limited
|
|
|
|
Technology: Business Objects
|
|
Feature: Dog Command Link Business Object
|
|
"""
|
|
|
|
# internal
|
|
from business_objects.base import Base
|
|
from business_objects.dog.command import Command
|
|
# from business_objects.dog.command_category import Command_Category
|
|
from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base
|
|
from business_objects.dog.dog import Dog
|
|
from business_objects.dog.obedience_level import Obedience_Level
|
|
from extensions import db
|
|
from forms.dog.dog_command_link import Filters_Dog_Command_Link
|
|
from helpers.helper_app import Helper_App
|
|
import lib.argument_validation as av
|
|
# external
|
|
from dataclasses import dataclass
|
|
from typing import ClassVar
|
|
|
|
|
|
class Dog_Command_Link(SQLAlchemy_ABC, Base):
|
|
ATTR_ID_DOG_COMMAND_LINK: ClassVar[str] = 'id_link'
|
|
FLAG_DOG_COMMAND_LINK: ClassVar[str] = 'dog_command_link'
|
|
FLAG_HAND_SIGNAL_DESCRIPTION: ClassVar[str] = 'hand-signal-description'
|
|
NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_ID_DOG_COMMAND_LINK
|
|
NAME_ATTR_OPTION_TEXT: ClassVar[str] = FLAG_HAND_SIGNAL_DESCRIPTION
|
|
|
|
__tablename__ = 'DOG_Dog_Command_Link'
|
|
__table_args__ = { 'extend_existing': True }
|
|
|
|
id_link = db.Column(db.Integer, primary_key=True)
|
|
id_dog = db.Column(db.Integer)
|
|
id_command = db.Column(db.Integer)
|
|
hand_signal_description = db.Column(db.Text)
|
|
notes = db.Column(db.Text)
|
|
active = db.Column(db.Boolean)
|
|
created_on = db.Column(db.DateTime)
|
|
|
|
def __init__(self):
|
|
self.id_link = 0
|
|
self.is_new = False
|
|
self.dog = None
|
|
self.command = None
|
|
super().__init__()
|
|
|
|
@classmethod
|
|
def from_db_dog_command_link(cls, query_row):
|
|
_m = 'Dog_Command_Link.from_db_dog_command_link'
|
|
dog_command_link = cls()
|
|
dog_command_link.id_link = query_row[0]
|
|
dog_command_link.id_dog = query_row[1]
|
|
#dog_command_link.id_command_category = query_row[3]
|
|
dog_command_link.id_command = query_row[5]
|
|
dog_command_link.hand_signal_description = query_row[7]
|
|
dog_command_link.notes = query_row[9]
|
|
dog_command_link.active = av.input_bool(query_row[10], 'active', _m)
|
|
# dog_command_link.created_on = query_row[7]
|
|
dog_command_link.dog = Dog.from_db_dog_command_link(query_row)
|
|
dog_command_link.command = Command.from_db_dog_command_link(query_row)
|
|
return dog_command_link
|
|
|
|
@classmethod
|
|
def from_json(cls, json):
|
|
_m = 'Dog_Command_Link.from_json'
|
|
dog_command_link = cls()
|
|
if json is None: return dog_command_link
|
|
# Helper_App.console_log(f'{_m}\njson: {json}')
|
|
dog_command_link.id_link = -1
|
|
dog_command_link.id_dog = json[Dog.ATTR_ID_DOG]
|
|
dog_command_link.id_command = json[Command.ATTR_ID_COMMAND]
|
|
dog_command_link.hand_signal_description = json[cls.FLAG_HAND_SIGNAL_DESCRIPTION]
|
|
dog_command_link.notes = json[cls.FLAG_NOTES]
|
|
dog_command_link.active = json[cls.FLAG_ACTIVE]
|
|
dog_command_link.created_on = json.get(cls.FLAG_CREATED_ON, None)
|
|
# dog_command_link.id_command_category = json[Command_Category.FLAG_COMMAND_CATEGORY]
|
|
# Helper_App.console_log(f'Dog Command Link: {dog_command_link}')
|
|
return dog_command_link
|
|
|
|
def to_json(self):
|
|
as_json = {
|
|
**self.get_shared_json_attributes(self)
|
|
, self.ATTR_ID_DOG_COMMAND_LINK: self.id_link
|
|
, Dog.FLAG_DOG: self.id_dog
|
|
, Command.FLAG_COMMAND: self.id_command
|
|
, self.FLAG_HAND_SIGNAL_DESCRIPTION: self.hand_signal_description
|
|
, self.FLAG_NOTES: self.notes
|
|
, self.FLAG_ACTIVE: self.active
|
|
, self.FLAG_CREATED_ON: self.created_on
|
|
}
|
|
# , Command_Category.FLAG_COMMAND_CATEGORY: self.id_command_category
|
|
# Helper_App.console_log(f'as_json: {as_json}')
|
|
return as_json
|
|
|
|
def __repr__(self):
|
|
return f'''
|
|
{self.__class__.__name__}(
|
|
{self.FLAG_DOG_COMMAND_LINK}: {self.id_link}
|
|
{Dog.FLAG_DOG}: {self.dog}
|
|
{Command.FLAG_COMMAND}: {self.command}
|
|
{self.FLAG_HAND_SIGNAL_DESCRIPTION}: {self.hand_signal_description}
|
|
{self.FLAG_NOTES}: {self.notes}
|
|
{self.FLAG_ACTIVE}: {self.active}
|
|
{self.FLAG_CREATED_ON}: {self.created_on}
|
|
)
|
|
'''
|
|
# {Command_Category.FLAG_COMMAND_CATEGORY}: {self.id_command_category}
|
|
|
|
class Dog_Command_Link_Temp(db.Model, Base):
|
|
__tablename__ = 'DOG_Dog_Command_Link_Temp'
|
|
__table_args__ = { 'extend_existing': True }
|
|
id_temp = db.Column(db.Integer, primary_key=True)
|
|
id_link = db.Column(db.Integer)
|
|
id_dog = db.Column(db.Integer)
|
|
id_command = db.Column(db.Integer)
|
|
hand_signal_description = db.Column(db.Text)
|
|
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_dog_command_link(cls, dog_command_link):
|
|
_m = 'Dog_Command_Link_Temp.from_dog_command_link'
|
|
temp = cls()
|
|
temp.id_link = dog_command_link.id_link
|
|
temp.id_dog = dog_command_link.id_dog
|
|
temp.id_command = dog_command_link.id_command
|
|
temp.hand_signal_description = dog_command_link.hand_signal_description
|
|
temp.notes = dog_command_link.notes
|
|
temp.active = dog_command_link.active
|
|
# temp.created_on = dog_command_link.created_on
|
|
return temp
|
|
|
|
|
|
class Parameters_Dog_Command_Link(Get_Many_Parameters_Base):
|
|
get_all_link: bool
|
|
get_inactive_link: bool
|
|
ids_link: str
|
|
get_all_dog: bool
|
|
get_inactive_dog: bool
|
|
ids_dog: str
|
|
names_dog: str
|
|
get_all_command_category: bool
|
|
get_inactive_command_category: bool
|
|
ids_command_category: str
|
|
names_command_category: str
|
|
get_all_command: bool
|
|
get_inactive_command: bool
|
|
ids_command: str
|
|
names_command: str
|
|
hand_signal_descriptions_link: str
|
|
notes_command: str
|
|
notes_link: 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_link = True
|
|
, get_inactive_link = False
|
|
, ids_link = ''
|
|
, get_all_dog = True
|
|
, get_inactive_dog = False
|
|
, ids_dog = ''
|
|
, names_dog = ''
|
|
, get_all_command_category = True
|
|
, get_inactive_command_category = False
|
|
, ids_command_category = ''
|
|
, names_command_category = ''
|
|
, get_all_command = True
|
|
, get_inactive_command = False
|
|
, ids_command = ''
|
|
, names_command = ''
|
|
, hand_signal_descriptions_link = ''
|
|
, notes_command = ''
|
|
, notes_link = ''
|
|
, 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_link = json.get('a_get_all_link', False)
|
|
, get_inactive_link = json.get('a_get_inactive_link', False)
|
|
, ids_link = json.get('a_ids_link', '')
|
|
, 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_command_category = json.get('a_get_all_command_category', False)
|
|
, get_inactive_command_category = json.get('a_get_inactive_command_category', False)
|
|
, ids_command_category = json.get('a_ids_command_category', '')
|
|
, names_command_category = json.get('a_names_command_category', '')
|
|
, get_all_command = json.get('a_get_all_command', False)
|
|
, get_inactive_command = json.get('a_get_inactive_command', False)
|
|
, ids_command = json.get('a_ids_command', '')
|
|
, names_command = json.get('a_names_command', '')
|
|
, hand_signal_descriptions_link = json.get('a_hand_signal_descriptions_link', '')
|
|
, notes_command = json.get('a_notes_command', '')
|
|
, notes_link = json.get('a_notes_link', '')
|
|
, 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_dog_command_link(cls, form):
|
|
_m = f'{cls.__qualname__}.from_form_filters_dog_command_link'
|
|
Helper_App.console_log(_m)
|
|
Helper_App.console_log(f'Filters: {form}')
|
|
av.val_instance(form, 'form', _m, Filters_Dog_Command_Link)
|
|
has_filter_search_text = not (form.search.data == '' or form.search.data is None)
|
|
has_filter_dog = not (form.id_dog.data == '0' or form.id_dog.data == '' or form.id_dog.data is None)
|
|
has_filter_command_category = not (form.id_command_category.data == '0' or form.id_command_category.data == '' or form.id_command_category.data is None)
|
|
has_filter_command = not (form.id_command.data == '0' or form.id_command.data == '' or form.id_command.data is None)
|
|
active_only = av.input_bool(form.active_only.data, "active", _m)
|
|
Helper_App.console_log(f'''
|
|
has_filter_search_text: {has_filter_search_text}
|
|
has_filter_dog: {has_filter_dog}
|
|
has_filter_command_category: {has_filter_command_category}
|
|
has_filter_command: {has_filter_command}
|
|
active_only: {active_only}
|
|
''')
|
|
filters = cls.get_default()
|
|
filters.get_all_link = True
|
|
filters.get_inactive_link = not active_only
|
|
filters.ids_link = ''
|
|
filters.get_all_dog = not has_filter_dog
|
|
filters.get_inactive_dog = not active_only
|
|
filters.ids_dog = form.id_dog.data if has_filter_dog else ''
|
|
filters.names_dog = form.search.data if has_filter_search_text else ''
|
|
filters.get_all_command_category = not has_filter_command_category
|
|
filters.get_inactive_command_category = not active_only
|
|
filters.ids_command_category = form.id_command_category.data if has_filter_command_category else ''
|
|
filters.names_command_category = form.search.data if has_filter_search_text else ''
|
|
filters.get_all_command = not has_filter_command
|
|
filters.get_inactive_command = not active_only
|
|
filters.ids_command = form.id_command.data if has_filter_command else ''
|
|
filters.names_command = form.search.data if has_filter_search_text else ''
|
|
filters.hand_signal_descriptions_link = form.search.data if has_filter_search_text else ''
|
|
# filters.notes_command = form.search.data if has_filter_search_text else ''
|
|
filters.notes_link = form.search.data if has_filter_search_text else ''
|
|
return filters
|
|
|
|
def to_json(self):
|
|
return {
|
|
'a_get_all_link': self.get_all_link
|
|
, 'a_get_inactive_link': self.get_inactive_link
|
|
, 'a_ids_link': self.ids_link
|
|
, 'a_get_all_dog': self.get_all_dog
|
|
, 'a_get_inactive_dog': self.get_inactive_dog
|
|
, 'a_ids_dog': self.ids_dog
|
|
, 'a_names_dog': self.names_dog
|
|
, 'a_get_all_command_category': self.get_all_command_category
|
|
, 'a_get_inactive_command_category': self.get_inactive_command_category
|
|
, 'a_ids_command_category': self.ids_command_category
|
|
, 'a_names_command_category': self.names_command_category
|
|
, 'a_get_all_command': self.get_all_command
|
|
, 'a_get_inactive_command': self.get_inactive_command
|
|
, 'a_ids_command': self.ids_command
|
|
, 'a_names_command': self.names_command
|
|
, 'a_hand_signal_descriptions_link': self.hand_signal_descriptions_link
|
|
, 'a_notes_command': self.notes_command
|
|
, 'a_notes_link': self.notes_link
|
|
, '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
|
|
}
|
|
|
|
|