Commit 89a3d682 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

application & device delete fixed (notifications), everything tested up to...

application & device delete fixed (notifications), everything tested up to now, todo:user settings & admin
parent b90bf1e4
...@@ -94,6 +94,21 @@ def get(cur, appkey, dev_id): ...@@ -94,6 +94,21 @@ def get(cur, appkey, dev_id):
else: else:
return (True, dev) return (True, dev)
@with_psql
def update(cur, appkey, devid, name, desc):
tn = 'devices_'+appkey
query = """
UPDATE
{}
SET
name = %s,
description = %s
WHERE
dev_id = %s
""".format(tn)
cur.execute(query, (name, desc, devid))
return (True,)
@with_psql @with_psql
def get_list(cur, appkey): def get_list(cur, appkey):
......
...@@ -61,6 +61,19 @@ def get_list(cur, appkey): ...@@ -61,6 +61,19 @@ def get_list(cur, appkey):
return (True, cur.fetchall()) return (True, cur.fetchall())
@with_psql
def get_per_device(cur, appkey, devid):
query = """
SELECT * FROM
notifications
WHERE
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (appkey, devid))
return (True, cur.fetchall())
@with_psql @with_psql
def get_alerts_list(cur, appkey): def get_alerts_list(cur, appkey):
......
...@@ -59,7 +59,22 @@ def delete_list(cur, appkey): ...@@ -59,7 +59,22 @@ def delete_list(cur, appkey):
cur.execute(query, (appkey,)) cur.execute(query, (appkey,))
return (True,) return (True,)
@with_psql
def delete_per_device(cur, appkey, devid):
query = """
DELETE FROM
notifications_queue
WHERE
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (appkey, devid))
return (True,)
@with_psql @with_psql
def get_all(cur): def get_all(cur):
query = """ query = """
......
...@@ -94,18 +94,18 @@ ...@@ -94,18 +94,18 @@
</li> </li>
{% if session['role'] == 'admin' %} {% if session['role'] == 'admin' %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link " href="./examples/icons.html"> <a class="nav-link " href="./administration">
<i class="ni ni-briefcase-24 text-pink"></i> Administration <i class="ni ni-briefcase-24 text-pink"></i> Administration
</a> </a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link " href="./examples/icons.html"> <a class="nav-link " href="./users">
<i class="ni ni-single-02 text-info"></i> Users <i class="ni ni-single-02 text-info"></i> Users
</a> </a>
</li> </li>
{% endif %} {% endif %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link " href="./examples/icons.html"> <a class="nav-link " href="./settings">
<i class="ni ni-settings-gear-65 text-grey"></i> Settings <i class="ni ni-settings-gear-65 text-grey"></i> Settings
</a> </a>
</li> </li>
......
...@@ -61,7 +61,7 @@ ...@@ -61,7 +61,7 @@
<script type="text/javascript"> <script type="text/javascript">
function remove_alert(id, devid) { function remove_alert(id, devid) {
$.ajax({ $.ajax({
url:"/application/{{ app[1] }}/remove-alert?id="+id+"&devid="+devid, url:"/application/{{ app[1] }}/delete-alert?id="+id+"&devid="+devid,
type:"get", type:"get",
success: function() { success: function() {
location.reload(); location.reload();
......
...@@ -58,7 +58,7 @@ ...@@ -58,7 +58,7 @@
<script type="text/javascript"> <script type="text/javascript">
function remove_automation(id, devid) { function remove_automation(id, devid) {
$.ajax({ $.ajax({
url:"/application/{{ app[1] }}/remove-automation?id="+id+"&devid="+devid, url:"/application/{{ app[1] }}/delete-automation?id="+id+"&devid="+devid,
type:"get", type:"get",
success: function() { success: function() {
location.reload(); location.reload();
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
<div class="col"> <div class="col">
<div class="card shadow"> <div class="card shadow">
<div class="card-header bg-transparent"> <div class="card-header bg-transparent">
<h3 class="mb-0">Device Configuration - {{ dev[0] }}</h3> <h3 class="mb-0">Device Configuration</h3>
</div> </div>
<div class="card-body"> <div class="card-body">
<form id="confform" onsubmit="onsub()"> <form id="confform" onsubmit="onsub()">
......
{% extends 'logged_layout.html' %}
{% block title %} HPC&amp;A IoT - Device Settings {% endblock %}
{% block location %}
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/applications">Applications</a>
<p class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block"> &nbsp;-&nbsp; </p>
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/application/{{ app[1] }}">{{ app[0] }}</a>
<p class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block"> &nbsp;-&nbsp; </p>
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/application/{{ app[1] }}/device/{{ dev[1] }}">{{ dev[0] }}</a>
{% endblock %}
{% block body %}
<!-- Page content -->
<div class="container-fluid mt--7">
<!-- Table -->
<div class="row">
<div class="col">
<div class="card shadow">
<div class="card-header bg-transparent">
<h3 class="mb-0">Device Settings</h3>
</div>
<div class="card-body">
<form action="/application/{{ app[1] }}/device/{{ dev[1] }}/settings" method="post" id="devsettings">
<div class="form-group">
<label>Name:</label><br>
<input type="text" maxlength="30" class="form-control" id="devname" name="devname" value="{{ dev[0] }}" required><br>
</div>
<div class="form-group">
<label>Description:</label><br>
<textarea id="devdesc" maxlength="200" class="form-control" name="devdesc" rows="5"></textarea>
</div>
</form>
<br>
<div class="row" style="margin-top: 30px;">
<div class="col-lg-3">
<button type="submit" form="devsettings" class="btn btn-primary btn-block">Save</button>
</div>
<div class="col-lg-3">
</div>
<div class="col-lg-3">
</div>
<div class="col-lg-3">
<button type="button" class="btn btn-danger btn-block" data-toggle="modal" data-target="#exampleModal">Delete</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Are you sure?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
This action will remove permanently the device and its corresponding data.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<a href="/application/{{ app[1] }}/device/{{ dev[1] }}/delete"><button type="button" class="btn btn-danger">Delete Device</button></a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block script %}
<script type="text/javascript">
document.getElementById("devdesc").value = "{{ dev[2] }}";
</script>
{% endblock %}
...@@ -124,32 +124,8 @@ ...@@ -124,32 +124,8 @@
<a href="./{{ dev[1] }}/download-csv"<button type="submit" class="btn btn-primary btn-block">Download CSV</button></a> <a href="./{{ dev[1] }}/download-csv"<button type="submit" class="btn btn-primary btn-block">Download CSV</button></a>
</div> </div>
<div class="col-lg-4"> <div class="col-lg-4">
<button type="submit" class="btn btn-primary btn-block">Settings</button> <a href="./{{ dev[1] }}/settings"<button type="submit" class="btn btn-primary btn-block">Settings</button></a>
</div> </div>
<!-- put inside settings
<div class="col-lg-3">
<button type="button" class="btn btn-danger btn-block" data-toggle="modal" data-target="#exampleModal">Delete</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
Modal Body ....
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-danger">Delete Application</button>
</div>
</div>
</div>
</div>
</div>
-->
</div> </div>
</div> </div>
</div> </div>
...@@ -205,35 +181,10 @@ ...@@ -205,35 +181,10 @@
// Declare all variables // Declare all variables
var i, tabcontent, tablinks; var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
//tabcontent = document.getElementsByClassName("tab-content");
//for (i = 0; i < tabcontent.length; i++) {
// tabcontent[i].style.display = "none";
//}
// Get all elements with class="tablinks" and remove the class "active"
//tablinks = document.getElementsByClassName("tablinks");
//for (i = 0; i < tablinks.length; i++) {
// tablinks[i].className = tablinks[i].className.replace(" active", "");
//}
// Show the current tab, and add an "active" class to the button that opened the tab
//document.getElementById(dname).style.display = "block";
//evt.currentTarget.className += " active";
tscroll = init_scroll(); tscroll = init_scroll();
fetch('/application/{{ app[1] }}/device/{{ dev[1] }}/data/'+dname+'/graph/1').then(res => res.text()).then(data => drawChart(dname, eval(data))); fetch('/application/{{ app[1] }}/device/{{ dev[1] }}/data/'+dname+'/graph/1').then(res => res.text()).then(data => drawChart(dname, eval(data)));
fetch('/application/{{ app[1] }}/device/{{ dev[1] }}/data/'+dname+'/table/'+tscroll[dname]).then(res => res.text()).then(data => $('#table_'+dname+'_body').html(data)); fetch('/application/{{ app[1] }}/device/{{ dev[1] }}/data/'+dname+'/table/'+tscroll[dname]).then(res => res.text()).then(data => $('#table_'+dname+'_body').html(data));
/*
window.onscroll = function (ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
tscroll[dname] += 1;
fetch(window.origin+'/dev-data/'+dname+'/table/'+tscroll[dname]).then(res => res.text()).then(data => $('#table_'+dname+'_body').append(data));
}
};
*/
} }
function show_first_page() { function show_first_page() {
......
...@@ -178,6 +178,13 @@ def application_delete(appkey): ...@@ -178,6 +178,13 @@ def application_delete(appkey):
for dev in devs[1]: for dev in devs[1]:
data.delete_table(appkey, dev[1]) data.delete_table(appkey, dev[1])
# delete notifications
nq.delete_per_device(appkey, dev[1])
nfss = nfs.get_per_device(appkey, dev[1])
for nf in nfss[1]:
tr.delete(appkey, dev[1], nf[0])
tr.delete_function(appkey, dev[1], nf[0])
nfs.delete(appkey, dev[1], nf[0])
dd.delete_table(appkey) dd.delete_table(appkey)
...@@ -205,8 +212,6 @@ def application_device(appkey, devid): ...@@ -205,8 +212,6 @@ def application_device(appkey, devid):
ld = data.get_last_range(appkey, devid, [MAX_PG_ENTRIES_DATA, 0]) ld = data.get_last_range(appkey, devid, [MAX_PG_ENTRIES_DATA, 0])
cnt = data.get_count(appkey, devid) cnt = data.get_count(appkey, devid)
print(ld)
ltup = 'Device have not any sent data yet' ltup = 'Device have not any sent data yet'
...@@ -241,11 +246,29 @@ def application_add_device(appkey): ...@@ -241,11 +246,29 @@ def application_add_device(appkey):
flash('Error: {}'.format(res[1]), 'danger') flash('Error: {}'.format(res[1]), 'danger')
return render_template(request.url) return render_template(request.url)
else: else:
return redirect(url_for('applications')) return redirect(url_for('application', appkey=appkey))
else: else:
return redirect(url_for('login')) return redirect(url_for('login'))
@app.route('/application/<appkey>/device/<devid>/delete')
def application_device_delete(appkey, devid):
if 'name' in session:
nq.delete_per_device(appkey, devid)
nfss = nfs.get_per_device(appkey, devid)
for nf in nfss[1]:
tr.delete(appkey, devid, nf[0])
tr.delete_function(appkey, devid, nf[0])
nfs.delete(appkey, devid, nf[0])
data.delete_table(appkey, devid)
res = dd.delete(appkey, devid)
return redirect(url_for('application', appkey=appkey))
else:
return redirect(utl_for('login'))
@app.route('/application/<appkey>/device/<devid>/configure', methods=['GET', 'POST']) @app.route('/application/<appkey>/device/<devid>/configure', methods=['GET', 'POST'])
def application_device_configuration(appkey, devid): def application_device_configuration(appkey, devid):
if 'name' in session: if 'name' in session:
...@@ -348,9 +371,7 @@ def app_(): ...@@ -348,9 +371,7 @@ def app_():
session['appkey'] = request.args.get('appkey') session['appkey'] = request.args.get('appkey')
ap = ad.get(session['appkey']) ap = ad.get(session['appkey'])
print(ap)
devs = dd.get_list(ap[1][1]) devs = dd.get_list(ap[1][1])
print(devs)
session['appname'] = ap[1][0] session['appname'] = ap[1][0]
if session['role'] == 'admin' or session['name'] == ap[1][2]: if session['role'] == 'admin' or session['name'] == ap[1][2]:
...@@ -762,7 +783,6 @@ def application_alerts(appkey): ...@@ -762,7 +783,6 @@ def application_alerts(appkey):
if 'name' in session: if 'name' in session:
ap = ad.get(appkey) ap = ad.get(appkey)
alerts = nfs.get_alerts_list(appkey) alerts = nfs.get_alerts_list(appkey)
print(alerts)
return render_template('new/public/alerts.html', alert_list=alerts[1], app=ap[1]) return render_template('new/public/alerts.html', alert_list=alerts[1], app=ap[1])
else: else:
return redirect(url_for('login')) return redirect(url_for('login'))
...@@ -800,7 +820,7 @@ def application_new_alert(appkey): ...@@ -800,7 +820,7 @@ def application_new_alert(appkey):
return redirect(url_for('login')) return redirect(url_for('login'))
@app.route('/application/<appkey>/remove-<ntype>') @app.route('/application/<appkey>/delete-<ntype>')
def application_notification_remove(appkey, ntype): def application_notification_remove(appkey, ntype):
if 'name' in session: if 'name' in session:
nq.delete(appkey, request.args.get('devid'), request.args.get('id')) nq.delete(appkey, request.args.get('devid'), request.args.get('id'))
...@@ -888,6 +908,27 @@ def application_settings(appkey): ...@@ -888,6 +908,27 @@ def application_settings(appkey):
return redirect(url_for('login')) return redirect(url_for('login'))
@app.route('/application/<appkey>/device/<devid>/settings', methods=['GET', 'POST'])
def application_device_settings(appkey, devid):
if 'name' in session:
if request.method == 'GET':
ap = ad.get(appkey)
dev = dd.get(appkey, devid)
dev_list = dd.get_list(appkey)
return render_template('new/public/device-settings.html', app=ap[1], dev=dev[1], free_ids=misc.prep_id_range(dev_list[1]))
elif request.method == 'POST':
res = dd.update(appkey, devid, request.form['devname'], request.form['devdesc'])
if not res[0]:
flash('Error: {}'.format(res[1]), 'danger')
return render_template(request.url)
return redirect(request.url)
else:
return redirect(url_for('login'))
@app.route('/alerts') @app.route('/alerts')
def alerts(): def alerts():
if 'name' in session: if 'name' in session:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment