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

app dao changed

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