From 1a25e405387c82b8f1140696d1c3d6c2031490d0 Mon Sep 17 00:00:00 2001 From: Morg42 <43153739+Morg42@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:16:32 +0200 Subject: [PATCH 1/7] database: add orphan reassignment --- database/__init__.py | 19 +++++++++++++++ database/webif/__init__.py | 5 ++-- database/webif/templates/base_database.html | 4 ++- database/webif/templates/index.html | 27 ++++++++++++++++++++- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/database/__init__.py b/database/__init__.py index 65eb46b76..086251a90 100755 --- a/database/__init__.py +++ b/database/__init__.py @@ -967,6 +967,25 @@ def _count_orphanlogentries(self): return + def reassign_orphaned_id(self, orphan_id, to): + """ + Reassign values from orphaned item ID to given item ID + + :param orphan_id: item id of the orphaned item + :param to: item id of the target item + :type orphan_id: int + :type to: int + """ + self.logger.error(f'reassign called: {orphan_id} -> {to}') + cur = self._db_maint.cursor() + self._execute(self._prepare("UPDATE {log} SET item_id = :newid WHERE item_id = :orphanid;"), {'newid': to, 'orphanid': orphan_id}, cur=cur) + self._execute(self._prepare("DELETE FROM {item} WHERE id = :orphanid LIMIT 1;"), {'orphanid': orphan_id}, cur=cur) + self.logger.info(f'reassigned orphaned id {orphan_id} to new id {to}') + cur.close() + self._db_maint.commit() + self.logger.debug('rebuilding orphan list') + self.build_orphanlist() + def _delete_orphan(self, item_path): """ Delete orphan item or logentries it diff --git a/database/webif/__init__.py b/database/webif/__init__.py index 9c9178391..d31a4b270 100755 --- a/database/webif/__init__.py +++ b/database/webif/__init__.py @@ -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): + time_orig=None, changed_orig=None, orphanID=None, newID=None): """ Build index.html for cherrypy @@ -76,6 +76,8 @@ 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 orphanID is not None and newID is not None and orphanID != newID: + self.plugin.reassign_orphaned_id(orphanID, to=newID) 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: @@ -271,7 +273,6 @@ def db_sqldump(self): return - @cherrypy.expose def cleanup(self): self.plugin.cleanup() diff --git a/database/webif/templates/base_database.html b/database/webif/templates/base_database.html index 8a305091a..0bbea1793 100755 --- a/database/webif/templates/base_database.html +++ b/database/webif/templates/base_database.html @@ -31,7 +31,8 @@ { className: "time", targets: 2 }, { className: "type", targets: 3 }, { className: "id", targets: 4, render: $.fn.dataTable.render.number('.', ',', 0, '') }, - { className: "logcount", targets: 5, render: $.fn.dataTable.render.number('.', ',', 0, '') }, + { className: "reassign", targets: 5 }, + { className: "logcount", targets: 6, render: $.fn.dataTable.render.number('.', ',', 0, '') }, ].concat($.fn.dataTable.defaults.columnDefs)}); {% else %} orphantable = $('#orphantable').DataTable( { @@ -42,6 +43,7 @@ { className: "time", targets: 2 }, { className: "type", targets: 3 }, { className: "id", targets: 4, render: $.fn.dataTable.render.number('.', ',', 0, '') }, + { className: "reassign", targets: 5 }, ].concat($.fn.dataTable.defaults.columnDefs)}); {% endif %} diff --git a/database/webif/templates/index.html b/database/webif/templates/index.html index 6a52ed64b..9939883f1 100755 --- a/database/webif/templates/index.html +++ b/database/webif/templates/index.html @@ -158,6 +158,17 @@ {% set tab3title = _('Verwaiste Items') %} {% block bodytab3 %} + + +