Home Information - Your home, your data, organized.
Python
Improlved settings manager while adding alert email setting.
Anthony Cassandra6e8023c96d12
diff --git a/src/hi/apps/alert/enums.py b/src/hi/apps/alert/enums.pyindex 3d6de0bd..765a7da9 100644--- a/src/hi/apps/alert/enums.py+++ b/src/hi/apps/alert/enums.py@@ -31,7 +31,7 @@ class SecurityPosture(LabeledEnum):ERROR = ( 'Error' , '' )-class SecurityState(LabeledEnum):+class AlertState(LabeledEnum):ERROR = ( 'Error' , '' , -12 )UNKNOWN = ( 'Unknown' , '' , -3 )@@ -54,7 +54,7 @@ class SecurityState(LabeledEnum):return@staticmethod- def get_recent_variant( cls, security_state : 'SecurityState' ):+ def get_recent_variant( cls, security_state : 'AlertState' ):"""Converts a state to its 'recent' version if one exists.Otherwise, just returns the state passed in.diff --git a/src/hi/apps/config/enums.py b/src/hi/apps/config/enums.pyindex 4dcb8791..2865021b 100644--- a/src/hi/apps/config/enums.py+++ b/src/hi/apps/config/enums.py@@ -28,7 +28,8 @@ class ConfigPageType(LabeledEnum):class SubsystemType(LabeledEnum):LOCALE = ('Locale' , '' )- DISPLAY = ('Display' , '' )+ DISPLAY = ('Display' , '' )+ ALERTS = ('Alerts' , '' )class Theme(LabeledEnum):@@ -58,6 +59,16 @@ class SubsystemAttributeType(LabeledEnum):True,str( Theme.default() ),)+ AWAY_ALERT_EMAILS = (+ 'Away Alert Emails',+ 'Email addresses to send critical alerts to when away.',+ SubsystemType.ALERTS,+ AttributeValueType.TEXT,+ '',+ True,+ False,+ '',+ )def __init__( self,label : str,diff --git a/src/hi/apps/config/migrations/0005_subsystemattribute_subsystem_attribute_type_str.py b/src/hi/apps/config/migrations/0005_subsystemattribute_subsystem_attribute_type_str.pynew file mode 100644index 00000000..269e5dab--- /dev/null+++ b/src/hi/apps/config/migrations/0005_subsystemattribute_subsystem_attribute_type_str.py@@ -0,0 +1,21 @@+# Generated by Django 4.2.15 on 2024-12-06 21:51++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("config", "0004_rename_value_range_subsystemattribute_value_range_str"),+ ]++ operations = [+ migrations.AddField(+ model_name="subsystemattribute",+ name="subsystem_attribute_type_str",+ field=models.CharField(+ default="foo", max_length=32, verbose_name="Subsystem Attribute Type"+ ),+ preserve_default=False,+ ),+ ]diff --git a/src/hi/apps/config/models.py b/src/hi/apps/config/models.pyindex 083d0e53..9dc279c9 100644--- a/src/hi/apps/config/models.py+++ b/src/hi/apps/config/models.py@@ -2,7 +2,7 @@ from django.db import modelsfrom hi.apps.attribute.models import AttributeModel-from .enums import SubsystemType+from .enums import SubsystemAttributeType, SubsystemTypeclass Subsystem( models.Model ):@@ -49,11 +49,24 @@ class SubsystemAttribute( AttributeModel ):verbose_name = 'Subsystem',on_delete = models.CASCADE,)-+ subsystem_attribute_type_str = models.CharField(+ 'Subsystem Attribute Type',+ max_length = 32,+ null = False, blank = False,+ )+class Meta:verbose_name = 'Subsystem Attribute'verbose_name_plural = 'Subsystem Attributes'def get_upload_to(self):- return 'subsystem/attributes/'+ return 'settings/'+ @property+ def subsystem_attribute_type(self):+ return SubsystemAttributeType.from_name_safe( self.subsystem_attribute_type_str )++ @subsystem_attribute_type.setter+ def subsystem_attribute_type( self, subsystem_attribute_type : SubsystemAttributeType ):+ self.subsystem_attribute_type_str = str(subsystem_attribute_type)+ returndiff --git a/src/hi/apps/config/settings_manager.py b/src/hi/apps/config/settings_manager.pynew file mode 100644index 00000000..270faed8--- /dev/null+++ b/src/hi/apps/config/settings_manager.py@@ -0,0 +1,90 @@+from typing import List++from django.db import transaction++from hi.apps.attribute.enums import AttributeType+from hi.apps.common.singleton import Singleton++from .enums import SubsystemType, SubsystemAttributeType+from .models import Subsystem, SubsystemAttribute+++class SettingsManager( Singleton ):++ def __init_singleton__( self ):+ self._subsystem_list = list()+ self.reload()+ return++ def reload(self):+ self._subsystem_list = self._load_subsystem_list()+ return++ def get_subsystems(self) -> List[ Subsystem ]:+ return self._subsystem_list++ def _load_subsystem_list(self):+ existing_subsystem_map = {+ x.subsystem_type: x+ for x in Subsystem.objects.prefetch_related('attributes').all()+ }++ subsystem_list = list()+ with transaction.atomic():+ for subsystem_type in SubsystemType:+ if subsystem_type in existing_subsystem_map:+ subsystem = self._ensure_all_attributes(+ subsystem = existing_subsystem_map[subsystem_type],+ )+ else:+ subsystem = self._create_subsystem( subsystem_type )+ subsystem_list.append( subsystem )+ continue++ return subsystem_list++ def _create_subsystem( self, subsystem_type : SubsystemType ):+ subsystem = Subsystem.objects.create(+ name = subsystem_type.label,+ subsystem_type_str = str(subsystem_type),+ )+ for subsystem_attribute_type in SubsystemAttributeType:+ if subsystem_type != subsystem_attribute_type.subsystem_type:+ continue+ self._create_attribute(+ subsystem = subsystem,+ subsystem_attribute_type = subsystem_attribute_type,+ )+ continue+ return subsystem++ def _ensure_all_attributes( self, subsystem : Subsystem ):++ existing_attr_types = { x.subsystem_attribute_type for x in subsystem.attributes.all() }++ for subsystem_attribute_type in SubsystemAttributeType:+ if subsystem.subsystem_type != subsystem_attribute_type.subsystem_type:+ continue+ if subsystem_attribute_type not in existing_attr_types:+ self._create_attribute(+ subsystem = subsystem,+ subsystem_attribute_type = subsystem_attribute_type,+ )+ continue+ return subsystem++ def _create_attribute( self,+ subsystem : Subsystem,+ subsystem_attribute_type : SubsystemAttributeType ) -> SubsystemAttribute:+ return SubsystemAttribute.objects.create(+ subsystem = subsystem,+ subsystem_attribute_type_str = str(subsystem_attribute_type),+ name = subsystem_attribute_type.label,+ value = subsystem_attribute_type.initial_value,+ value_type_str = str(subsystem_attribute_type.value_type),+ value_range_str = subsystem_attribute_type.value_range_str,+ attribute_type_str = AttributeType.PREDEFINED,+ is_editable = subsystem_attribute_type.is_editable,+ is_required = subsystem_attribute_type.is_required,+ )+diff --git a/src/hi/apps/config/system_settings.py b/src/hi/apps/config/system_settings.pydeleted file mode 100644index ac8eb931..00000000--- a/src/hi/apps/config/system_settings.py+++ /dev/null@@ -1,54 +0,0 @@-import json-from typing import List--from django.db import transaction--from hi.apps.attribute.enums import AttributeType-from hi.apps.common.singleton import Singleton--from .enums import SubsystemType, SubsystemAttributeType-from .models import Subsystem, SubsystemAttribute---class SystemSettings( Singleton ):-- def __init_singleton__( self ):- return-- def get_subsystems(self) -> List[ Subsystem ]:-- existing_subsystem_map = { x.subsystem_type: x for x in Subsystem.objects.all() }-- subsystem_list = list()- with transaction.atomic():- for subsystem_type in SubsystemType:- if subsystem_type in existing_subsystem_map:- subsystem = existing_subsystem_map[subsystem_type]- else:- subsystem = self._create_subsystem( subsystem_type )- subsystem_list.append( subsystem )- continue-- return subsystem_list-- def _create_subsystem( self, subsystem_type : SubsystemType ):- subsystem = Subsystem.objects.create(- name = subsystem_type.label,- subsystem_type_str = str(subsystem_type),- )- for subsystem_attribute_type in SubsystemAttributeType:- if subsystem_type != subsystem_attribute_type.subsystem_type:- continue- SubsystemAttribute.objects.create(- subsystem = subsystem,- name = subsystem_attribute_type.label,- value = subsystem_attribute_type.initial_value,- value_type_str = str(subsystem_attribute_type.value_type),- value_range_str = subsystem_attribute_type.value_range_str,- attribute_type_str = AttributeType.PREDEFINED,- is_editable = subsystem_attribute_type.is_editable,- is_required = subsystem_attribute_type.is_required,- )- continue- return subsystem-diff --git a/src/hi/apps/config/views.py b/src/hi/apps/config/views.pyindex 6a395225..06a11775 100644--- a/src/hi/apps/config/views.py+++ b/src/hi/apps/config/views.py@@ -8,7 +8,7 @@ from hi.hi_grid_view import HiGridViewfrom .enums import ConfigPageTypefrom .forms import SubsystemAttributeFormSet-from .system_settings import SystemSettings+from .settings_manager import SettingsManagerclass ConfigPageView( HiGridView ):@@ -74,7 +74,7 @@ class ConfigSettingsView( ConfigPageView ):def get_template_context( self, request, *args, **kwargs ):- subsystem_list = SystemSettings().get_subsystems()+ subsystem_list = SettingsManager().get_subsystems()subsystem_attribute_formset_list = list()for subsystem in subsystem_list:@@ -94,7 +94,7 @@ class ConfigSettingsView( ConfigPageView ):def post( self, request, *args, **kwargs ):- subsystem_list = SystemSettings().get_subsystems()+ subsystem_list = SettingsManager().get_subsystems()all_valid = Truesubsystem_attribute_formset_list = list()