""" 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 import lib.argument_validation as av from extensions import db from helpers.helper_app import Helper_App # external from dataclasses import dataclass from typing import ClassVar class Command(SQLAlchemy_ABC, Base): 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_Dog_Command(query_row): _m = 'Command.from_DB_Dog_Command' command = Command() command.id_command = query_row[5] command.id_command_category = query_row[3] command.name = query_row[7] # 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