Commit 6b335653 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

app dao changed

parent 4040f3f9
import psycopg2
import bcrypt
from misc import with_psql
class ApplicationDao:
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, appkey, username, desc):
@with_psql
def create(cur, name, appkey, username, desc):
query = """
INSERT INTO
applications
......@@ -40,9 +12,8 @@ class ApplicationDao:
return (True,)
@staticmethod
@with_psql
def delete(cur, appkey):
@with_psql
def delete(cur, appkey):
query = """
DELETE FROM
applications
......@@ -53,9 +24,8 @@ class ApplicationDao:
return (True,)
@staticmethod
@with_psql
def get(cur, appkey):
@with_psql
def get(cur, appkey):
query = """
SELECT * FROM
applications
......@@ -71,9 +41,8 @@ class ApplicationDao:
return (True, app)
@staticmethod
@with_psql
def get_list(cur, username):
@with_psql
def get_list(cur, username):
query = """
SELECT * FROM
applications
......@@ -84,9 +53,8 @@ class ApplicationDao:
return (True, cur.fetchall())
@staticmethod
@with_psql
def update(cur, appkey, name, desc):
@with_psql
def update(cur, appkey, name, desc):
query = """
UPDATE
applications
......
......@@ -39,3 +39,25 @@ def prep_id_range(devlist):
s += str(r[-1])+']'
return s
import psycopg2
# 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
No preview for this file type
......@@ -19,8 +19,7 @@ server = Flask(__name__, template_folder='templates/')
@server.route('/')
def index():
if 'name' in session and len(session['name']) > 0:
ah = ad.ApplicationDao()
apps = ah.get_list(session['name'].encode('utf-8'))
apps = ad.get_list(session['name'].encode('utf-8'))
session.pop('appkey', None)
# print('apps: ', apps)
......@@ -98,13 +97,12 @@ def new_application():
@server.route('/app', methods=['GET', 'POST'])
def app():
if 'name' in session:
ah = ad.ApplicationDao()
if request.method == 'GET':
dh = dd.DeviceDao()
session['appkey'] = request.args.get('appkey')
app = ah.get(session['appkey'])
app = ad.get(session['appkey'])
devs = dh.get_list(app[1][1])
try:
......@@ -122,7 +120,7 @@ def app():
return render_template('new-app.html', feedback=error)
else:
appkey = misc.rand_str(APP_KEY_LEN)
res = ah.create(request.form['appname'], appkey, session['name'], request.form['appdesc'])
res = ad.create(request.form['appname'], appkey, session['name'], request.form['appdesc'])
if not res[0]:
return render_template('new-app.html', feedback=res[1])
......@@ -131,7 +129,7 @@ def app():
res = dh.create_table(appkey)
if not res[0]:
ah.delete(appkey)
ad.delete(appkey)
return render_template('new-app.html', feedback=res[1])
return redirect(url_for('index'))
......@@ -149,8 +147,7 @@ def delete_app():
dh.delete_table(session['appkey'])
ah = ad.ApplicationDao()
res = ah.delete(session['appkey'])
res = ad.delete(session['appkey'])
if not res[0]:
return redirect(url_for('app'))
......
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