Skip to content

Home Information - Your home, your data, organized.

Python

In midst of adding console lock feature. WIP

Anthony Cassandra24a0d899897d
diff --git a/src/hi/apps/console/console_manager.py b/src/hi/apps/console/console_manager.py
index 58a90e09..90215a55 100644
--- a/src/hi/apps/console/console_manager.py
+++ b/src/hi/apps/console/console_manager.py
@@ -1,11 +1,15 @@
import logging
from typing import Dict
+from django.http import HttpRequest
+from django.template.loader import get_template
+
from hi.apps.common.singleton import Singleton
from hi.apps.config.settings_manager import SettingsManager
from .audio_file import AudioFile
from .audio_signal import AudioSignal
+from .constants import ConsoleConstants
logger = logging.getLogger(__name__)
@@ -17,6 +21,9 @@ class ConsoleManager(Singleton):
SettingsManager().register_change_listener( self._reload_console_audio_map )
return
+ def is_console_locked( self, request : HttpRequest ) -> bool:
+ return request.session.get( ConsoleConstants.CONSOLE_LOCKED_SESSION_VAR, False )
+
def get_console_audio_map( self ) -> Dict[ str, str ]:
return self._console_audio_map
diff --git a/src/hi/apps/console/constants.py b/src/hi/apps/console/constants.py
new file mode 100644
index 00000000..77f6b6d0
--- /dev/null
+++ b/src/hi/apps/console/constants.py
@@ -0,0 +1,4 @@
+class ConsoleConstants:
+
+ CONSOLE_LOCKED_SESSION_VAR = 'console_locked'
+
diff --git a/src/hi/apps/console/middleware.py b/src/hi/apps/console/middleware.py
new file mode 100644
index 00000000..7e4bce83
--- /dev/null
+++ b/src/hi/apps/console/middleware.py
@@ -0,0 +1,30 @@
+from django.urls import reverse
+
+from .constants import ConsoleConstants
+from .views import ConsoleUnlockView
+
+
+class ConsoleLockMiddleware:
+
+ EXCLUDED_PATHS = [ reverse('console_unlock'), reverse('api_status' ) ]
+
+ def __init__(self, get_response):
+ self.get_response = get_response
+ return
+
+ def __call__(self, request):
+ response = self.process_request( request )
+ if response:
+ return response
+ response = self.get_response(request)
+ return self.process_response( request, response )
+
+ def process_request( self, request ):
+ if request.session.get( ConsoleConstants.CONSOLE_LOCKED_SESSION_VAR, False ):
+ if request.path in self.EXCLUDED_PATHS:
+ return None
+ return ConsoleUnlockView().get( request )
+ return None
+
+ def process_response(self, request, response):
+ return response
diff --git a/src/hi/apps/console/templates/console/modals/console_unlock.html b/src/hi/apps/console/templates/console/modals/console_unlock.html
new file mode 100644
index 00000000..7e60a1ec
--- /dev/null
+++ b/src/hi/apps/console/templates/console/modals/console_unlock.html
@@ -0,0 +1,26 @@
+{% extends "modals/action_1.html" %}
+
+{% block preamble %}
+<form action="{% url 'console_unlock' %}" method="post" class="form" data-async="true">
+ {% csrf_token %}
+{% endblock %}
+
+ {% block title_text %}
+ Console is Locked
+ {% endblock %}
+
+ {% block body %}
+ <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-unlock-button" type="submit" class="btn btn-lg btn-success"
+ name="action" value="unlock">
+ UNLOCK
+ </button>
+ {% endblock %}
+
+{% block postamble %}
+</form>
+{% endblock %}
diff --git a/src/hi/apps/config/templates/config/panes/audio_control.html b/src/hi/apps/console/templates/console/panes/audio_control.html
similarity index 100%
rename from src/hi/apps/config/templates/config/panes/audio_control.html
rename to src/hi/apps/console/templates/console/panes/audio_control.html
diff --git a/src/hi/apps/console/templates/console/panes/console_lock_button.html b/src/hi/apps/console/templates/console/panes/console_lock_button.html
new file mode 100644
index 00000000..e148fe62
--- /dev/null
+++ b/src/hi/apps/console/templates/console/panes/console_lock_button.html
@@ -0,0 +1,10 @@
+<div id="hi-console-lock-button">
+ <form action="{% url 'console_lock' %}" method="post" class="form" data-async="true">
+ {% csrf_token %}
+ <button type="submit" id="hi-console-lock-button" role="button"
+ class="btn btn-secondary btn-control"
+ title="Console Unlocked. Click to Lock">
+ LOCK
+ </button>
+ </form>
+</div>
diff --git a/src/hi/apps/console/urls.py b/src/hi/apps/console/urls.py
new file mode 100644
index 00000000..9cdda3bc
--- /dev/null
+++ b/src/hi/apps/console/urls.py
@@ -0,0 +1,15 @@
+from django.urls import re_path
+
+from . import views
+
+
+urlpatterns = [
+
+ re_path( r'^lock$',
+ views.ConsoleLockView.as_view(),
+ name='console_lock'),
+
+ 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
new file mode 100644
index 00000000..371fef7a
--- /dev/null
+++ b/src/hi/apps/console/views.py
@@ -0,0 +1,34 @@
+from django.http import JsonResponse
+
+from hi.hi_async_view import HiModalView
+
+from .constants import ConsoleConstants
+
+
+class ConsoleLockView( HiModalView ):
+
+ def get_template_name( self ) -> str:
+ return 'console/modals/console_unlock.html'
+
+ def post( self, request, *args, **kwargs ):
+ request.session[ConsoleConstants.CONSOLE_LOCKED_SESSION_VAR] = True
+ context = {
+ }
+ return self.modal_response( request, context )
+
+
+class ConsoleUnlockView( ConsoleLockView ):
+
+ def get( self, request, *args, **kwargs ):
+ context = {
+ }
+ return self.modal_response( request, context )
+
+ 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.
+
+ request.session[ConsoleConstants.CONSOLE_LOCKED_SESSION_VAR] = False
+ return JsonResponse( {'message': 'Console unlocked.'} )
diff --git a/src/hi/constants.py b/src/hi/constants.py
index 8d4e6ed0..b56057d0 100644
--- a/src/hi/constants.py
+++ b/src/hi/constants.py
@@ -56,4 +56,6 @@ DIVID = {
'ALERT_BANNER_CONTAINER': 'hi-alert-banner-container',
'ALERT_BANNER_CONTENT': 'hi-alert-banner-content',
'SECURITY_STATE_CONTROL': 'hi-security-state-control',
+
+ 'CONSOLE_LOCK_BUTTON': 'hi-console-lock-button',
}
diff --git a/src/hi/settings/base.py b/src/hi/settings/base.py
index e0c1bdf8..7c825344 100644
--- a/src/hi/settings/base.py
+++ b/src/hi/settings/base.py
@@ -168,6 +168,7 @@ MIDDLEWARE = [
'hi.middleware.ExceptionMiddleware',
'hi.middleware.ViewMiddleware',
+ 'hi.apps.console.middleware.ConsoleLockMiddleware',
]
ROOT_URLCONF = 'hi.urls'
diff --git a/src/hi/static/js/settings.js b/src/hi/static/js/settings.js
index 1afba056..c3d8291f 100644
--- a/src/hi/static/js/settings.js
+++ b/src/hi/static/js/settings.js
@@ -3,6 +3,7 @@
window.Hi = window.Hi || {};
const HiSettings = {
+
enableAudio: function() {
return _enableAudio( );
},
@@ -25,7 +26,7 @@
const AudioStateSettingName = 'audioState';
const AudioStateEnabled = 'enabled';
const AudioStateDisabled = 'disabled';
-
+
function setConsoleSetting( name, value ) {
Hi.setCookie( name, value );
}
@@ -43,7 +44,7 @@
setConsoleSetting( AudioStateSettingName, AudioStateDisabled );
return true;
}
-
+
function _isAudioEnabled() {
var audioState = getConsoleSetting( AudioStateSettingName );
if ( audioState && ( audioState == AudioStateDisabled ))
diff --git a/src/hi/templates/panes/bottom_buttons.html b/src/hi/templates/panes/bottom_buttons.html
index b765f00e..296c11d3 100644
--- a/src/hi/templates/panes/bottom_buttons.html
+++ b/src/hi/templates/panes/bottom_buttons.html
@@ -4,7 +4,7 @@
<div class="d-flex justify-content-end">
{% include "security/panes/security_state_control.html" %}
<div>
- {% include "config/panes/audio_control.html" %}
+ {% include "console/panes/audio_control.html" %}
</div>
<div>
<a href="#"
@@ -14,11 +14,7 @@
</a>
</div>
<div>
- <a href="#"
- class="btn btn-secondary btn-control" role="button"
- data-async="true" >
- LOCK
- </a>
+ {% include "console/panes/console_lock_button.html" %}
</div>
<div>
<a href="{% url 'event_history' %}"
diff --git a/src/hi/urls.py b/src/hi/urls.py
index 4fb0648d..a77bb86f 100644
--- a/src/hi/urls.py
+++ b/src/hi/urls.py
@@ -40,6 +40,7 @@ urlpatterns = [
re_path( r'^alert/', include('hi.apps.alert.urls' )),
re_path( r'^security/', include('hi.apps.security.urls' )),
re_path( r'^notify/', include('hi.apps.notify.urls' )),
+ re_path( r'^console/', include('hi.apps.console.urls' )),
# Custom error pages
re_path( r'^400.html$', views.bad_request_response, name='bad_request' ),