Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database: add orphan reassignment #963

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 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):
time_orig=None, changed_orig=None, orphanID=None, newID=None):
"""
Build index.html for cherrypy

Expand All @@ -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:
Expand Down Expand Up @@ -271,7 +273,6 @@ def db_sqldump(self):

return


@cherrypy.expose
def cleanup(self):
self.plugin.cleanup()
Expand Down
4 changes: 3 additions & 1 deletion database/webif/templates/base_database.html
Original file line number Diff line number Diff line change
Expand Up @@ -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( {
Expand All @@ -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 %}

Expand Down
27 changes: 26 additions & 1 deletion database/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@

{% set tab3title = _('Verwaiste Items') %}
{% block bodytab3 %}

<script type="text/javascript">
function reassignOrphan(selObj, orphanID) {
var value = selObj.value;
if (confirm('{{ _("Wollen Sie alle Datensätze des verwaisten Items (ID ' + orphanID + ') der ID ' + value + ' neu zuweisen?") }}')) {
shngPost('', {orphanID: orphanID, newID: value});
// setTimeout(window.location.reload(), 3000);
}
}
</script>

<div class="container-fluid m-2 table-resize">
<div id="webif-orphanbuttons" style="margin-left: 10px; margin-bottom: 10px; float:right; white-space: nowrap;" class="mb-2">
{% if p.remove_orphan or len(p.orphanlist) == 0 %}
Expand All @@ -174,13 +185,15 @@
<th>{{ _('Letzte Änderung') }}</th>
<th>{{ _('Typ') }}</th>
<th class="dt-head-right" title="{{ _('Item-ID in der Datenbank') }}">{{ _('DB-ID') }}</th>
<th>{{ _('Neuzuweisung') }}</th>
{% if p.count_logentries %}
<th class="logcount dt-head-right">{{ _('Anzahl Einträge') }}</th>
{% endif %}
</tr>
</thead>
<tbody>
{% for item in p.orphanlist %}
{% set itemid = p.id(item, create=False) %}
<tr>
<td></td>
<td id="{{ item }}_orphan_path">{{ item }}</td>
Expand All @@ -199,7 +212,19 @@
{% endif %}
{% endif %}</td>
<td id="{{ item }}_orphan_type">{{ _(p.db_itemtype(item)) }}</td>
<td id="{{ item }}_orphan_id">{{ p.id(item, create=False) }}</td>
<td id="{{ item }}_orphan_id">{{ itemid }}</td>
<td id="{{ item }}_orphan_reassign">
<select name="{{ item }}_reassign" onchange="reassignOrphan(this, {{ itemid }})">
<option value=-1 selected="selected"></option>
{% for newitem in items %}
{% set newitemid = p.id(newitem, create=False) %}
{% if p.has_iattr(newitem.conf, 'database') %}
<option value={{ newitemid }}>{{ newitem.property.path }} ({{ newitemid }})</option>
{% endif %}
{% endfor %}
</select>
<!-- <button type="button" class="btn btn-danger btn-sm" onclick="">><i class="fas fa-share"></i></button> -->
</td>
{% if p.count_logentries %}
<td id="{{ item }}_orphan_logcount">{{ p._orphan_logcount[p.id(item, create=False)] }}</td>
{% endif %}
Expand Down
Loading