]> www.vanbest.org Git - motioneye-debian/commitdiff
added preliminary preferences support
authorCalin Crisan <ccrisan@gmail.com>
Tue, 10 Nov 2015 14:24:02 +0000 (16:24 +0200)
committerCalin Crisan <ccrisan@gmail.com>
Tue, 10 Nov 2015 14:59:03 +0000 (16:59 +0200)
motioneye/handlers.py
motioneye/prefs.py [new file with mode: 0644]
motioneye/server.py
motioneye/templates/main.html

index e1debcf25115d29781101c380fcef759aa701ef4..37bfc323d756b7c688bf892ea77f96ab014289ac 100644 (file)
@@ -30,6 +30,7 @@ import config
 import mediafiles
 import motionctl
 import powerctl
+import prefs
 import remote
 import settings
 import smbctl
@@ -90,6 +91,12 @@ class BaseHandler(RequestHandler):
             logging.error('authentication failed for user %(user)s' % {'user': username})
 
         return None
+    
+    def get_pref(self, key, default=None):
+        return prefs.get(self.current_user or 'anonymous', key, default)
+        
+    def set_pref(self, key, value):
+        return prefs.set(self.current_user or 'anonymous', key, value)
         
     def _handle_request_exception(self, exception):
         try:
@@ -1340,6 +1347,22 @@ class MovieHandler(BaseHandler):
             raise HTTPError(400, 'unknown operation')
 
 
+class PrefsHandler(BaseHandler):
+    def get(self, key):
+        self.finish_json(self.get_pref(key))
+
+    def post(self, key):
+        try:
+            value = json.loads(self.request.body)
+
+        except Exception as e:
+            logging.error('could not decode json: %s' % e)
+
+            raise
+
+        self.set_pref(key, value)
+    
+
 class RelayEventHandler(BaseHandler):
     @BaseHandler.auth(admin=True)
     def post(self):
diff --git a/motioneye/prefs.py b/motioneye/prefs.py
new file mode 100644 (file)
index 0000000..793277c
--- /dev/null
@@ -0,0 +1,96 @@
+
+# Copyright (c) 2013 Calin Crisan
+# This file is part of motionEye.
+#
+# motionEye is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
+
+import json
+import logging
+import os.path
+
+import settings
+
+
+_PREFS_FILE_NAME = 'prefs.json'
+_prefs = None
+
+
+def _load():
+    global _prefs
+    
+    _prefs = {}
+
+    file_path = os.path.join(settings.CONF_PATH, _PREFS_FILE_NAME)
+    
+    if os.path.exists(file_path):
+        logging.debug('loading preferences from "%s"...' % file_path)
+    
+        try:
+            file = open(file_path, 'r')
+        
+        except Exception as e:
+            logging.error('could not open preferences file "%s": %s' % (file_path, e))
+            
+            return
+        
+        try:
+            _prefs = json.load(file)
+
+        except Exception as e:
+            logging.error('could not read preferences from file "%s": %s'(file_path, e))
+
+        finally:
+            file.close()
+            
+    else:
+        logging.debug('preferences file "%s" does not exist, using default preferences')
+
+
+def _save():
+    file_path = os.path.join(settings.CONF_PATH, _PREFS_FILE_NAME)
+    
+    logging.debug('saving preferences to "%s"...' % file_path)
+
+    try:
+        file = open(file_path, 'w')
+
+    except Exception as e:
+        logging.error('could not open preferences file "%s": %s' % (file_path, e))
+        
+        return
+
+    try:
+        json.dump(_prefs, file, sort_keys=True, indent=4)
+
+    except Exception as e:
+        logging.error('could not save preferences to file "%s": %s'(file_path, e))
+
+    finally:
+        file.close()
+
+
+def get(username, key, default=None):
+    if _prefs is None:
+        _load()
+
+    return _prefs.get(username, {}).get(key, default)
+
+
+def set(username, key, value):
+    if _prefs is None:
+        _load()
+
+    _prefs.setdefault(username, {})[key] = value
+    _save()
+
index b2eaf83becf0e552bd19145b72d84cdb3d66efbe..816924a77149d90375794832e198a2da5a51d68d 100644 (file)
@@ -170,6 +170,7 @@ handler_mapping = [
     (r'^/movie/(?P<camera_id>\d+)/(?P<op>list)/?$', handlers.MovieHandler),
     (r'^/movie/(?P<camera_id>\d+)/(?P<op>download|preview|delete)/(?P<filename>.+?)/?$', handlers.MovieHandler),
     (r'^/movie/(?P<camera_id>\d+)/(?P<op>delete_all)/(?P<group>.*?)/?$', handlers.MovieHandler),
+    (r'^/prefs/(?P<key>\w+)/?$', handlers.PrefsHandler),
     (r'^/_relay_event/?$', handlers.RelayEventHandler),
     (r'^/log/(?P<name>\w+)/?$', handlers.LogHandler),
     (r'^/update/?$', handlers.UpdateHandler),
index 6ccd717868ba2bfb97cf5270bb108f8e097b1bea..47b86777dc057b78aac2b2f9990f165fc07dfff2 100644 (file)
                 </div>
                 <table class="settings">
                     <tr class="settings-item">
-                        <td class="settings-item-label"><span class="settings-item-label">Show Advanced Settings</span></td>
+                        <td class="settings-item-label"><span class="settings-item-label">Advanced Settings</span></td>
                         <td class="settings-item-value"><input type="checkbox" class="styled general main-config" id="showAdvancedSwitch"></td>
                         <td><span class="help-mark" title="enable this to be able to access all the advanced settings">?</span></td>
                     </tr>
                     <tr class="settings-item" required="true" strip="true">
-                        <td class="settings-item-label"><span class="settings-item-label">Administrator Username</span></td>
+                        <td class="settings-item-label"><span class="settings-item-label">Admin Username</span></td>
                         <td class="settings-item-value"><input type="text" class="styled general main-config" id="adminUsernameEntry" readonly="readonly"></td>
                         <td><span class="help-mark" title="the username to be used for configuring the system">?</span></td>
                     </tr>
                     <tr class="settings-item" strip="true" {% if enable_reboot %}reboot="true"{% endif %}>
-                        <td class="settings-item-label"><span class="settings-item-label">Administrator Password</span></td>
+                        <td class="settings-item-label"><span class="settings-item-label">Admin Password</span></td>
                         <td class="settings-item-value"><input type="password" class="styled general main-config" id="adminPasswordEntry"></td>
                         <td><span class="help-mark" title="administrator's password">?</span></td>
                     </tr>