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
def create_datatable(cur, appkey, dev_id):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
cur.execute(
sql.SQL(
"""CREATE TABLE {} (
utc NUMERIC(10) NOT NULL,
timedate VARCHAR(100) NOT NULL,
data json NOT NULL
)"""
).format(sql.Identifier(tn)))
return (True,)
class DeviceDao:
def __init__(self): @with_psql
pass def delete_datatable(cur, appkey, dev_id):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
# decorator implementation cur.execute(
def with_psql(f): psycopg2.sql.SQL(
def _with_psql(*args, **kwargs): "DROP TABLE {}"
conn = psycopg2.connect('dbname=gateway') ).format(sql.Identifier(tn)))
cur = conn.cursor() return (True,)
@with_psql
def create_table(cur, appkey):
tn = 'devices_' +str(appkey)
cur.execute(
sql.SQL(
"""CREATE TABLE {} (
name VARCHAR(30) NOT NULL,
dev_id NUMERIC(3) PRIMARY KEY,
description VARCHAR(200)
)"""
).format(sql.Identifier(tn)))
return (True,)
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 @with_psql
return _with_psql def delete_table(cur, appkey):
tn = 'devices_' +str(appkey)
@staticmethod cur.execute(
@with_psql psycopg2.sql.SQL(
def create_datatable(cur, appkey, dev_id): "DROP TABLE {}"
tn = 'dev_' +str(appkey)+ '_' +str(dev_id) ).format(sql.Identifier(tn)))
cur.execute( return (True,)
sql.SQL(
"""CREATE TABLE {} ( @with_psql
utc NUMERIC(10) NOT NULL, def create(cur, name, dev_id, appkey, desc):
timedate VARCHAR(100) NOT NULL, tn = 'devices_' +str(appkey)
data json NOT NULL query = """
)""" INSERT INTO
).format(sql.Identifier(tn))) {}
return (True,) VALUES
(%s, %s, %s)
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [name, dev_id, desc])
return (True,)
@with_psql
def delete(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """
DELETE FROM
{}
WHERE
dev_id = %s
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
return (True,)
@with_psql
def get(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """
SELECT * FROM
{}
WHERE
dev_id = %s
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
dev = cur.fetchone()
@staticmethod if (dev is None):
@with_psql return (False, 'There is no device with dev_id = {}'.format(dev_id))
def delete_datatable(cur, appkey, dev_id): else:
tn = 'dev_' +str(appkey)+ '_' +str(dev_id) return (True, dev)
cur.execute(
psycopg2.sql.SQL(
"DROP TABLE {}" @with_psql
).format(sql.Identifier(tn))) def get_list(cur, appkey):
return (True,) tn = 'devices_' +str(appkey)
query = """
@staticmethod SELECT * FROM
@with_psql {}
def create_table(cur, appkey): """
tn = 'devices_' +str(appkey) cur.execute(
cur.execute( sql.SQL(query).format(sql.Identifier(tn)))
sql.SQL( return (True, cur.fetchall())
"""CREATE TABLE {} (
name VARCHAR(30) NOT NULL,
dev_id NUMERIC(3) PRIMARY KEY,
description VARCHAR(200)
)"""
).format(sql.Identifier(tn)))
return (True,)
@staticmethod
@with_psql
def delete_table(cur, appkey):
tn = 'devices_' +str(appkey)
cur.execute(
psycopg2.sql.SQL(
"DROP TABLE {}"
).format(sql.Identifier(tn)))
return (True,)
@staticmethod
@with_psql
def create(cur, name, dev_id, appkey, desc):
tn = 'devices_' +str(appkey)
query = """
INSERT INTO
{}
VALUES
(%s, %s, %s)
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [name, dev_id, desc])
return (True,)
@staticmethod
@with_psql
def delete(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """
DELETE FROM
{}
WHERE
dev_id = %s
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
return (True,)
@staticmethod
@with_psql
def get(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """
SELECT * FROM
{}
WHERE
dev_id = %s
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
dev = cur.fetchone()
if (dev is None):
return (False, 'There is no device with dev_id = {}'.format(dev_id))
else:
return (True, dev)
@staticmethod
@with_psql
def get_list(cur, appkey):
tn = 'devices_' +str(appkey)
query = """
SELECT * FROM
{}
"""
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)))
return (True, cur.fetchall())
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