Skip to content

Commit

Permalink
sql continued - missed PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Apr 7, 2023
1 parent 5a5236c commit b1038a6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
44 changes: 23 additions & 21 deletions ovos_local_backend/backend/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@

import base64

import flask

import ovos_local_backend.database as db
from flask import request
from ovos_local_backend.backend import API_VERSION
from ovos_local_backend.backend.decorators import noindex, requires_admin


def get_database_crud(app):

# DATABASE - (backend manager uses these)
@app.route("/" + API_VERSION + "/admin/<uuid>/skill_settings",
methods=['POST'])
@requires_admin
@noindex
def create_skill_settings(uuid):
data = request.json
data = flask.request.json
skill_id = data.pop("skill_id")
device = db.get_device(uuid)
if not device:
Expand Down Expand Up @@ -64,7 +66,7 @@ def get_skill_settings(uuid, skill_id):
success = db.delete_skill_settings(remote_id)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_skill_settings(remote_id, **request.json)
entry = db.update_skill_settings(remote_id, **flask.request.json)
else: # GET
entry = db.get_skill_settings(remote_id)
if not entry:
Expand All @@ -76,7 +78,7 @@ def get_skill_settings(uuid, skill_id):
@requires_admin
@noindex
def create_shared_skill_settings():
data = request.json
data = flask.request.json
skill_id = data.pop("skill_id")
entry = db.add_skill_settings(skill_id, **data)
if not entry:
Expand All @@ -100,7 +102,7 @@ def get_shared_skill_settings(skill_id):
success = db.delete_skill_settings(skill_id)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_skill_settings(skill_id, **request.json)
entry = db.update_skill_settings(skill_id, **flask.request.json)
else: # GET
entry = db.get_skill_settings(skill_id)
if not entry:
Expand All @@ -112,7 +114,7 @@ def get_shared_skill_settings(skill_id):
@requires_admin
@noindex
def create_oauth_app():
entry = db.add_oauth_application(**request.json)
entry = db.add_oauth_application(**flask.request.json)
if not entry:
return {"error": "entry not found"}
return entry.serialize()
Expand All @@ -134,7 +136,7 @@ def get_oauth_apps(token_id):
success = db.delete_oauth_application(token_id)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_oauth_application(token_id, **request.json)
entry = db.update_oauth_application(token_id, **flask.request.json)
else: # GET
entry = db.get_oauth_application(token_id)
if not entry:
Expand All @@ -146,7 +148,7 @@ def get_oauth_apps(token_id):
@requires_admin
@noindex
def create_oauth_toks():
entry = db.add_oauth_token(**request.json)
entry = db.add_oauth_token(**flask.request.json)
if not entry:
return {"error": "entry not found"}
return entry.serialize()
Expand All @@ -168,7 +170,7 @@ def get_oauth_toks(token_id):
success = db.delete_oauth_token(token_id)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_oauth_token(token_id, **request.json)
entry = db.update_oauth_token(token_id, **flask.request.json)
else: # GET
entry = db.get_oauth_token(token_id)
if not entry:
Expand All @@ -181,7 +183,7 @@ def get_oauth_toks(token_id):
@noindex
def create_voice_rec():
# b64 decode bytes before saving
data = request.json
data = flask.flask.request.json
audio_b64 = data.pop("audio_b64")
data["byte_data"] = base64.decodestring(audio_b64)
entry = db.add_stt_recording(**data)
Expand All @@ -207,7 +209,7 @@ def get_voice_rec(recording_id):
success = db.delete_stt_recording(recording_id)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_stt_recording(recording_id, **request.json)
entry = db.update_stt_recording(recording_id, **flask.request.json)
else: # GET
entry = db.get_stt_recording(recording_id)
if not entry:
Expand All @@ -220,7 +222,7 @@ def get_voice_rec(recording_id):
@noindex
def create_ww_rec():
# b64 decode bytes before saving
data = request.json
data = flask.request.json
audio_b64 = data.pop("audio_b64")
data["byte_data"] = base64.decodestring(audio_b64)
entry = db.add_ww_recording(**data)
Expand All @@ -246,7 +248,7 @@ def get_ww_rec(recording_id):
success = db.delete_ww_recording(recording_id)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_ww_recording(recording_id, **request.json)
entry = db.update_ww_recording(recording_id, **flask.request.json)
else: # GET
entry = db.get_ww_recording(recording_id)
if not entry:
Expand All @@ -258,7 +260,7 @@ def get_ww_rec(recording_id):
@requires_admin
@noindex
def create_metric():
entry = db.add_metric(**request.json)
entry = db.add_metric(**flask.request.json)
if not entry:
return {"error": "entry not found"}
return entry.serialize()
Expand All @@ -281,7 +283,7 @@ def get_metric(metric_id):
success = db.delete_metric(metric_id)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_metric(metric_id, request.json)
entry = db.update_metric(metric_id, flask.request.json)
else: # GET
entry = db.get_metric(metric_id)
if not entry:
Expand All @@ -293,7 +295,7 @@ def get_metric(metric_id):
@requires_admin
@noindex
def create_device():
entry = db.add_device(**request.json)
entry = db.add_device(**flask.request.json)
if not entry:
return {"error": "entry not found"}
return entry.serialize()
Expand All @@ -315,7 +317,7 @@ def get_device(uuid):
success = db.delete_device(uuid)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_device(uuid, **request.json)
entry = db.update_device(uuid, **flask.request.json)
else: # GET
entry = db.get_device()
if not entry:
Expand All @@ -327,7 +329,7 @@ def get_device(uuid):
@requires_admin
@noindex
def create_voice_defs():
entry = db.add_voice_definition(**request.json)
entry = db.add_voice_definition(**flask.request.json)
if not entry:
return {"error": "entry not found"}
return entry.serialize()
Expand All @@ -349,7 +351,7 @@ def get_voice_def(voice_id):
success = db.delete_voice_definition(voice_id)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_voice_definition(voice_id, **request.json)
entry = db.update_voice_definition(voice_id, **flask.request.json)
else: # GET
entry = db.get_voice_definition(voice_id)
if not entry:
Expand All @@ -361,7 +363,7 @@ def get_voice_def(voice_id):
@requires_admin
@noindex
def create_ww_def():
entry = db.add_wakeword_definition(**request.json)
entry = db.add_wakeword_definition(**flask.request.json)
if not entry:
return {"error": "entry not found"}
return entry.serialize()
Expand All @@ -383,7 +385,7 @@ def get_ww_def(ww_id):
success = db.delete_wakeword_definition(ww_id)
return {"success": success}
elif flask.request.method == 'PUT':
entry = db.update_wakeword_definition(ww_id, **request.json)
entry = db.update_wakeword_definition(ww_id, **flask.request.json)
else: # GET
entry = db.get_wakeword_definition(ww_id)
if not entry:
Expand Down
50 changes: 26 additions & 24 deletions ovos_local_backend/backend/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,63 @@
from flask import request

