Files
dog_training/business_objects/dog/assessment.py

345 lines
16 KiB
Python

"""
Project: PARTS Website
Author: Edward Middleton-Smith
Precision And Research Technology Systems Limited
Technology: Business Objects
Feature: Assessment Business Object
"""
# internal
from business_objects.base import Base
from business_objects.dog.command import Command
# from business_objects.dog.command_category import Command_Category
from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base
from business_objects.dog.dog import Dog
from business_objects.dog.lighting_level import Lighting_Level
from business_objects.dog.location import Location
from business_objects.dog.obedience_level import Obedience_Level
from business_objects.dog.weather import Weather
from business_objects.dog.user import User
from extensions import db
from forms.dog.assessment import Filters_Assessment
from helpers.helper_app import Helper_App
import lib.argument_validation as av
# external
from dataclasses import dataclass
from typing import ClassVar, Optional
class Assessment(SQLAlchemy_ABC, Base):
ATTR_ID_ASSESSMENT: ClassVar[str] = Filters_Assessment.ATTR_ID_ASSESSMENT
FLAG_ASSESSMENT: ClassVar[str] = 'assessment'
FLAG_ASSESSMENT_COMMAND_MODALITY_LINK: ClassVar[str] = 'assessment_command_modality_link'
FLAG_DISTRACTION: ClassVar[str] = 'distraction'
FLAG_TEMPERATURE_CELCIUS: ClassVar[str] = 'temperature_celcius'
FLAG_USER_HANDLER: ClassVar[str] = Filters_Assessment.FLAG_USER_HANDLER
NAME_ATTR_OPTION_VALUE: ClassVar[str] = ATTR_ID_ASSESSMENT
NAME_ATTR_OPTION_TEXT: ClassVar[str] = ATTR_ID_ASSESSMENT
__tablename__ = 'DOG_Assessment'
__table_args__ = { 'extend_existing': True }
id_assessment = db.Column(db.Integer, primary_key=True)
id_weather = db.Column(db.Integer)
id_lighting_level = db.Column(db.Integer)
id_location = db.Column(db.Integer)
id_user_handler = db.Column(db.Integer)
notes = db.Column(db.Text)
temperature_celcius = db.Column(db.Numeric(5, 2))
# difficulty_level = db.Column(db.Float)
active = db.Column(db.Boolean)
created_on = db.Column(db.DateTime)
def __init__(self):
self.id_assessment = 0
self.is_new = False
self.weather = None
self.lighting_level = None
self.temperature_celcius = 22
self.location = None
self.user_handler = None
self.distractions = []
self.assessment_command_modality_links = []
super().__init__()
@classmethod
def from_db_assessment(cls, query_row):
_m = f'{cls.__qualname__}.from_db_assessment'
assessment = cls()
assessment.id_assessment = query_row[0]
assessment.id_weather = query_row[1]
assessment.id_lighting_level = query_row[3]
assessment.id_location = query_row[5]
assessment.id_user_handler = query_row[7]
assessment.notes = query_row[10]
assessment.temperature_celcius = query_row[11]
assessment.active = av.input_bool(query_row[12], 'active', _m)
assessment.weather = Weather.from_db_assessment(query_row)
assessment.lighting_level = Lighting_Level.from_db_assessment(query_row)
assessment.location = Location.from_db_assessment(query_row)
assessment.user_handler = User.from_db_assessment(query_row)
return assessment
@classmethod
def from_db_distraction(cls, query_row):
_m = f'{cls.__qualname__}.from_db_distraction'
assessment = cls()
assessment.id_assessment = query_row[1]
return assessment
@classmethod
def from_db_assessment_command_modality_link(cls, query_row):
_m = f'{cls.__qualname__}.from_db_assessment_command_modality_link'
assessment = cls()
assessment.id_assessment = query_row[1]
return assessment
@classmethod
def from_json(cls, json):
_m = f'{cls.__qualname__}.from_json'
assessment = cls()
if json is None: return assessment
assessment.id_assessment = json.get(Assessment.ATTR_ID_ASSESSMENT, -1)
assessment.id_weather = json[Weather.ATTR_ID_WEATHER]
assessment.id_lighting_level = json[Lighting_Level.ATTR_ID_LIGHTING_LEVEL]
assessment.id_location = json[Location.ATTR_ID_LOCATION]
assessment.id_user_handler = json[cls.FLAG_USER_HANDLER]
assessment.notes = json[cls.FLAG_NOTES]
assessment.temperature_celcius = json[cls.FLAG_TEMPERATURE_CELCIUS]
assessment.active = av.input_bool(json[cls.FLAG_ACTIVE], cls.FLAG_ACTIVE, _m)
assessment.created_on = json.get(cls.FLAG_CREATED_ON, None)
return assessment
def to_json(self):
as_json = {
**self.get_shared_json_attributes(self)
, self.ATTR_ID_ASSESSMENT: self.id_assessment
, Weather.ATTR_ID_WEATHER: { Weather.ATTR_ID_WEATHER: None } if self.weather is None else self.weather.to_json()
, Lighting_Level.ATTR_ID_LIGHTING_LEVEL: { Lighting_Level.ATTR_ID_LIGHTING_LEVEL: None } if self.lighting_level is None else self.lighting_level.to_json()
, Location.ATTR_ID_LOCATION: { Location.ATTR_ID_LOCATION: None } if self.weather is None else self.weather.to_json()
, self.FLAG_USER_HANDLER: { User.ATTR_ID_USER: None } if self.user_handler is None else self.user_handler.to_json()
, self.FLAG_NOTES: self.notes
, self.FLAG_TEMPERATURE_CELCIUS: self.temperature_celcius
, self.FLAG_ACTIVE: self.active
, self.FLAG_CREATED_ON: self.created_on
, self.FLAG_DISTRACTION: [distraction.to_json() for distraction in self.distractions] if (self.distractions is not None and len(self.distractions) > 0) else []
, self.FLAG_ASSESSMENT_COMMAND_MODALITY_LINK: [link.to_json() for link in self.assessment_command_modality_links] if (self.assessment_command_modality_links is not None and len(self.assessment_command_modality_links) > 0) else []
}
return as_json
def __repr__(self):
return f'''
{self.__class__.__name__}(
{self.FLAG_ASSESSMENT}: {self.id_assessment}
{Weather.FLAG_WEATHER}: {self.weather}
{Lighting_Level.FLAG_LIGHTING_LEVEL}: {self.lighting_level}
{Location.FLAG_LOCATION}: {self.location}
{self.FLAG_USER_HANDLER}: {self.user_handler}
{self.FLAG_NOTES}: {self.notes}
{self.FLAG_TEMPERATURE_CELCIUS}: {self.temperature_celcius}
{self.FLAG_ACTIVE}: {self.active}
{self.FLAG_CREATED_ON}: {self.created_on}
)
'''
# {Command_Category.FLAG_COMMAND_CATEGORY}: {self.id_command_category}
class Assessment_Temp(db.Model, Base):
__tablename__ = 'DOG_Assessment_Temp'
__table_args__ = { 'extend_existing': True }
id_temp = db.Column(db.Integer, primary_key=True)
id_assessment = db.Column(db.Integer)
id_weather = db.Column(db.Integer)
id_lighting_level = db.Column(db.Integer)
id_location = db.Column(db.Integer)
id_user_handler = db.Column(db.Integer)
notes = db.Column(db.Text)
temperature_celcius = db.Column(db.Numeric(5, 2))
# difficulty_level = db.Column(db.Float)
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_assessment(cls, assessment):
_m = 'Assessment_Temp.from_assessment'
temp = cls()
temp.id_assessment = assessment.id_assessment
temp.id_weather = assessment.id_weather
temp.id_lighting_level = assessment.id_lighting_level
temp.id_location = assessment.id_location
temp.id_user_handler = assessment.id_user_handler
temp.notes = assessment.notes
temp.temperature_celcius = assessment.temperature_celcius
temp.active = assessment.active
# temp.created_on = assessment.created_on
return temp
class Parameters_Assessment(Get_Many_Parameters_Base):
get_all_assessment: bool
get_inactive_assessment: bool
ids_assessment: str
notes_assessment: str
min_temperature_assessment: Optional[float]
max_temperature_assessment: Optional[float]
get_all_weather: bool
get_inactive_weather: bool
ids_weather: str
names_weather: str
get_all_lighting_level: bool
get_inactive_lighting_level: bool
ids_lighting_level: str
names_lighting_level: str
get_all_location: bool
get_inactive_location: bool
ids_location: str
names_location: str
get_all_user_handler: bool
get_inactive_user_handler: bool
ids_user_handler: str
#auth0_ids_user_handler: str
names_user_handler: str
emails_user_handler: 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
@classmethod
def get_default(cls):
return cls(
get_all_assessment = True
, get_inactive_assessment = False
, ids_assessment = ''
, notes_assessment = ''
, min_temperature_assessment = None
, max_temperature_assessment = None
, get_all_weather = True
, get_inactive_weather = False
, ids_weather = ''
, names_weather = ''
, get_all_lighting_level = True
, get_inactive_lighting_level = False
, ids_lighting_level = ''
, names_lighting_level = ''
, get_all_location = True
, get_inactive_location = False
, ids_location = ''
, names_location = ''
, get_all_user_handler = True
, get_inactive_user_handler = False
, ids_user_handler = ''
# , auth0_ids_user_handler = ''
, names_user_handler = ''
, emails_user_handler = ''
, 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
)
@classmethod
def from_json(cls, json):
return cls(
get_all_assessment = json.get('a_get_all_assessment', False)
, get_inactive_assessment = json.get('a_get_inactive_assessment', False)
, ids_assessment = json.get('a_ids_assessment', '')
, notes_assessment = json.get('a_notes_assessment', '')
, min_temperature_assessment = json.get('a_min_temperature_assessment', None)
, max_temperature_assessment = json.get('a_max_temperature_assessment', None)
, get_all_weather = json.get('a_get_all_weather', False)
, get_inactive_weather = json.get('a_get_inactive_weather', False)
, ids_weather = json.get('a_ids_weather', '')
, names_weather = json.get('a_names_weather', '')
, get_all_lighting_level = json.get('a_get_all_lighting_level', False)
, get_inactive_lighting_level = json.get('a_get_inactive_lighting_level', False)
, ids_lighting_level = json.get('a_ids_lighting_level', '')
, names_lighting_level = json.get('a_names_lighting_level', '')
, get_all_location = json.get('a_get_all_location', False)
, get_inactive_location = json.get('a_get_inactive_location', False)
, ids_location = json.get('a_ids_location', '')
, names_location = json.get('a_names_location', '')
, get_all_user_handler = json.get('a_get_all_user_handler', False)
, get_inactive_user_handler = json.get('a_get_inactive_user_handler', False)
, ids_user_handler = json.get('a_ids_user_handler', '')
# , auth0_ids_user_handler = json.get('a_ids_user_handler', '')
, names_user_handler = json.get('a_names_user_handler', '')
, emails_user_handler = json.get('a_emails_user_handler', '')
, 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)
)
@classmethod
def from_form_filters_assessment(cls, form):
_m = f'{cls.__qualname__}.from_form_filters_assessment'
av.val_instance(form, 'form', _m, Filters_Assessment)
has_filter_search_text = not (form.search.data == '' or form.search.data is None)
has_filter_weather = not (form.id_weather.data == '0' or form.id_weather.data == '' or form.id_weather.data is None)
has_filter_lighting_level = not (form.id_lighting_level.data == '0' or form.id_lighting_level.data == '' or form.id_lighting_level.data is None)
has_filter_location = not (form.id_location.data == '0' or form.id_location.data == '' or form.id_location.data is None)
has_filter_user_handler = not (form.id_user_handler.data == '0' or form.id_user_handler.data == '' or form.id_user_handler.data is None)
active_only = av.input_bool(form.active_only.data, "active", _m)
filters = cls.get_default()
filters.get_all_assessment = True
filters.get_inactive_assessment = not active_only
filters.ids_assessment = ''
filters.get_all_weather = not has_filter_weather
filters.get_inactive_weather = not active_only
filters.ids_weather = form.id_weather.data if has_filter_weather else ''
filters.names_weather = form.search.data if has_filter_search_text else ''
filters.get_all_lighting_level = not has_filter_lighting_level
filters.get_inactive_lighting_level = not active_only
filters.ids_lighting_level = form.id_lighting_level.data if has_filter_lighting_level else ''
filters.names_lighting_level = form.search.data if has_filter_search_text else ''
filters.get_all_location = not has_filter_location
filters.get_inactive_location = not active_only
filters.ids_location = form.id_location.data if has_filter_location else ''
filters.names_location = form.search.data if has_filter_search_text else ''
filters.get_all_user_handler = not has_filter_user_handler
filters.get_inactive_user_handler = not active_only
filters.ids_user_handler = form.id_user_handler.data if has_filter_user_handler else ''
# filters.auth0_ids_user_handler = form.id_user_handler.data if has_filter_user_handler else ''
filters.names_user_handler = form.search.data if has_filter_search_text else ''
filters.emails_user_handler = form.search.data if has_filter_search_text else ''
return filters
def to_json(self):
return {
'a_get_all_assessment': self.get_all_assessment
, 'a_get_inactive_assessment': self.get_inactive_assessment
, 'a_ids_assessment': self.ids_assessment
, 'a_notes_assessment': self.notes_assessment
, 'a_min_temperature_assessment': self.min_temperature_assessment
, 'a_max_temperature_assessment': self.max_temperature_assessment
, 'get_all_weather': self.get_all_weather
, 'get_inactive_weather': self.get_inactive_weather
, 'ids_weather': self.ids_weather
, 'names_weather': self.names_weather
, 'get_all_lighting_level': self.get_all_lighting_level
, 'get_inactive_lighting_level': self.get_inactive_lighting_level
, 'ids_lighting_level': self.ids_lighting_level
, 'names_lighting_level': self.names_lighting_level
, 'get_all_location': self.get_all_location
, 'get_inactive_location': self.get_inactive_location
, 'ids_location': self.ids_location
, 'names_location': self.names_location
, 'get_all_user_handler': self.get_all_user_handler
, 'get_inactive_user_handler': self.get_inactive_user_handler
, 'ids_user_handler': self.ids_user_handler
#, 'auth0_ids_user_handler': self.ids_user_handler
, 'names_user_handler': self.names_user_handler
, 'emails_user_handler': self.emails_user_handler
, '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
}