Skip to content

Commit

Permalink
database: move to REST communication
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Oct 14, 2024
1 parent 8fac751 commit 8df8228
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ def reassign_orphaned_id(self, orphan_id, to):
self.build_orphanlist()
except Exception as e:
self.logger.error(f'error on reassigning id {orphan_id} to {to}: {e}')
return e

def _delete_orphan(self, item_path):
"""
Expand Down
26 changes: 23 additions & 3 deletions database/webif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, webif_dir, plugin):

@cherrypy.expose
def index(self, reload=None, action=None, item_id=None, item_path=None, time_end=None, day=None, month=None, year=None,
time_orig=None, changed_orig=None, orphan_id=None, new_id=None):
time_orig=None, changed_orig=None):
"""
Build index.html for cherrypy
Expand All @@ -76,8 +76,6 @@ def index(self, reload=None, action=None, item_id=None, item_path=None, time_end
if item_path is not None:
item = self.plugin.items.return_item(item_path)
delete_triggered = False
if orphan_id is not None and new_id is not None and orphan_id != new_id:
self.plugin.reassign_orphaned_id(orphan_id, to=new_id)
if action is not None:
if action == "delete_log" and item_id is not None:
if time_orig is not None and changed_orig is not None:
Expand Down Expand Up @@ -132,6 +130,28 @@ def index(self, reload=None, action=None, item_id=None, item_path=None, time_end
tabcount=2, action=action, item_id=item_id, delete_triggered=delete_triggered,
language=self.plugin.get_sh().get_defaultlanguage())

@cherrypy.expose
def reassign(self):
cl = cherrypy.request.headers['Content-Length']
if not cl:
return
try:
rawbody = cherrypy.request.body.read(int(cl))
data = json.loads(rawbody)
except Exception:
return
orphan_id = data.get("orphan_id")
new_id = data.get("new_id")
result = {"operation": "request", "result": "success"}
if orphan_id is not None and new_id is not None and orphan_id != new_id:
self.logger.info(f'reassigning orphaned id {orphan_id} to new id {new_id}')
err = self.plugin.reassign_orphaned_id(orphan_id, to=new_id)
if err:
return
return json.dumps(result)
else:
self.logger.warning(f'reassigning orphaned id {orphan_id} to new id {new_id} failed')

@cherrypy.expose
def get_data_html(self, dataSet=None, params=None):
"""
Expand Down
22 changes: 17 additions & 5 deletions database/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,23 @@
// debug: show call parameters
// alert("shngPost('', {orphan_id: " + orphanID + ", new_id: " + newID + "});");

// call index page with arguments
shngPost('', {orphan_id: orphanID, new_id: newID});

// reload page to reflect recalculated orphans
setTimeout(window.location.reload(), 3000);
var mydata = {"orphan_id": orphanID, "new_id": newID};
$.ajax({
type: "POST",
url: "reassign",
data: JSON.stringify(mydata),
contentType: 'application/json',
dataType: 'json',
error: function() {
alert("Fehler beim Übermitteln der Daten. Bitte shng-Log prüfen!");
document.getElementById('orphanModal').style.display = 'none';
},
success: function() {
document.getElementById('orphanModal').style.display = 'none';
// reload page to reflect recalculated orphans
setTimeout(window.location.reload(), 3000);
}
})
}
</script>

Expand Down

0 comments on commit 8df8228

Please sign in to comment.