Commit 7b46acea authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

applications DAO added

parent 4bba2691
import psycopg2
import bcrypt
from misc import rand_str
APP_KEY_LEN = 8
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, username, desc):
query = """
INSERT INTO
applications
VALUES
(%s, %s, %s, %s)
"""
cur.execute(query, (name, rand_str(APP_KEY_LEN), username, desc))
return (True,)
@staticmethod
@with_psql
def delete(cur, appkey):
query = """
DELETE FROM
applications
WHERE
app_key = %s
"""
cur.execute(query, (appkey,))
return (True,)
@staticmethod
@with_psql
def get(cur, appkey):
query = """
SELECT * FROM
applications
WHERE
app_key = %s
"""
cur.execute(query, (appkey,))
app = cur.fetchone()
if app is None:
return (False, 'Application with key {} does not exist'.format(appkey))
else:
return (True, app)
@staticmethod
@with_psql
def get_list(cur, username):
query = """
SELECT * FROM
applications
WHERE
username = %s
"""
cur.execute(query, (username,))
return (True, cur.fetchall())
@staticmethod
@with_psql
def update(cur, appkey, name, desc):
query = """
UPDATE
applications
SET
name = %s,
description = %s,
WHERE
app_key = %s
"""
cur.execute(query, (name, desc, appkey))
return (True,)
...@@ -3,6 +3,7 @@ import psycopg2 ...@@ -3,6 +3,7 @@ import psycopg2
import bcrypt import bcrypt
import misc import misc
import dao.user.user as ud import dao.user.user as ud
import dao.application.application as ad
APP_KEY_LEN = 8 APP_KEY_LEN = 8
...@@ -11,80 +12,6 @@ APP_KEY_LEN = 8 ...@@ -11,80 +12,6 @@ APP_KEY_LEN = 8
server = Flask(__name__, template_folder='templates/') server = Flask(__name__, template_folder='templates/')
def get_apps(username):
res = []
try:
conn = psycopg2.connect('dbname=gateway')
cur = conn.cursor()
query = """
SELECT * FROM
applications
WHERE
username = %s
"""
cur.execute(query, (username,))
res = cur.fetchall()
except (Exception, psycopg2.DatabaseError) as error:
print('Error querying applications: ', error)
finally:
if (conn):
cur.close()
conn.close()
return res
def get_app(appkey):
res = []
try:
conn = psycopg2.connect('dbname=gateway')
cur = conn.cursor()
query = """
SELECT * FROM
applications
WHERE
app_key = %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
def new_app(name, desc):
suc = (True, 'App created')
try:
conn = psycopg2.connect('dbname=gateway')
cur = conn.cursor()
query = """
INSERT INTO
applications
VALUES
(%s, %s, %s, %s)
"""
cur.execute(query, (name, misc.rand_str(APP_KEY_LEN), session['name'], desc))
conn.commit()
print('App created')
except (Exception, psycopg2.DatabaseError) as error:
print('Error creating app: ', error)
suc = (False, error)
finally:
if (conn):
cur.close()
conn.close()
return suc
def new_app_devs(appkey): def new_app_devs(appkey):
suc = (True, 'app_devs created') suc = (True, 'app_devs created')
...@@ -144,9 +71,10 @@ def get_devs(appkey): ...@@ -144,9 +71,10 @@ def get_devs(appkey):
@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:
apps = get_apps(session['name'].encode('utf-8')) ah = ad.ApplicationDao()
apps = ah.get_list(session['name'].encode('utf-8'))
print('apps: ', apps) print('apps: ', apps)
return render_template('index.html', apps=apps) return render_template('index.html', apps=apps[1])
return render_template('index.html') return render_template('index.html')
...@@ -213,8 +141,9 @@ def new_application(): ...@@ -213,8 +141,9 @@ def new_application():
@server.route('/app', methods=['GET', 'POST']) @server.route('/app', methods=['GET', 'POST'])
def app(): def app():
ah = ad.ApplicationDao()
if request.method == 'GET': if request.method == 'GET':
app = get_app(request.form['appkey']) app = ah.get(request.form['appkey'])
devs = get_devs(app[1]) devs = get_devs(app[1])
return render_template('app.html', app=app, devs=devs) return render_template('app.html', app=app, devs=devs)
...@@ -223,19 +152,20 @@ def app(): ...@@ -223,19 +152,20 @@ 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 = new_app(request.form['appname'], request.form['appdesc']) res = ah.create(request.form['appname'], 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']) #res = new_app_devs(request.form['appname'])
if not res[0]: #if not res[0]:
rm_app(request.form['appname']) # rm_app(request.form['appname'])
return render_template('new-app.html', feedback=res[1]) # return render_template('new-app.html', feedback=res[1])
if not res[0] or not rer[0]: #if not res[0] or not rer[0]:
return render_template('new-app.html', feedback=str(res[1])+'|'+str(rer[1])) # return render_template('new-app.html', feedback=str(res[1])+'|'+str(rer[1]))
else: #else:
return redirect(url_for('index')) return redirect(url_for('index'))
if __name__ == '__main__': if __name__ == '__main__':
......
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