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

device dao changed

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