Skip to content

Home Information - Your home, your data, organized.

Python

In midst of adding console locking, but settings need a refactor. WIP

Anthony Cassandra9bb1c56b3f34
diff --git a/src/hi/apps/config/settings_manager.py b/src/hi/apps/config/settings_manager.py
index 75bdaa0e..23e47d55 100644
--- a/src/hi/apps/config/settings_manager.py
+++ b/src/hi/apps/config/settings_manager.py
@@ -68,6 +68,34 @@ class SettingsManager( Singleton ):
def get_setting_value( self, setting_enum : SettingEnum ):
return self._attribute_value_map.get( setting_enum.key )
+ def set_setting_value( self, setting_enum : SettingEnum, value : str ):
+
+ module_name = setting_enum.__class__.__module__
+ subsystem_key = '.'.join( module_name.split('.')[:-1] )
+
+ print( f'\n\nMODULE = direct={setting_enum.__module__}, indirect={module_name}, name={app_name}\n' )
+
+
+
+
+
+ raise NotImplementedError()
+
+
+
+ try:
+ db_attribute = SubsystemAttribute.objects.get(
+ subsystem = subsystem,
+ setting_key = setting_key,
+ name = setting_definition.label,
+ )
+ db_attribute.value = value
+ db_attribute.save()
+ self._attribute_value_map[setting_enum.key] = value
+ except SubsystemAttribute.DoesNotExist:
+ raise KeyError( f'No setting "{setting_enum.name}" found.' )
+ return
+
def _discover_app_settings(self) -> List[ AppSettings ]:
app_settings_list = list()
diff --git a/src/hi/apps/console/settings.py b/src/hi/apps/console/settings.py
index c768c92e..7935e6c5 100644
--- a/src/hi/apps/console/settings.py
+++ b/src/hi/apps/console/settings.py
@@ -19,6 +19,15 @@ class ConsoleSetting( SettingEnum ):
is_required = True,
initial_value = 'America/Chicago',
)
+ CONSOLE_LOCK_PASSWORD = SettingDefinition(
+ label = 'Lock Password',
+ description = 'Password to use to unlock console',
+ value_type = AttributeValueType.SECRET,
+ value_range_str = '',
+ is_editable = True,
+ is_required = False,
+ initial_value = '',
+ )
THEME = SettingDefinition(
label = 'Theme',
description = 'Overall look and feel of interfaces',
diff --git a/src/hi/apps/console/templates/console/modals/set_lock_password.html b/src/hi/apps/console/templates/console/modals/set_lock_password.html
new file mode 100644
index 00000000..77175e80
--- /dev/null
+++ b/src/hi/apps/console/templates/console/modals/set_lock_password.html
@@ -0,0 +1,29 @@
+{% extends "modals/action_1.html" %}
+
+{% block preamble %}
+<form action="{% url 'console_set_lock_password' %}" method="post" class="form" data-async="true">
+ {% csrf_token %}
+{% endblock %}
+
+ {% block title_text %}
+ Set Console Lock Password
+ {% endblock %}
+
+ {% block body %}
+ <p>
+ Please create a password to unlock the console.
+ </p>
+ <label for="hi-console-password">Enter Password:</label>
+ <input type="password" id="hi-console-password" name="password" required>
+ {% endblock %}
+
+ {% block action_center %}
+ <button id="hi-console-set-lock-password-button" type="submit" class="btn btn-lg btn-success"
+ name="action" value="set">
+ SET PASSWORD
+ </button>
+ {% endblock %}
+
+{% block postamble %}
+</form>
+{% endblock %}
diff --git a/src/hi/apps/console/urls.py b/src/hi/apps/console/urls.py
index 9cdda3bc..15ce5571 100644
--- a/src/hi/apps/console/urls.py
+++ b/src/hi/apps/console/urls.py
@@ -9,6 +9,10 @@ urlpatterns = [
views.ConsoleLockView.as_view(),
name='console_lock'),
+ re_path( r'^set-lock-password$',
+ views.SetLockPasswordView.as_view(),
+ name='console_set_lock_password'),
+
re_path( r'^unlock$',
views.ConsoleUnlockView.as_view(),
name='console_unlock'),
diff --git a/src/hi/apps/console/views.py b/src/hi/apps/console/views.py
index 6b12cff8..94db60f5 100644
--- a/src/hi/apps/console/views.py
+++ b/src/hi/apps/console/views.py
@@ -1,34 +1,69 @@
+from django.core.exceptions import BadRequest
from django.http import JsonResponse
+from django.shortcuts import render
+from django.views.generic import View
+
+from hi.apps.config.settings_manager import SettingsManager
from hi.hi_async_view import HiModalView
from .constants import ConsoleConstants
+from .settings import ConsoleSetting
-class ConsoleLockView( HiModalView ):
-
- def get_template_name( self ) -> str:
- return 'console/modals/console_unlock.html'
+class ConsoleLockView( View ):
def post( self, request, *args, **kwargs ):
+ lock_password = SettingsManager().get_setting_value( ConsoleSetting.CONSOLE_LOCK_PASSWORD )
+ if not lock_password:
+ return SetLockPasswordView().get( request, *args, **kwargs )
+
request.session[ConsoleConstants.CONSOLE_LOCKED_SESSION_VAR] = True
- context = {
- }
- return self.modal_response( request, context )
+ return ConsoleUnlockView().get( request, *args, **kwargs )
-class ConsoleUnlockView( ConsoleLockView ):
+class SetLockPasswordView( HiModalView ):
+ def get_template_name( self ) -> str:
+ return 'console/modals/set_lock_password.html'
+
def get( self, request, *args, **kwargs ):
- context = {
- }
- return self.modal_response( request, context )
+ return self.modal_response( request )
+
+ def post( self, request, *args, **kwargs ):
+ input_password = request.POST.get('password')
+ if not input_password:
+ raise BadRequest( 'No password provided.' )
+ SettingsManager().set_setting_value( ConsoleSetting.CONSOLE_LOCK_PASSWORD, input_password )
+ request.session[ConsoleConstants.CONSOLE_LOCKED_SESSION_VAR] = True
+ return ConsoleUnlockView().get( request, *args, **kwargs )
+
+class ConsoleUnlockView( HiModalView ):
+
+ def get_template_name( self ) -> str:
+ return 'console/modals/console_unlock.html'
+
+ def get(self, request, *args, **kwargs):
+ return self.modal_response( request )
+
def post(self, request, *args, **kwargs):
# N.B. Simplified security for now. Just meant to be used when
# visitors in the house to prevent snooping. Beef up security here
# if/when needed.
-
+ input_password = request.POST.get('password')
+ if not input_password:
+ raise BadRequest( 'No password provided.' )
+
+ lock_password = SettingsManager().get_setting_value( ConsoleSetting.CONSOLE_LOCK_PASSWORD )
+ if lock_password and ( input_password != lock_password ):
+ raise BadRequest( 'Invalid password.' )
+
+ # N.B. It should not be possible to get into state where console is
+ # locked without a password set. However, if it happens, it would
+ # be impossible to unlock the console. Thus, we'll unlock in that
+ # exceptional case.
+
request.session[ConsoleConstants.CONSOLE_LOCKED_SESSION_VAR] = False
return JsonResponse( {'message': 'Console unlocked.'} )
diff --git a/src/hi/hi_async_view.py b/src/hi/hi_async_view.py
index 92bbec5c..adcc12c0 100644
--- a/src/hi/hi_async_view.py
+++ b/src/hi/hi_async_view.py
@@ -4,7 +4,6 @@ import urllib.parse
from django.shortcuts import render
from django.template.loader import get_template
-from django.utils.safestring import mark_safe
from django.views.generic import View
import hi.apps.common.antinode as antinode
@@ -109,7 +108,7 @@ class HiModalView( View ):
def get_template_name( self ) -> str:
raise NotImplementedError('Subclasses must override this method.')
- def modal_response( self, request, context, status = 200 ):
+ def modal_response( self, request, context = None, status = 200 ):
modal_response = antinode.modal_from_template(
request = request,
template_name = self.get_template_name(),