Commit 06100a6f authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

changed table management, tested

parent 5c990a0c
import psycopg2 import psycopg2
import bcrypt import bcrypt
from misc import rand_str
APP_KEY_LEN = 8
class ApplicationDao: class ApplicationDao:
...@@ -31,14 +29,14 @@ class ApplicationDao: ...@@ -31,14 +29,14 @@ class ApplicationDao:
@staticmethod @staticmethod
@with_psql @with_psql
def create(cur, name, username, desc): def create(cur, name, appkey, username, desc):
query = """ query = """
INSERT INTO INSERT INTO
applications applications
VALUES VALUES
(%s, %s, %s, %s) (%s, %s, %s, %s)
""" """
cur.execute(query, (name, rand_str(APP_KEY_LEN), username, desc)) cur.execute(query, (name, appkey, username, desc))
return (True,) return (True,)
......
import psycopg2 import psycopg2
from psycopg2 import sql
import bcrypt import bcrypt
...@@ -27,46 +28,100 @@ class DeviceDao: ...@@ -27,46 +28,100 @@ class DeviceDao:
return res return res
return _with_psql return _with_psql
@staticmethod
@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,
data json NOT NULL
)"""
).format(sql.Identifier(tn)))
return (True,)
@staticmethod
@with_psql
def delete_datatable(cur, appkey, dev_id):
tn = 'dev_' +str(appkey)+ '_' +str(dev_id)
cur.execute(
psycopg2.sql.SQL(
"DROP TABLE {}"
).format(sql.Identifier(tn)))
return (True,)
@staticmethod
@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,)
@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 @staticmethod
@with_psql @with_psql
def create(cur, name, dev_id, appkey, desc): def create(cur, name, dev_id, appkey, desc):
tn = 'devices_' +str(appkey)
query = """ query = """
INSERT INTO INSERT INTO
devices {}
VALUES VALUES
(%s, %s, %s, %s) (%s, %s, %s)
""" """
cur.execute(query, (name, dev_id, appkey, desc)) cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [name, dev_id, desc])
return (True,) return (True,)
@staticmethod @staticmethod
@with_psql @with_psql
def delete(cur, appkey, dev_id): def delete(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """ query = """
DELETE FROM DELETE FROM
devices {}
WHERE WHERE
app_key = %s
AND
dev_id = %s dev_id = %s
""" """
cur.execute(query, (appkey, dev_id)) cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
return (True,) return (True,)
@staticmethod @staticmethod
@with_psql @with_psql
def get(cur, appkey, dev_id): def get(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """ query = """
SELECT * FROM SELECT * FROM
devices {}
WHERE WHERE
app_key = %s
AND
dev_id = %s dev_id = %s
""" """
cur.execute(query, (appkey, dev_id)) cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
dev = cur.fetchone() dev = cur.fetchone()
if (dev is None): if (dev is None):
...@@ -78,12 +133,12 @@ class DeviceDao: ...@@ -78,12 +133,12 @@ class DeviceDao:
@staticmethod @staticmethod
@with_psql @with_psql
def get_list(cur, appkey): def get_list(cur, appkey):
tn = 'devices_' +str(appkey)
query = """ query = """
SELECT * FROM SELECT * FROM
devices {}
WHERE
app_key = %s
""" """
cur.execute(query, (appkey,)) cur.execute(
sql.SQL(query).format(sql.Identifier(tn)))
return (True, cur.fetchall()) return (True, cur.fetchall())
No preview for this file type
...@@ -6,6 +6,7 @@ import dao.user.user as ud ...@@ -6,6 +6,7 @@ import dao.user.user as ud
import dao.application.application as ad import dao.application.application as ad
import dao.device.device as dd import dao.device.device as dd
APP_KEY_LEN = 8
server = Flask(__name__, template_folder='templates/') server = Flask(__name__, template_folder='templates/')
...@@ -109,21 +110,38 @@ def app(): ...@@ -109,21 +110,38 @@ def app():
error = 'Application name cannot be empty.' error = 'Application name cannot be empty.'
return render_template('new-app.html', feedback=error) return render_template('new-app.html', feedback=error)
else: else:
res = ah.create(request.form['appname'], session['name'], request.form['appdesc']) appkey = misc.rand_str(APP_KEY_LEN)
res = ah.create(request.form['appname'], appkey, session['name'], request.form['appdesc'])
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])
#res = new_app_devs(request.form['appname'])
#if not res[0]:
# rm_app(request.form['appname'])
# return render_template('new-app.html', feedback=res[1])
#if not res[0] or not rer[0]: dh = dd.DeviceDao()
# return render_template('new-app.html', feedback=str(res[1])+'|'+str(rer[1])) res = dh.create_table(appkey)
#else:
if not res[0]:
ah.delete(appkey)
return render_template('new-app.html', feedback=res[1])
return redirect(url_for('index')) return redirect(url_for('index'))
@server.route('/delete-app')
def delete_app():
dh = dd.DeviceDao()
devs = dh.get_list(session['appkey'])
for dev in devs[1]:
dh.delete_datatable(session['appkey'], dev[1])
dh.delete_table(session['appkey'])
ah = ad.ApplicationDao()
res = ah.delete(session['appkey'])
if not res[0]:
return redirect(url_for('app'))
else:
return redirect(url_for('index'))
@server.route('/add-dev') @server.route('/add-dev')
def new_dev(): def new_dev():
...@@ -145,17 +163,23 @@ def new_dev(): ...@@ -145,17 +163,23 @@ def new_dev():
def dev(): def dev():
dh = dd.DeviceDao() dh = dd.DeviceDao()
if request.method == 'GET': if request.method == 'GET':
dev = dh.get(session['appkey'], request.args.get('dev_id')) dev = dh.get(session['appkey'], request.args.get('id'))
ltup = 'recently' ltup = 'recently'
return render_template('dev.html', dev=dev[1], 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 = dh.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])
else: else:
return redirect(url_for('app')) res = dh.create_datatable(session['appkey'], request.form['devid'])
if not res[0]:
dh.delete(session['appkey'], request.form['devid'])
return render_template('add-dev.html', feedback=res[1])
else:
return redirect(url_for('app'))
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
{% if devs %} {% if devs %}
<ol> <ol>
{% for dev in devs %} {% for dev in devs %}
<li><a href="/dev?dev_id={{ dev[1] }}"> {{ dev[0] }} </a></li> <li><a href="/dev?id={{ dev[1] }}"> {{ dev[0] }} </a></li>
{% endfor %} {% endfor %}
</ol> </ol>
{% else %} {% else %}
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
<div class="col-md-6 col-md-offset-3"> <div class="col-md-6 col-md-offset-3">
<p>Name: {{ dev[0] }} </p> <p>Name: {{ dev[0] }} </p>
<p>dev_id: {{ dev[1] }} </p> <p>dev_id: {{ dev[1] }} </p>
<p>app-key: {{ dev[2] }} </p> <p>app-key: {{ appkey }} </p>
<p>Description : {{ dev[3] }} </p> <p>Description : {{ dev[2] }} </p>
<p>Last time up : {{ ltup }} </p> <p>Last time up : {{ ltup }} </p>
<a href="/data"><button type="submit" class="btn btn-primary">Data</button></a> <a href="/data"><button type="submit" class="btn btn-primary">Data</button></a>
......
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