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

changed table management, tested

parent 5c990a0c
import psycopg2
import bcrypt
from misc import rand_str
APP_KEY_LEN = 8
class ApplicationDao:
......@@ -31,14 +29,14 @@ class ApplicationDao:
@staticmethod
@with_psql
def create(cur, name, username, desc):
def create(cur, name, appkey, username, desc):
query = """
INSERT INTO
applications
VALUES
(%s, %s, %s, %s)
"""
cur.execute(query, (name, rand_str(APP_KEY_LEN), username, desc))
cur.execute(query, (name, appkey, username, desc))
return (True,)
......
import psycopg2
from psycopg2 import sql
import bcrypt
......@@ -27,46 +28,100 @@ class DeviceDao:
return res
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
@with_psql
def create(cur, name, dev_id, appkey, desc):
tn = 'devices_' +str(appkey)
query = """
INSERT INTO
devices
{}
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,)
@staticmethod
@with_psql
def delete(cur, appkey, dev_id):
tn = 'devices_' +str(appkey)
query = """
DELETE FROM
devices
{}
WHERE
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (appkey, dev_id))
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
devices
SELECT * FROM
{}
WHERE
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (appkey, dev_id))
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)), [dev_id])
dev = cur.fetchone()
if (dev is None):
......@@ -78,12 +133,12 @@ class DeviceDao:
@staticmethod
@with_psql
def get_list(cur, appkey):
tn = 'devices_' +str(appkey)
query = """
SELECT * FROM
devices
WHERE
app_key = %s
SELECT * FROM
{}
"""
cur.execute(query, (appkey,))
cur.execute(
sql.SQL(query).format(sql.Identifier(tn)))
return (True, cur.fetchall())
No preview for this file type
......@@ -6,6 +6,7 @@ import dao.user.user as ud
import dao.application.application as ad
import dao.device.device as dd
APP_KEY_LEN = 8
server = Flask(__name__, template_folder='templates/')
......@@ -109,21 +110,38 @@ def app():
error = 'Application name cannot be empty.'
return render_template('new-app.html', feedback=error)
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]:
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]:
# return render_template('new-app.html', feedback=str(res[1])+'|'+str(rer[1]))
#else:
dh = dd.DeviceDao()
res = dh.create_table(appkey)
if not res[0]:
ah.delete(appkey)
return render_template('new-app.html', feedback=res[1])
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')
def new_dev():
......@@ -145,17 +163,23 @@ def new_dev():
def dev():
dh = dd.DeviceDao()
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'
return render_template('dev.html', dev=dev[1], ltup=ltup)
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'])
if not res[0]:
return render_template('add-dev.html', feedback=res[1])
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__':
......
......@@ -14,7 +14,7 @@
{% if devs %}
<ol>
{% 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 %}
</ol>
{% else %}
......
......@@ -8,8 +8,8 @@
<div class="col-md-6 col-md-offset-3">
<p>Name: {{ dev[0] }} </p>
<p>dev_id: {{ dev[1] }} </p>
<p>app-key: {{ dev[2] }} </p>
<p>Description : {{ dev[3] }} </p>
<p>app-key: {{ appkey }} </p>
<p>Description : {{ dev[2] }} </p>
<p>Last time up : {{ ltup }} </p>
<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