""" 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.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_ALTCHA: ClassVar[str] = 'altcha' FLAG_COMMAND: ClassVar[str] = 'command' FLAG_NAME_COMPANY: ClassVar[str] = 'name-company' FLAG_NAME_CONTACT: ClassVar[str] = 'name-contact' FLAG_MESSAGE: ClassVar[str] = 'message' FLAG_RECEIVE_MARKETING_COMMUNICATIONS: ClassVar[str] = 'receive-marketing-communications' NAME_ATTR_OPTION_VALUE: ClassVar[str] = FLAG_COMMAND NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_EMAIL __tablename__ = 'PH_Command' __table_args__ = { 'extend_existing': True } id_command = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(250)) name_contact = db.Column(db.String(250)) name_company = db.Column(db.String(250)) message = db.Column(db.Text) receive_marketing_communications = db.Column(db.Boolean) active = db.Column(db.Boolean) created_on = db.Column(db.DateTime) def __init__(self): self.id_command = 0 self.is_new = False super().__init__() def from_DB_Command(query_row): _m = 'Command.from_DB_Command' command = Command() command.id_command = query_row[0] command.email = query_row[1] command.name_contact = query_row[2] command.name_company = query_row[3] command.message = query_row[4] command.receive_marketing_communications = av.input_bool(query_row[5], 'receive_marketing_communications', _m) command.active = av.input_bool(query_row[6], '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.email = json[cls.FLAG_EMAIL] command.name_contact = json[cls.FLAG_NAME_CONTACT] command.name_company = json[cls.FLAG_NAME_COMPANY] command.message = json[cls.FLAG_MESSAGE] command.receive_marketing_communications = json[cls.FLAG_RECEIVE_MARKETING_COMMUNICATIONS] 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 , self.FLAG_EMAIL: self.email , self.FLAG_NAME_CONTACT: self.name_contact , self.FLAG_NAME_COMPANY: self.name_company , self.FLAG_MESSAGE: self.message , self.FLAG_RECEIVE_MARKETING_COMMUNICATIONS: self.receive_marketing_communications , 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} {self.FLAG_EMAIL}: {self.email} {self.FLAG_NAME_CONTACT}: {self.name_contact} {self.FLAG_NAME_COMPANY}: {self.name_company} {self.FLAG_MESSAGE}: {self.message} {self.FLAG_RECEIVE_MARKETING_COMMUNICATIONS}: {self.receive_marketing_communications} {self.FLAG_ACTIVE}: {self.active} {self.FLAG_CREATED_ON}: {self.created_on} ) ''' class Command_Temp(db.Model, Base): __tablename__ = 'PH_Command_Temp' __table_args__ = { 'extend_existing': True } id_temp = db.Column(db.Integer, primary_key=True) id_command = db.Column(db.Integer) email = db.Column(db.String(250)) name_contact = db.Column(db.String(250)) name_company = db.Column(db.String(250)) message = db.Column(db.Text) receive_marketing_communications = db.Column(db.Boolean) 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.email = command.email temp.name_contact = command.name_contact temp.name_company = command.name_company temp.message = command.message temp.receive_marketing_communications = command.receive_marketing_communications temp.active = command.active temp.created_on = command.created_on return temp