diff --git a/VERSION.md b/VERSION.md index f61208c..646dc82 100644 --- a/VERSION.md +++ b/VERSION.md @@ -1,6 +1,4 @@ -24.6 -- Fixed "Deprecated RType () for API request" error -- Fixed camerastream not working +24.7 +- Added REST API read more in wiki - Add Security device -- UI improvments - Add more selectable device types diff --git a/modules/api.py b/modules/api.py index 33b00d2..12acea6 100644 --- a/modules/api.py +++ b/modules/api.py @@ -18,14 +18,30 @@ def modifyServerSettings(request): dbsettings = Settings.query.get_or_404(1) - dbsettings.client_id = request.args.get('aogclient', '') - dbsettings.client_secret = request.args.get('aogsecret', '') - dbsettings.api_key = request.args.get('aogapi', '') - dbsettings.tempunit = request.args.get('tempunit', '') - dbsettings.language = request.args.get('language', '') - dbsettings.use_ssl = (request.args.get('ssl', '') == 'true') - dbsettings.ssl_cert = request.args.get('sslcert', '') - dbsettings.ssl_key = request.args.get('sslkey', '') + client_id = request.args.get('aogclient', None) + if client_id: + dbsettings.client_id = client_id + client_secret = request.args.get('aogsecret', None) + if client_secret: + dbsettings.client_secret = client_secret + api_key = request.args.get('aogapi', None) + if api_key: + dbsettings.api_key = api_key + tempunit = request.args.get('tempunit', None) + if tempunit: + dbsettings.tempunit = tempunit + language = request.args.get('language', None) + if language: + dbsettings.language = language + use_ssl = request.args.get('ssl', None) + if use_ssl: + dbsettings.use_ssl =(True if use_ssl else False) + ssl_cert = request.args.get('sslcert', None) + if ssl_cert: + dbsettings.ssl_cert = ssl_cert + ssl_key = request.args.get('sslkey', None) + if ssl_key: + dbsettings.ssl_key = ssl_key db.session.add(dbsettings) db.session.commit() @@ -37,12 +53,24 @@ def modifyUserSettings(username, request): dbuser = User.query.filter_by(username=username).first() - dbuser.domo_url = request.args.get('domourl', '') - dbuser.domouser = request.args.get('domouser', '') - dbuser.domopass = request.args.get('domopass', '') - dbuser.roomplan = request.args.get('roomplan', '') - dbuser.password = request.args.get('uipassword', '') - dbuser.googleassistant = (request.args.get('googleassistant', '') == 'true') + domo_url = request.args.get('domourl', None) + if domo_url: + dbuser.domo_url = domo_url + domouser = request.args.get('domouser', None) + if domouser: + dbuser.domouser = domouser + domopass = request.args.get('domopass', None) + if domopass: + dbuser.domopass = domopass + roomplan = request.args.get('roomplan', None) + if roomplan: + dbuser.roomplan = roomplan + password = request.args.get('uipassword', None) + if password: + dbuser.password = password + googleassistant = request.args.get('googleassistant', None) + if googleassistant: + dbuser.googleassistant = (True if googleassistant == 'true' else False) db.session.add(dbuser) db.session.commit() @@ -70,12 +98,12 @@ def gateway(): else: getDomoticzDevices(flask_login.current_user.username) flash("Devices synced with domoticz") - return "Devices synced with domoticz", 200 + result = '{"title": "RequestedSync", "status": "OK"}' elif custom == "restart": - - logger.info('Restarts smarthome server') - os.execv(sys.executable, ['python'] + sys.argv) + if dbuser.admin: + logger.info('Restarts smarthome server') + os.execv(sys.executable, ['python'] + sys.argv) elif custom == "setArmLevel": armLevel = request.args.get('armLevel', '') @@ -83,29 +111,33 @@ def gateway(): result = queryDomoticz(flask_login.current_user.username, '?type=command¶m=setsecstatus&secstatus=' + armLevel + '&seccode=' + hashlib.md5(str.encode(seccode)).hexdigest()) elif custom == "server_settings": - - modifyServerSettings(request) + if dbuser.admin: + modifyServerSettings(request) + result = '{"title": "ServerSettingsChanged", "status": "OK"}' elif custom == "user_settings": modifyUserSettings(flask_login.current_user.username, request) - + result = '{"title": "UserSettingsChanged", "status": "OK"}' + elif custom == "removeuser": - userToRemove = request.args.get('user', '') + if dbuser.admin: + userToRemove = request.args.get('user', '') - removeuser = User.query.filter_by(username=userToRemove).first() + removeuser = User.query.filter_by(username=userToRemove).first() - db.session.delete(removeuser) - db.session.commit() - remove_user(userToRemove) - logger.info("User " + userToRemove + " is deleted") + db.session.delete(removeuser) + db.session.commit() + remove_user(userToRemove) + logger.info("User " + userToRemove + " is deleted") - return "User removed", 200 + result = '{"title": "UserRemoved", "status": "OK"}' + elif '?type' in requestedUrl[1]: result = queryDomoticz(flask_login.current_user.username, requestedUrl[1]) try: - return json.loads(result) + return json.loads(result), 200 except Exception: - return "No results returned", 404 + return {"title": "No results returned", "status": "ERR"}, 404