Commit 2f8a8810 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

devid checked to be not existent on creation

parent 9d4efd7b
[2020-09-13 17:53:00,310] INFO in views: Logged in superuser admin [2020-09-13 17:53:00,310] INFO in views: Logged in superuser admin
[2020-09-13 17:55:00,520] INFO in views: superuser admin created new application THSO 49f4d289 [2020-09-13 17:55:00,520] INFO in views: superuser admin created new application THSO 49f4d289
[2020-09-13 17:58:38,291] INFO in views: superuser admin added new device ESP32 for application 49f4d289 [2020-09-13 17:58:38,291] INFO in views: superuser admin added new device ESP32 for application 49f4d289
[2020-10-01 15:08:14,780] INFO in views: Logged in superuser admin
[2020-10-01 15:08:37,331] INFO in views: Logged in superuser admin
[2020-10-01 15:10:09,547] INFO in views: superuser admin failed to add new device tricky for application 8cc6f4b1. Cause: existing devid
[2020-10-01 15:11:29,771] INFO in views: superuser admin failed to add new device tricky for application 8cc6f4b1. Cause: existing devid
[2020-10-01 15:11:42,083] INFO in views: superuser admin added new device tricky for application 8cc6f4b1
[2020-10-01 15:11:55,695] INFO in views: superuser admin deleted for application 8cc6f4b1 device 2
...@@ -136,6 +136,7 @@ def get(cur, appkey, dev_id): ...@@ -136,6 +136,7 @@ def get(cur, appkey, dev_id):
dev[3] = json.loads(dev[3].tobytes()) dev[3] = json.loads(dev[3].tobytes())
return (True, dev) return (True, dev)
@with_psql @with_psql
def update(cur, appkey, devid, name, desc): def update(cur, appkey, devid, name, desc):
tn = 'devices_'+appkey tn = 'devices_'+appkey
...@@ -221,3 +222,20 @@ def get_count_by_user(cur, username): ...@@ -221,3 +222,20 @@ def get_count_by_user(cur, username):
count += cur.fetchone()[0] count += cur.fetchone()[0]
return count return count
@with_psql
def check_devid(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """
SELECT dev_id FROM
{}
WHERE
dev_id = %s
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
dev = cur.fetchone()
return dev is None
...@@ -217,24 +217,29 @@ def application_add_device(appkey): ...@@ -217,24 +217,29 @@ def application_add_device(appkey):
dev_list = dd.get_list(appkey) dev_list = dd.get_list(appkey)
return render_template('views/public/add-device.html', app=ap[1], free_ids=misc.prep_id_range(dev_list[1]), models=ddm.MODELS) return render_template('views/public/add-device.html', app=ap[1], free_ids=misc.prep_id_range(dev_list[1]), models=ddm.MODELS)
elif request.method == 'POST': elif request.method == 'POST':
ddmin = ddm.extract(request) if dd.check_devid(appkey, request.form['devid']):
ddmin = ddm.extract(request)
res = dd.create_ddm(request.form['devname'], request.form['devid'], appkey, request.form['devdesc'], ddmin)
if not res[0]: res = dd.create_ddm(request.form['devname'], request.form['devid'], appkey, request.form['devdesc'], ddmin)
app.logger.error('%s %s failed to add device for application %s - %s', session['role'], session['name'], appkey, res[1])
flash('Error: {}'.format(res[1]), 'danger')
return redirect(request.url)
else:
res = data.create_table_ddm(appkey, request.form['devid'])
if not res[0]: if not res[0]:
app.logger.error('%s %s failed to add device for application %s - %s', session['role'], session['name'], appkey, res[1]) app.logger.error('%s %s failed to add device for application %s - %s', session['role'], session['name'], appkey, res[1])
dd.delete(session['appkey'], request.form['devid'])
flash('Error: {}'.format(res[1]), 'danger') flash('Error: {}'.format(res[1]), 'danger')
return redirect(request.url) return redirect(request.url)
else: else:
app.logger.info('%s %s added new device %s for application %s', session['role'], session['name'], request.form['devname'], appkey) res = data.create_table_ddm(appkey, request.form['devid'])
flash('Device added', 'success') if not res[0]:
return redirect(url_for('application', appkey=appkey)) app.logger.error('%s %s failed to add device for application %s - %s', session['role'], session['name'], appkey, res[1])
dd.delete(session['appkey'], request.form['devid'])
flash('Error: {}'.format(res[1]), 'danger')
return redirect(request.url)
else:
app.logger.info('%s %s added new device %s for application %s', session['role'], session['name'], request.form['devname'], appkey)
flash('Device added', 'success')
return redirect(url_for('application', appkey=appkey))
else:
app.logger.info('%s %s failed to add new device %s for application %s. Cause: existing devid', session['role'], session['name'], request.form['devname'], appkey)
flash('Error: Selected Device ID already used, pick another one.', 'danger')
return redirect(request.url)
@app.route('/application/<appkey>/device/<devid>/delete') @app.route('/application/<appkey>/device/<devid>/delete')
......
...@@ -135,25 +135,30 @@ def administration_user_application_add_device(name, appkey): ...@@ -135,25 +135,30 @@ def administration_user_application_add_device(name, appkey):
dev_list = dd.get_list(appkey) dev_list = dd.get_list(appkey)
return render_template('views/admin/user-application-add-device.html', app=ap[1], free_ids=misc.prep_id_range(dev_list[1]), models=ddm.MODELS, user=name) return render_template('views/admin/user-application-add-device.html', app=ap[1], free_ids=misc.prep_id_range(dev_list[1]), models=ddm.MODELS, user=name)
elif request.method == 'POST': elif request.method == 'POST':
ddmin = ddm.extract(request) if dd.check_devid(appkey, request.form['devid']):
res = dd.create_ddm(request.form['devname'], request.form['devid'], appkey, request.form['devdesc'], ddmin) ddmin = ddm.extract(request)
res = dd.create_ddm(request.form['devname'], request.form['devid'], appkey, request.form['devdesc'], ddmin)
if not res[0]: if not res[0]:
app.logger.error('Administrator %s failed to add device application %s for %s - %s', session['name'], appkey, name, res[1]) app.logger.error('Administrator %s failed to add device application %s for %s - %s', session['name'], appkey, name, res[1])
flash('Error: {}'.format(res[1]), 'danger') flash('Error: {}'.format(res[1]), 'danger')
return redirect(request.url) return redirect(request.url)
res = data.create_table_ddm(appkey, request.form['devid']) res = data.create_table_ddm(appkey, request.form['devid'])
if not res[0]:
dd.delete(appkey, request.form['devid'])
app.logger.error('Administrator %s failed to add device application %s for %s - %s', session['name'], appkey, name, res[1])
flash('Error: {}'.format(res[1]), 'danger')
return rendirect(request.url)
app.logger.warning('Administrator %s added new device %s application %s for %s', session['name'], request.form['devid'], appkey, name)
return redirect(url_for('administration_user_application', name=name, appkey=appkey)) if not res[0]:
dd.delete(appkey, request.form['devid'])
app.logger.error('Administrator %s failed to add device application %s for %s - %s', session['name'], appkey, name, res[1])
flash('Error: {}'.format(res[1]), 'danger')
return rendirect(request.url)
app.logger.warning('Administrator %s added new device %s application %s for %s', session['name'], request.form['devid'], appkey, name)
return redirect(url_for('administration_user_application', name=name, appkey=appkey))
else:
app.logger.info('%s %s failed to add new device %s for application %s. Cause: existing devid', session['role'], session['name'], request.form['devname'], appkey)
flash('Error: Selected Device ID already used, pick another one.', 'danger')
return redirect(request.url)
@app.route('/administration/<name>/application/<appkey>/device/<devid>') @app.route('/administration/<name>/application/<appkey>/device/<devid>')
......
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