Files
dog_training/business_objects/sql_error.py

112 lines
3.8 KiB
Python

"""
Project: PARTS Website
Author: Edward Middleton-Smith
Precision And Research Technology Systems Limited
Technology: Business Objects
Feature: SQL Error Business Object
Description:
Business object for SQL errors returned by Get Many Stored Procedures
"""
# internal
from business_objects.base import Base
from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base
from extensions import db
from helpers.helper_app import Helper_App
import lib.argument_validation as av
from lib import data_types
# external
from enum import Enum
from datetime import datetime, timedelta
import locale
from flask_sqlalchemy import SQLAlchemy
from typing import ClassVar
# db = SQLAlchemy()
class SQL_Error(SQLAlchemy_ABC, Base):
ATTR_ID_ERROR: ClassVar[str] = 'id_error'
FLAG_IS_BREAKING_ERROR: ClassVar[str] = 'is_breaking_error'
__tablename__ = 'DOG_Temp_SQL_Error'
__table_args__ = { 'extend_existing': True }
id_error = db.Column(db.Integer, primary_key=True)
id_type = db.Column(db.Integer)
code_type = db.Column(db.String(50))
name_type = db.Column(db.String(500))
description_type = db.Column(db.String(4000))
is_breaking_error_type = db.Column(db.Boolean)
background_colour_type = db.Column(db.String(50))
text_colour_type = db.Column(db.String(50))
msg = db.Column(db.String(4000))
def __init__(self):
self.id_error = 0
super().__init__()
@classmethod
def from_db_record(cls, record):
_m = f'{cls.__qualname__}.from_db_record'
Helper_App.console_log(_m)
Helper_App.console_log(f'record: {record}')
error = cls()
error.id_error = record[0]
error.id_type = record[1]
error.code_type = record[2]
error.name_type = record[3]
error.description_type = record[4]
error.is_breaking_error_type = av.input_bool(record[5], 'is_breaking_error_type', _m)
error.background_colour_type = record[6]
error.text_colour_type = record[7]
error.msg = record[8]
return error
@classmethod
def from_json(cls, json):
_m = f'{cls.__qualname__}.from_json'
error = cls()
if json is None: return error
"""
# Helper_App.console_log(f'{_m}\njson: {json}')
error.id_error = json.get(cls.ATTR_ID_ERROR, -1)
error.id_type = json.get()
error.name = json[cls.FLAG_NAME]
error.code = json.get(cls.FLAG_CODE, error.name.upper().replace(" ", "_"))
error.active = av.input_bool(json[cls.FLAG_ACTIVE], cls.FLAG_ACTIVE, _m)
# Helper_App.console_log(f'Error: {error}')
return error
"""
def to_json(self):
return {
self.ATTR_ID_ERROR: self.display_order
, Base.ATTR_ID_MSG_ERROR_TYPE: self.id_type
, Base.FLAG_CODE: self.code_type
, Base.FLAG_NAME: self.name_type
, Base.FLAG_DESCRIPTION: self.description_type
, self.FLAG_IS_BREAKING_ERROR: self.is_breaking_error_type
, Base.FLAG_BACKGROUND_COLOUR: self.background_colour_type
, Base.FLAG_TEXT_COLOUR: self.text_colour_type
, Base.FLAG_MESSAGE: self.msg
}
def __repr__(self):
# Helper_App.console_log(f'{cls.__qualname__}.__repr__')
return f'''
{self.__class__.__name__}(
{self.ATTR_ID_ERROR}: {self.id_error}
{Base.ATTR_ID_MSG_ERROR_TYPE}: {self.id_type}
{Base.FLAG_CODE}: {self.code_type}
{Base.FLAG_NAME}: {self.name_type}
{Base.FLAG_DESCRIPTION}: {self.description_type}
{self.FLAG_IS_BREAKING_ERROR}: {self.is_breaking_error_type}
{Base.FLAG_BACKGROUND_COLOUR}: {self.background_colour_type}
{Base.FLAG_TEXT_COLOUR}: {self.text_colour_type}
{Base.FLAG_MESSAGE}: {self.msg}
)
'''