Files
dog_training/business_objects/dog/command.py

316 lines
14 KiB
Python

"""
Project: PARTS Website
Author: Edward Middleton-Smith
Precision And Research Technology Systems Limited
Technology: Business Objects
Feature: Command Business Object
"""
# internal
from business_objects.base import Base
from business_objects.dog.command_category import Command_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.command import Filters_Command
from forms.dog.command_category import Filters_Command_Category
from helpers.helper_app import Helper_App
# external
from dataclasses import dataclass
from typing import ClassVar
class Command(SQLAlchemy_ABC, Base):
ATTR_ID_COMMAND: ClassVar[str] = 'id_command'
FLAG_COMMAND: ClassVar[str] = 'command'
FLAG_HAND_SIGNAL_DEFAULT_DESCRIPTION: ClassVar[str] = 'hand-signal-default-description'
FLAG_CAN_HAVE_BUTTON: ClassVar[str] = 'can-have-button'
NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_ID_COMMAND
NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_NAME
__tablename__ = 'DOG_Command'
__table_args__ = { 'extend_existing': True }
id_command = db.Column(db.Integer, primary_key=True)
id_command_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_command = 0
self.command_category = None
self.is_new = False
self.has_button = False
super().__init__()
@classmethod
def from_db_command(cls, query_row):
_m = f'{cls.__qualname__}.from_db_command'
command = cls()
command.id_command = query_row[0]
command.id_command_category = query_row[1]
command.name = query_row[2]
command.hand_signal_default_description = query_row[3]
command.can_have_button = av.input_bool(query_row[4], 'can_have_button', _m)
# command.has_button = av.input_bool(query_row[7], 'has_button', _m)
command.notes = query_row[5]
command.active = av.input_bool(query_row[6], 'active', _m)
# command.created_on = query_row[7]
# command.command_category = Command_Category.from_db_command(query_row)
return command
@classmethod
def from_db_dog_command_link(cls, query_row):
_m = f'{cls.__qualname__}.from_db_dog_command_link'
command = cls()
command.id_command = query_row[5]
command.id_command_category = query_row[3]
command.name = query_row[6]
# command.hand_signal_default_description = query_row[2]
command.can_have_button = av.input_bool(query_row[8], 'can_have_button', _m)
# command.has_button = av.input_bool(query_row[7], 'has_button', _m)
# command.notes = query_row[4]
command.active = True # av.input_bool(True, 'active', _m)
# command.created_on = query_row[7]
command.command_category = Command_Category.from_db_dog_command_link(query_row) # this is done in datastore get many method using category dictionary
return command
@classmethod
def from_db_command_button_link(cls, query_row):
_m = f'{cls.__qualname__}.from_db_command_button_link'
command = cls()
command.id_command = query_row[3]
command.name = query_row[4]
command.active = True
command.command_category = Command_Category.from_db_command_button_link(query_row)
return command
@classmethod
def from_db_assessment_command_modality_link(cls, query_row):
_m = f'{cls.__qualname__}.from_db_assessment_command_modality_link'
command = cls()
command.id_command = query_row[5]
command.name = query_row[6]
command.active = True
command.command_category = Command_Category.from_db_assessment_command_modality_link(query_row)
return command
@classmethod
def from_json(cls, json):
_m = f'{cls.__qualname__}.from_json'
command = cls()
if json is None: return command
# Helper_App.console_log(f'{_m}\njson: {json}')
command.id_command = json.get(Command.ATTR_ID_COMMAND, -1)
command.id_command_category = json[Command_Category.ATTR_ID_COMMAND_CATEGORY]
command.name = json[cls.FLAG_NAME]
command.hand_signal_default_description = json[cls.FLAG_HAND_SIGNAL_DEFAULT_DESCRIPTION]
command.can_have_button = json[cls.FLAG_CAN_HAVE_BUTTON]
command.notes = json[cls.FLAG_NOTES]
command.active = av.input_bool(json[cls.FLAG_ACTIVE], cls.FLAG_ACTIVE, _m)
command.created_on = json.get(cls.FLAG_CREATED_ON, None)
# Helper_App.console_log(f'Command: {command}')
return command
def to_json(self):
as_json = {
**self.get_shared_json_attributes(self)
, self.ATTR_ID_COMMAND: self.id_command
, Command_Category.ATTR_ID_COMMAND_CATEGORY: self.id_command_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_COMMAND}: {self.id_command}
{Command_Category.FLAG_COMMAND_CATEGORY}: {self.id_command_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 Command_Temp(db.Model, Base):
__tablename__ = 'DOG_Command_Temp'
__table_args__ = { 'extend_existing': True }
id_temp = db.Column(db.Integer, primary_key=True)
id_command = db.Column(db.Integer)
id_command_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_command(cls, command):
_m = 'Command_Temp.from_command'
temp = cls()
temp.id_command = command.id_command
temp.id_command_category = command.id_command_category
temp.name = command.name
temp.hand_signal_default_description = command.hand_signal_default_description
temp.can_have_button = command.can_have_button
temp.notes = command.notes
temp.active = command.active
temp.created_on = command.created_on
return temp
class Parameters_Command(Get_Many_Parameters_Base):
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_default_descriptions_command: str
notes_command: 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_command_categories: bool
output_commands: bool
@classmethod
def get_default(cls, id_user_session):
return cls(
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_default_descriptions_command = ''
, notes_command = ''
, 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_command_categories = True
, output_commands = True
)
@classmethod
def from_json(cls, json):
return cls(
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_default_descriptions_command = json.get('a_hand_signal_default_descriptions_command', '')
, notes_command = json.get('a_notes_command', '')
, 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_command_categories = json.get('a_output_command_categories', False)
, output_commands = json.get('a_output_commands', False)
)
@classmethod
def from_form_filters_command(cls, form, id_user_session):
av.val_instance(form, 'form', 'Parameters_Command.from_form_filters_command', Filters_Command)
has_filter_search_text = not (form.search.data == '' or form.search.data is None)
has_filter_command_category = not (has_filter_search_text or form.id_command_category.data == '0' or form.id_command_category.data == '' or form.id_command_category.data is None)
active_only = av.input_bool(form.active_only.data, "active", "Parameters_Command.from_form_filters_command")
filters = cls.get_default(id_user_session)
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 = True
filters.get_inactive_command = not active_only
filters.ids_command = ''
filters.names_command = form.search.data if has_filter_search_text else ''
return filters
@classmethod
def from_form_filters_command_category(cls, form, id_user_session):
av.val_instance(form, 'form', 'Parameters_Command.from_form_filters_command_category', Filters_Command_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_Command.from_form_filters_command")
filters = cls.get_default(id_user_session)
filters.get_all_command_category = True
filters.get_inactive_command_category = not active_only
filters.ids_command_category = ''
filters.names_command_category = form.search.data if has_filter_search_text else ''
filters.get_all_command = False
filters.get_inactive_command = False
filters.ids_command = ''
filters.names_command = ''
filters.require_all_id_search_filters_met = False
filters.output_commands = False
return filters
def to_json(self):
return {
'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_default_descriptions_command': self.hand_signal_default_descriptions_command
, 'a_notes_command': self.notes_command
, '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
, 'a_output_command_categories': self.output_command_categories
, 'a_output_commands': self.output_commands
}