from ovos_local_backend.backend import API_VERSION
from ovos_local_backend.backend.decorators import noindex, requires_auth, requires_opt_in
from ovos_local_backend.backend.decorators import noindex, requires_auth
from ovos_local_backend.configuration import CONFIGURATION
from ovos_local_backend.utils import generate_code, nice_json
from ovos_local_backend.utils.geolocate import get_request_location
from ovos_local_backend.utils.mail import send_email
from ovos_local_backend.database import (
add_metric,
save_metric,
get_device,
add_device,
SkillSettings,
update_skill_settings,
get_skill_settings_for_device
SkillSettings
)



@requires_opt_in
def save_metric(uuid, name, data):
add_metric(uuid, name, data)



def get_device_routes(app):
@app.route(f"/{API_VERSION}/device/<uuid>/settingsMeta", methods=['PUT'])
@requires_auth
def settingsmeta(uuid):
""" new style skill settings meta (upload only) """
s = SkillSettings.deserialize(request.json)
# ignore s.settings on purpose
update_skill_settings(s.remote_id,
metadata_json=s.meta,
display_name=s.display_name)

# TODO - sql db
# save new settings meta to db
with SettingsDatabase() as db:
# keep old settings, update meta only
old_s = db.get_setting(s.skill_id, uuid)
if old_s:
s.settings = old_s.settings
db.add_setting(uuid, s.skill_id, s.settings, s.meta,
s.display_name, s.remote_id)
# end TODO

return nice_json({"success": True, "uuid": uuid})

@app.route(f"/{API_VERSION}/device/<uuid>/skill/settings", methods=['GET'])
@requires_auth
def skill_settings_v2(uuid):
""" new style skill settings (download only)"""
return {s.skill_id: s.settings for s in get_skill_settings_for_device(uuid)}
# TODO
db = SettingsDatabase()
return {s.skill_id: s.settings for s in db.get_device_settings(uuid)}

@app.route(f"/{API_VERSION}/device/<uuid>/skill", methods=['GET', 'PUT'])
@requires_auth
def skill_settings(uuid):
""" old style skill settings/settingsmeta - supports 2 way sync
PUT - json for 1 skill
GET - list of all skills """
# TODO
if request.method == 'PUT':
s = SkillSettings.deserialize(request.json)
update_skill_settings(s.remote_id,
settings_json=s.settings,
metadata_json=s.meta,
display_name=s.display_name)
# update local db
with SettingsDatabase() as db:
s = SkillSettings.deserialize(request.json)
db.add_setting(uuid, s.skill_id, s.settings, s.meta,
s.display_name, s.remote_id)
return nice_json({"success": True, "uuid": uuid})
else:
return nice_json([s.serialize() for s in get_skill_settings_for_device(uuid)])
return nice_json([s.serialize() for s in SettingsDatabase().get_device_settings(uuid)])

@app.route(f"/{API_VERSION}/device/<uuid>/skillJson", methods=['PUT'])
@requires_auth
Expand All @@ -95,7 +97,7 @@ def skill_json(uuid):
def location(uuid):
device = get_device(uuid)
if device:
return device.location
return device.location_json
return get_request_location()

@app.route(f"/{API_VERSION}/device/<uuid>/setting", methods=['GET'])
Expand Down
11 changes: 6 additions & 5 deletions ovos_local_backend/backend/precise.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import request
import flask
import json

from ovos_local_backend.backend import API_VERSION
from ovos_local_backend.backend.decorators import noindex, requires_auth, requires_opt_in
Expand Down Expand Up @@ -43,9 +44,9 @@ def get_precise_routes(app):
def precise_upload():
success = False
if CONFIGURATION["record_wakewords"]:
auth = request.headers.get('Authorization', '').replace("Bearer ", "")
auth = flask.request.headers.get('Authorization', '').replace("Bearer ", "")
uuid = auth.split(":")[-1] # this split is only valid here, not selene
success = save_ww_recording(uuid, request.files)
success = save_ww_recording(uuid, flask.request.files)

return {"success": success,
"sent_to_mycroft": False,
Expand All @@ -56,11 +57,11 @@ def precise_upload():
@requires_auth
def precise_upload_v2(uuid):
success = False
if 'audio' not in request.files:
if 'audio' not in flask.request.files:
return "No Audio to upload", 400

if CONFIGURATION["record_wakewords"]:
success = save_ww_recording(uuid, request.files)
success = save_ww_recording(uuid, flask.request.files)

return {"success": success,
"sent_to_mycroft": False,
Expand Down

0 comments on commit b1038a6

Please sign in to comment.