Commit 1067f8e8 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

device dao changed

parent 6b335653
import psycopg2
from psycopg2 import sql
import bcrypt
from misc import with_psql
class DeviceDao:
def __init__(self):
pass
# decorator implementation
def with_psql(f):
def _with_psql(*args, **kwargs):
conn = psycopg2.connect('dbname=gateway')
cur = conn.cursor()
try:
res = f(cur, *args, **kwargs)
except (Exception, psycopg2.DatabaseError) as error:
conn.rollback()
res = (False, error)
else:
conn.commit()
finally:
cur.close()
conn.close()
return res
return _with_psql
@staticmethod
@with_psql
def create_datatable(cur, appkey, dev_id):
@with_psql
def create_datatable(cur, appkey, dev_id):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
cur.execute(
sql.SQL(
......@@ -43,9 +15,8 @@ class DeviceDao:
return (True,)
@staticmethod
@with_psql
def delete_datatable(cur, appkey, dev_id):
@with_psql
def delete_datatable(cur, appkey, dev_id):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
cur.execute(
psycopg2.sql.SQL(
......@@ -53,9 +24,8 @@ class DeviceDao:
).format(sql.Identifier(tn)))
return (True,)
@staticmethod
@with_psql
def create_table(cur, appkey):
@with_psql
def create_table(cur, appkey):
tn = 'devices_' +str(appkey)
cur.execute(
sql.SQL(
......@@ -68,9 +38,8 @@ class DeviceDao:
return (True,)
@staticmethod
@with_psql
def delete_table(cur, appkey):
@with_psql
def delete_table(cur, appkey):
tn = 'devices_' +str(appkey)
cur.execute(
psycopg2.sql.SQL(
......@@ -78,12 +47,8 @@ class DeviceDao:
).format(sql.Identifier(tn)))
return (True,)
@staticmethod
@with_psql
def create(cur, name, dev_id, appkey, desc):
@with_psql
def create(cur, name, dev_id, appkey, desc):
tn = 'devices_' +str(appkey)
query = """
INSERT INTO
......@@ -96,9 +61,8 @@ class DeviceDao:
return (True,)
@staticmethod
@with_psql
def delete(cur, appkey, dev_id):
@with_psql
def delete(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """
DELETE FROM
......@@ -111,9 +75,8 @@ class DeviceDao:
return (True,)
@staticmethod
@with_psql
def get(cur, appkey, dev_id):
@with_psql
def get(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """
SELECT * FROM
......@@ -131,9 +94,8 @@ class DeviceDao:
return (True, dev)
@staticmethod
@with_psql
def get_list(cur, appkey):
@with_psql
def get_list(cur, appkey):
tn = 'devices_' +str(appkey)
query = """
SELECT * FROM
......
No preview for this file type
......@@ -98,12 +98,11 @@ def new_application():
def app():
if 'name' in session:
if request.method == 'GET':
dh = dd.DeviceDao()
session['appkey'] = request.args.get('appkey')
app = ad.get(session['appkey'])
devs = dh.get_list(app[1][1])
devs = dd.get_list(app[1][1])
try:
filelist = [f for f in os.listdir(DATA_DOWNLOAD_DIR) if f.startswith(session['appkey'])]
......@@ -125,8 +124,7 @@ def app():
if not res[0]:
return render_template('new-app.html', feedback=res[1])
dh = dd.DeviceDao()
res = dh.create_table(appkey)
res = dd.create_table(appkey)
if not res[0]:
ad.delete(appkey)
......@@ -139,13 +137,12 @@ def app():
@server.route('/delete-app')
def delete_app():
if 'name' in session:
dh = dd.DeviceDao()
devs = dh.get_list(session['appkey'])
devs = dd.get_list(session['appkey'])
for dev in devs[1]:
data.delete_table(session['appkey'], dev[1])
dh.delete_table(session['appkey'])
dd.delete_table(session['appkey'])
res = ad.delete(session['appkey'])
......@@ -160,8 +157,7 @@ def delete_app():
@server.route('/add-dev')
def new_dev():
if 'name' in session:
dh = dd.DeviceDao()
dev_list = dh.get_list(session['appkey'])
dev_list = dd.get_list(session['appkey'])
#print('dev list : ', dev_list)
......@@ -177,9 +173,8 @@ def new_dev():
@server.route('/dev', methods=['GET', 'POST'])
def dev():
if 'name' in session:
dh = dd.DeviceDao()
if request.method == 'GET':
dev = dh.get(session['appkey'], request.args.get('id'))
dev = dd.get(session['appkey'], request.args.get('id'))
session['devid'] = dev[1][1]
session['devname'] = dev[1][0]
......@@ -193,7 +188,7 @@ def dev():
return render_template('dev.html', dev=dev[1], appkey=session['appkey'], ltup=ltup)
else:
res = dh.create(request.form['devname'], request.form['devid'], session['appkey'], request.form['devdesc'])
res = dd.create(request.form['devname'], request.form['devid'], session['appkey'], request.form['devdesc'])
if not res[0]:
return render_template('add-dev.html', feedback=res[1])
......@@ -201,7 +196,7 @@ def dev():
res = data.create_table(session['appkey'], request.form['devid'])
if not res[0]:
dh.delete(session['appkey'], request.form['devid'])
dd.delete(session['appkey'], request.form['devid'])
return render_template('add-dev.html', feedback=res[1])
else:
return redirect(url_for('app', appkey=session['appkey']))
......@@ -244,9 +239,8 @@ def dev_conf():
@server.route('/delete-dev')
def delete_dev():
if 'name' in session and 'devid' in session:
dh = dd.DeviceDao()
data.delete_table(session['appkey'], session['devid'])
res = dh.delete(session['appkey'], session['devid'])
res = dd.delete(session['appkey'], session['devid'])
return redirect(url_for('app', appkey=session['appkey']))
else:
......
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