""" Project: PARTS Website Author: Edward Middleton-Smith Precision And Research Technology Systems Limited Technology: Business Objects Feature: Command Business Object """ # internal from dog_training.business_objects.base import Base from dog_training.business_objects.dog.command_category import Command_Category from dog_training.business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base import dog_training.lib.argument_validation as av from dog_training.extensions import db from dog_training.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] = FLAG_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.is_new = False self.has_button = False super().__init__() def from_db_command(query_row): _m = 'Command.from_db_command' command = Command() 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] return command def from_db_dog_command_link(query_row): _m = 'Command.from_db_dog_command_link' command = Command() 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[5], 'can_have_button', _m) command.has_button = av.input_bool(query_row[7], 'has_button', _m) # command.notes = query_row[4] command.active = av.input_bool(True, 'active', _m) # command.created_on = query_row[7] return command @classmethod def from_json(cls, json): _m = 'Command.from_json' command = cls() if json is None: return Command Helper_App.console_log(f'{_m}\njson: {json}') command.id_command = -1 command.id_command_category = json[Command_Category.FLAG_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 = json[cls.FLAG_ACTIVE] 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.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 } 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: bool get_inactive_command: bool ids_command: str names_command: str @classmethod def get_default(cls): return cls( get_all_command = True , get_inactive_command = False , ids_command = '' , names_command = '' ) @classmethod def from_json(cls, json): return cls( 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', '') ) """ @classmethod def from_form_filters_command(cls, form): av.val_instance(form, 'form', 'Parameters_Command.from_form_filters_command', Filters_Command) 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.data, "active", "Parameters_Command.from_form_filters_command") return cls( get_all_command = not has_filter_command , get_inactive_command = not active_only , ids_command = form.id_command.data if has_filter_id else '' , names_command = form.name_command.data if has_filter_name else '' ) """ def to_json(self): return { '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 }