Commit 20405f04 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

deviceDao added and tested(not all)

parent d1d6fd42
import psycopg2
import bcrypt
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(cur, name, dev_id, appkey, desc):
query = """
INSERT INTO
devices
VALUES
(%s, %s, %s, %s)
"""
cur.execute(query, (name, dev_id, appkey, desc))
return (True,)
@staticmethod
@with_psql
def delete(cur, appkey, dev_id):
query = """
DELETE FROM
devices
WHERE
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (appkey, dev_id))
return (True,)
@staticmethod
@with_psql
def get(cur, appkey, dev_id):
query = """
SELECT * FROM
devices
WHERE
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (appkey, 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):
query = """
SELECT * FROM
devices
WHERE
app_key = %s
"""
cur.execute(query, (appkey,))
return (True, cur.fetchall())
...@@ -4,77 +4,22 @@ import bcrypt ...@@ -4,77 +4,22 @@ import bcrypt
import misc import misc
import dao.user.user as ud import dao.user.user as ud
import dao.application.application as ad import dao.application.application as ad
import dao.device.device as dd
APP_KEY_LEN = 8
server = Flask(__name__, template_folder='templates/') server = Flask(__name__, template_folder='templates/')
def new_app_devs(appkey):
suc = (True, 'app_devs created')
try:
conn = psycopg2.connect('dbname=gateway')
cur = conn.cursor()
query = """
CREATE TABLE devs_%s (
name VARCHAR(30) NOT NULL,
dev_id NUMERIC(3) PRIMARY KEY,
app_key VARCHAR(80),
description VARCHAR(200)
FOREIGN KEY (app_key) REFERENCES applications(app_key)
);
"""
cur.execute(query, (appkey,))
conn.commit()
print('Devs table created')
except (Exception, psycopg2.DatabaseError) as error:
print('Error creating devs table: ', error)
suc = (False, error)
finally:
if (conn):
cur.close()
conn.close()
return suc
def get_devs(appkey):
res = []
try:
conn = psycopg2.connect('dbname=gateway')
cur = conn.cursor()
query = """
SELECT * FROM
devs-%s
"""
cur.execute(query, (appkey,))
res = cur.fetchall()
except (Exception, psycopg2.DatabaseError) as error:
print('Error querying applications: ', error)
finally:
if (conn):
cur.close()
conn.close()
return res
@server.route('/') @server.route('/')
def index(): def index():
if 'name' in session and len(session['name']) > 0: if 'name' in session and len(session['name']) > 0:
ah = ad.ApplicationDao() ah = ad.ApplicationDao()
apps = ah.get_list(session['name'].encode('utf-8')) apps = ah.get_list(session['name'].encode('utf-8'))
print('apps: ', apps) print('apps: ', apps)
return render_template('index.html', apps=apps[1]) if apps[0]:
return render_template('index.html', apps=apps[1])
else:
return render_template('index.html', feedback=apps[1])
return render_template('index.html') return render_template('index.html')
...@@ -143,10 +88,11 @@ def new_application(): ...@@ -143,10 +88,11 @@ def new_application():
def app(): def app():
ah = ad.ApplicationDao() ah = ad.ApplicationDao()
if request.method == 'GET': if request.method == 'GET':
dh = dd.DeviceDao()
app = ah.get(request.args.get('appkey')) app = ah.get(request.args.get('appkey'))
devs = get_devs(app[1]) devs = dh.get_list(app[1][1])
print('devs : ', devs)
return render_template('app.html', app=app[1], devs=devs) return render_template('app.html', app=app[1], devs=devs[1])
else: else:
if request.form['appname'] == '': if request.form['appname'] == '':
error = 'Application name cannot be empty.' error = 'Application name cannot be empty.'
......
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
{% else %} {% else %}
<p>Log in, please</p> <p>Log in, please</p>
{% endif %} {% endif %}
{% if feedback %}
<p class="text-danger float-right"> {{ feedback }} </p>
{% endif %}
</div> </div>
</div> </div>
......
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