Skip to content

Commit

Permalink
database: change selection to modal dialogue
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Oct 14, 2024
1 parent 1a25e40 commit 6d7e38d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 28 deletions.
21 changes: 12 additions & 9 deletions database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,15 +976,18 @@ def reassign_orphaned_id(self, orphan_id, to):
: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()
try:
self.logger.warning(f'reassigning orphaned data from (old) id {orphan_id} to (new) 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.warning(f'reassigned orphaned id {orphan_id} to new id {to}')
cur.close()
self._db_maint.commit()
self.logger.warning('rebuilding orphan list')
self.build_orphanlist()
except Exception as e:
self.logger.error(f'error on reassigning id {orphan_id} to {to}: {e}')

def _delete_orphan(self, item_path):
"""
Expand Down
8 changes: 5 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, orphanID=None, newID=None):
time_orig=None, changed_orig=None, orphan_id=None, new_id=None):
"""
Build index.html for cherrypy
Expand All @@ -76,8 +76,10 @@ 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)
self.logger.error(f'index called with oid {orphan_id} and nid {new_id}')
if orphan_id is not None and new_id is not None and orphan_id != new_id:
self.logger.error(f'calling reassign for {orphan_id} and {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
37 changes: 37 additions & 0 deletions database/webif/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* The Modal (background) */
.or-modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 999; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.or-modal-content {
background-color: #fefefe;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button */
.or-close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}

.or-close:hover,
.or-close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
64 changes: 48 additions & 16 deletions database/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
{% set dataSet = 'overview' %}
{% set tab1title = _('Database Items') %}

{% block pluginstyles %}
<link rel="stylesheet" href="static/style.css">
{% endblock pluginstyles %}

{%- block pluginscripts %}
{{ super() }}
<script>
Expand Down Expand Up @@ -160,15 +164,52 @@
{% 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);
}
function reassignOrphan(selObj) {
// get modal box data from elements
var orphanID = document.getElementById('orphan-item-id').textContent;
var orphanItem = document.getElementById('orphan-item-name').textContent;
var newID = document.getElementById('orphanSelect').value;
var newItem = document.getElementById('orphanSelect').selectedOptions[0].text;

// reset elements (just to be sure...)
document.getElementById('orphan-item-id').textContent = "";
document.getElementById('orphan-item-name').textContent = "";
document.getElementById('orphanSelect').selectedIndex = 0;

// 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);
}
</script>

<div id="orphanModal" class="or-modal">
<div class="or-modal-content">
<span class="or-close" onclick="document.getElementById('orphanModal').style.display = 'none';">&times;</span>
<div>
<strong>Neuzuweisen von Itemreihe <span id='orphan-item-name'></span> (ID <span id='orphan-item-id'></span>)</strong>
</div>
<div>
<span>Bitte wählen Sie die Itemreihe aus, der die verwaisten Daten zugewiesen werden sollen: </span>
<select id="orphanSelect" name="{{ item }}_reassign" onchange="btn = document.getElementById('orphanAssignBtn'); if (this.value == -1) { btn.disabled = true; } else { btn.disabled = false; }">
<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" id="orphanAssignBtn" disabled class="btn btn-danger btn-sm" onclick="reassignOrphan()">Zuweisen</button>
<button type="button" class="btn btn-shng btn-sm" onclick="document.getElementById('orphanModal').style.display = 'none';">Abbrechen</button>
</div>
</div>
</div>

<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 Down Expand Up @@ -214,16 +255,7 @@
<td id="{{ item }}_orphan_type">{{ _(p.db_itemtype(item)) }}</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> -->
<button type="button" id="orphanBtn" class="btn btn-danger btn-sm" value="{{ itemid }}-{{ item }}" onclick="document.getElementById('orphan-item-name').textContent = '{{ item }}'; document.getElementById('orphan-item-id').textContent = '{{ itemid }}'; document.getElementById('orphanModal').style.display = 'block';"><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>
Expand Down

0 comments on commit 6d7e38d

Please sign in to comment.