Commit ff4a13ed authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

major changes

parent 51f76a47
Pipeline #38 failed with stages
from app import app
from binascii import hexlify
import os
import psycopg2
......@@ -45,13 +46,13 @@ def prep_id_range(devlist):
# decorator implementation
def with_psql(f):
def _with_psql(*args, **kwargs):
db_conf = read_json_file('db.conf')
#db_conf = read_json_file('db.conf')
conn = psycopg2.connect(
database = db_conf['name'],
user = db_conf['user'],
password = db_conf['password'],
host = db_conf['host'],
port = db_conf['port']
database = app.config['DB_NAME'],#db_conf['name'],
user = app.config['DB_USERNAME'],#db_conf['user'],
password = app.config['DB_PASSWORD'],#db_conf['password'],
host = app.config['DB_HOST'],#db_conf['host'],
port = app.config['DB_PORT']#db_conf['port']
)
cur = conn.cursor()
......@@ -74,7 +75,7 @@ def read_json_file(path):
try:
with open(path) as json_file:
json_dict = json.load(json_file)
except Exception, e:
except Exception as e:
print("{} : {}".format(path, e))
return json_dict
......@@ -6,7 +6,7 @@
<title> {% block title %} {% endblock %} </title>
<meta name="viewport" content="with-device-width, initial-scale-1"/>
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="static/custom.css"/>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/custom.css') }}"/>
</head>
<body>
<nav class="navbar navbar-inverse">
......@@ -43,5 +43,6 @@
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
{% block script %} {% endblock %}
</body>
</html>
......@@ -23,7 +23,11 @@
{% for d in data %}
<tr>
<th> {{ d[1] }} </th>
<th> {{ d[2] }} </th>
<th>
{% for k in d[2] %}
{{ k }} : {{ d[2][k] }} <br>
{% endfor %}
</th>
</tr>
{% endfor %}
</tbody>
......
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
from app import app
from flask import render_template, request, redirect, url_for, session, send_from_directory
import psycopg2
import dao.user.user as ud
import dao.application.application as ad
import dao.device.device as dd
import dao.pend.pend as pend
import dao.data.data as data
import app.dao.user.user as ud
import app.dao.application.application as ad
import app.dao.device.device as dd
import app.dao.pend.pend as pend
import app.dao.data.data as data
import misc
import app.helpers.misc as misc
import os
APP_KEY_LEN = 8
DATA_DOWNLOAD_DIR = 'data'
server = Flask(__name__, template_folder='templates/')
@server.route('/')
@app.route('/')
def index():
if 'name' in session and len(session['name']) > 0:
apps = ad.get_list(session['name'].encode('utf-8'))
apps = ad.get_list(session['name'])
session.pop('appkey', None)
# print('apps: ', apps)
if apps[0]:
return render_template('index.html', apps=apps[1])
return render_template('public/index.html', apps=apps[1])
else:
return render_template('index.html', feedback=apps[1])
return render_template('public/index.html', feedback=apps[1])
else:
return render_template('index.html')
return render_template('public/index.html')
@server.route('/signup', methods=['GET', 'POST'])
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'GET':
return render_template('signup.html')
return render_template('public/signup.html')
else:
username = request.form['username']
password = request.form['password'].encode('utf-8')
if (username == '' or password == ''):
feedback = 'Username or password fields cannot be empty'
return render_template('signup.html', feedback=feedback)
return render_template('public/signup.html', feedback=feedback)
else:
res = ud.create(username, password)
if (not res[0]):
return render_template('signup.html', feedback=res[1])
return render_template('public/signup.html', feedback=res[1])
else:
session['name'] = username
......@@ -55,21 +52,21 @@ def signup():
@server.route('/login', methods=['GET', 'POST'])
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
return render_template('public/login.html')
else:
username = request.form['username']
password = request.form['password'].encode('utf-8')
if (username == '' or password == ''):
feedback = 'Username or password fields cannot be empty'
return render_template('login.html', feedback=feedback)
return render_template('public/login.html', feedback=feedback)
else:
res = ud.get(username, password)
if (not res[0]):
return render_template('login.html', feedback=msg[1])
return render_template('public/login.html', feedback=msg[1])
else:
session['name'] = username
......@@ -77,63 +74,63 @@ def login():
@server.route('/logout')
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('index'))
@server.route('/new-app')
@app.route('/new-app')
def new_application():
if 'name' in session:
return render_template('new-app.html')
return render_template('public/new-app.html')
else:
return redirect(url_for('index'))
@server.route('/app', methods=['GET', 'POST'])
def app():
@app.route('/app', methods=['GET', 'POST'])
def app_():
if 'name' in session:
if request.method == 'GET':
session['appkey'] = request.args.get('appkey')
app = ad.get(session['appkey'])
devs = dd.get_list(app[1][1])
ap = ad.get(session['appkey'])
devs = dd.get_list(ap[1][1])
try:
filelist = [f for f in os.listdir(DATA_DOWNLOAD_DIR) if f.startswith(session['appkey'])]
filelist = [f for f in os.listdir(app.config['DATA_DOWNLOAD_DIR']) if f.startswith(session['appkey'])]
for f in filelist:
os.remove(DATA_DOWNLOAD_DIR+'/'+f)
os.remove(app.config['DATA_DOWNLOAD_DIR']+'/'+f)
except OSError:
pass
# print('devs : ', devs)
return render_template('app.html', app=app[1], devs=devs[1])
return render_template('public/app.html', app=ap[1], devs=devs[1])
else:
if request.form['appname'] == '':
error = 'Application name cannot be empty.'
return render_template('new-app.html', feedback=error)
return render_template('public/new-app.html', feedback=error)
else:
appkey = misc.rand_str(APP_KEY_LEN)
appkey = misc.rand_str(app.config['APPKEY_LENGTH'])
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])
return render_template('public/new-app.html', feedback=res[1])
res = dd.create_table(appkey)
if not res[0]:
ad.delete(appkey)
return render_template('new-app.html', feedback=res[1])
return render_template('public/new-app.html', feedback=res[1])
return redirect(url_for('index'))
else:
return redirect(url_for('index'))
@server.route('/delete-app')
@app.route('/delete-app')
def delete_app():
if 'name' in session:
devs = dd.get_list(session['appkey'])
......@@ -146,14 +143,14 @@ def delete_app():
res = ad.delete(session['appkey'])
if not res[0]:
return redirect(url_for('app'))
return redirect(url_for('app_'))
else:
return redirect(url_for('index'))
else:
return redirect(url_for('index'))
@server.route('/add-dev')
@app.route('/add-dev')
def new_dev():
if 'name' in session:
dev_list = dd.get_list(session['appkey'])
......@@ -161,21 +158,21 @@ def new_dev():
#print('dev list : ', dev_list)
if not dev_list[0]:
return render_template('add-dev.html', feedback=dev_list[1])
return render_template('public/add-dev.html', feedback=dev_list[1])
else:
return render_template('add-dev.html', free_ids=misc.prep_id_range(dev_list[1]))
return render_template('public/add-dev.html', free_ids=misc.prep_id_range(dev_list[1]))
else:
return redirect(url_for('index'))
@server.route('/dev', methods=['GET', 'POST'])
@app.route('/dev', methods=['GET', 'POST'])
def dev():
if 'name' in session:
if request.method == 'GET':
dev = dd.get(session['appkey'], request.args.get('id'))
session['devid'] = dev[1][1]
session['devid'] = int(dev[1][1])
session['devname'] = dev[1][0]
last = data.get_last_n(session['appkey'], session['devid'], 1)
......@@ -185,29 +182,29 @@ def dev():
if last[0]:
ltup = last[1][0][1]
return render_template('dev.html', dev=dev[1], appkey=session['appkey'], ltup=ltup)
return render_template('public/dev.html', dev=dev[1], appkey=session['appkey'], ltup=ltup)
else:
res = dd.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])
return render_template('public/add-dev.html', feedback=res[1])
else:
res = data.create_table(session['appkey'], request.form['devid'])
if not res[0]:
dd.delete(session['appkey'], request.form['devid'])
return render_template('add-dev.html', feedback=res[1])
return render_template('public/add-dev.html', feedback=res[1])
else:
return redirect(url_for('app', appkey=session['appkey']))
else:
return redirect(url_for('index'))
@server.route('/dev-conf', methods=['GET', 'POST'])
@app.route('/dev-conf', methods=['GET', 'POST'])
def dev_conf():
if 'name' in session and 'devid' in session:
if request.method == 'GET':
return render_template('dev-conf.html', devname=session['devname'])
return render_template('public/dev-conf.html', devname=session['devname'])
else:
argslen = len(request.form['arg']) + 1
......@@ -235,18 +232,18 @@ def dev_conf():
return redirect(url_for('index'))
@server.route('/delete-dev')
@app.route('/delete-dev')
def delete_dev():
if 'name' in session and 'devid' in session:
data.delete_table(session['appkey'], session['devid'])
res = dd.delete(session['appkey'], session['devid'])
return redirect(url_for('app', appkey=session['appkey']))
return redirect(url_for('app_', appkey=session['appkey']))
else:
return redirect(utl_for('index'))
@server.route('/dev-data')
@app.route('/dev-data')
def dev_data():
if 'name' in session and 'devid' in session:
last = data.get_last_n(session['appkey'], session['devid'], 10)
......@@ -256,23 +253,24 @@ def dev_data():
if count[1][0] < 10:
last_ctr = count[1][0]
#print(last)
#print(last[1][2][2])
#print(type(last[1][2][2]))
#print(count)
if count[1][0] > 0:
return render_template('dev-data.html', data=last[1], total=count[1][0], lastctr=last_ctr, devname=session['devname'])
return render_template('public/dev-data.html', data=last[1], total=count[1][0], lastctr=last_ctr, devname=session['devname'])
else:
return render_template('dev-data.html', devname=session['devname'])
return render_template('public/dev-data.html', devname=session['devname'])
else:
return redirect(utl_for('index'))
@server.route('/data-csv')
@app.route('/data-csv')
def data_csv():
if 'name' in session and 'devid' in session:
dumpd = data.get_all(session['appkey'], session['devid'])
fn = session['appkey']+ '_' +str(session['devid'])+ '.csv'
with open(DATA_DOWNLOAD_DIR+'/'+fn, 'w') as f:
with open(app.config['DATA_DOWNLOAD_DIR']+'/'+fn, 'w') as f:
for d in dumpd[1][0][2]:
f.write(d)
f.write(',')
......@@ -284,13 +282,6 @@ def data_csv():
f.write(',')
f.write('\n')
return send_from_directory(DATA_DOWNLOAD_DIR, fn, as_attachment=True)
return send_from_directory(app.config['DATA_DOWNLOAD_DIR'], fn, as_attachment=True)
else:
return redirect(utl_for('index'))
if __name__ == '__main__':
server.secret_key = 'sdjfklsjf^$654sd^#sPH'
server.run(debug = True, host='0.0.0.0')
File added
class Config(object):
DEBUG = False
TESTING = False
SECRET_KEY = b'ksj^*(s90*Dklds;osdj'
DB_NAME = 'gateway'
DB_USERNAME = 'pi'
DB_PASSWORD = 'dev'
DB_HOST = 'localhost'
DB_PORT = 5432
SESSION_COOKIE_SECURE = True
APPKEY_LENGTH = 8
DATA_DOWNLOAD_DIR = 'data'
class ProductionConfig(Config):
pass
class DevelopmentConfig(Config):
DEBUG = True
SESSION_COOKIE_SECURE = False
class TestingConfig(Config):
TESTING = True
SESSION_COOKIE_SECURE = False
File added
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
if [ "${BASH_SOURCE-}" = "$0" ]; then
echo "You must source this script: \$ source $0" >&2
exit 33
fi
deactivate () {
unset -f pydoc >/dev/null 2>&1
# reset old environment variables
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all
if ! [ -z "${_OLD_VIRTUAL_PATH:+_}" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
if [ ! "${1-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
# unset irrelevant variables
deactivate nondestructive
VIRTUAL_ENV='/home/pi/thso.server/env'
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH
# unset PYTHONHOME if set
if ! [ -z "${PYTHONHOME+_}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1-}"
if [ "x" != x ] ; then
PS1="${PS1-}"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
fi
export PS1
fi
# Make sure to unalias pydoc if it's already there
alias pydoc 2>/dev/null >/dev/null && unalias pydoc || true
pydoc () {
python -m pydoc "$@"
}
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
hash -r 2>/dev/null
fi
# This file must be used with "source bin/activate.csh" *from csh*.
# You cannot run it directly.
# Created by Davide Di Blasi <davidedb@gmail.com>.
set newline='\
'
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH:q" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT:q" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate && unalias pydoc'
# Unset irrelevant variables.
deactivate nondestructive
setenv VIRTUAL_ENV '/home/pi/thso.server/env'
set _OLD_VIRTUAL_PATH="$PATH:q"
setenv PATH "$VIRTUAL_ENV:q/bin:$PATH:q"
if ('' != "") then
set env_name = ''
else
set env_name = '('"$VIRTUAL_ENV:t:q"') '
endif
if ( $?VIRTUAL_ENV_DISABLE_PROMPT ) then
if ( $VIRTUAL_ENV_DISABLE_PROMPT == "" ) then
set do_prompt = "1"
else
set do_prompt = "0"
endif
else
set do_prompt = "1"
endif
if ( $do_prompt == "1" ) then
# Could be in a non-interactive environment,
# in which case, $prompt is undefined and we wouldn't
# care about the prompt anyway.
if ( $?prompt ) then
set _OLD_VIRTUAL_PROMPT="$prompt:q"
if ( "$prompt:q" =~ *"$newline:q"* ) then
:
else
set prompt = "$env_name:q$prompt:q"
endif
endif
endif
unset env_name
unset do_prompt
alias pydoc python -m pydoc
rehash
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