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

start sql db file, configuration improved

parent efa82e9f
......@@ -11,3 +11,30 @@ def create(cur, appkey, devid, msg):
cur.execute(query, (appkey, devid, msg))
return (True,)
@with_psql
def get_list(cur, appkey, devid):
query = """
SELECT * FROM
pend_msgs
WHERE
app_key = %s
AND
dev_id = %s
"""
cur.execute(query, (appkey, devid))
return (True, cur.fetchall())
@with_psql
def delete(cur, appkey, devid, msg):
query = """
DELETE FROM
pend_msgs
WHERE
app_key = %s
AND
dev_id = %s
AND
msg LIKE %s
"""
cur.execute(query, (appkey, devid, msg))
return (True,)
......@@ -2,7 +2,6 @@ from app import app
from binascii import hexlify
import os
import psycopg2
import json
def rand_str(length):
if length % 2 == 0:
......@@ -11,7 +10,7 @@ def rand_str(length):
return hexlify(os.urandom(length//2 + 1))
def prep_id_range(devlist):
r = range(1,255)
r = list(range(1,255))
for dev in devlist:
del r[r.index(dev[1])]
......@@ -43,16 +42,14 @@ def prep_id_range(devlist):
return s
# decorator implementation
def with_psql(f):
def _with_psql(*args, **kwargs):
#db_conf = read_json_file('db.conf')
conn = psycopg2.connect(
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']
database = app.config['DB_NAME'],
user = app.config['DB_USERNAME'],
password = app.config['DB_PASSWORD'],
host = app.config['DB_HOST'],
port = app.config['DB_PORT']
)
cur = conn.cursor()
......@@ -69,13 +66,3 @@ def with_psql(f):
return res
return _with_psql
def read_json_file(path):
json_dict = None
try:
with open(path) as json_file:
json_dict = json.load(json_file)
except Exception as e:
print("{} : {}".format(path, e))
return json_dict
......@@ -27,6 +27,35 @@
<p class="text-danger float-right">{{ feedback }}</p>
{% endif %}
</form>
<br>
{% if config_list %}
<div>
<table class="table">
<thead>
<th> Config ID </th>
<th> Arguments </th>
<th> Acknowledged </th>
<th> </th>
</thead>
<tbody>
{% for c in config_list %}
{% if c[2] %}
<tr class="success">
{% else %}
<tr class="warning">
{% endif %}
<th> {{ c[0] }} </th>
<th> {{ c[1] }} </th>
<th> {{ c[2] }} </th>
<th> <a href="/dev-conf-rm?conf={{ c[3] }}"> <span class="glyphicon glyphicon-remove"</span> </a> </th>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<center> No configuration messages. </center>
{% endif %}
</div>
</div>
......
......@@ -11,6 +11,7 @@ import app.dao.data.data as data
import app.helpers.misc as misc
import binascii
import os
......@@ -201,7 +202,7 @@ def dev():
dd.delete(session['appkey'], request.form['devid'])
return render_template('public/add-dev.html', feedback=res[1])
else:
return redirect(url_for('app', appkey=session['appkey']))
return redirect(url_for('app_', appkey=session['appkey']))
else:
return redirect(url_for('index'))
......@@ -210,15 +211,28 @@ def dev():
def dev_conf():
if 'name' in session and 'devid' in session:
if request.method == 'GET':
return render_template('public/dev-conf.html', devname=session['devname'])
pend_msgs = pend.get_list(session['appkey'], session['devid'])
if pend_msgs[0]:
config_list = []
for pm in pend_msgs[1]:
cntt = binascii.a2b_base64(pm[2])
config_id = int(cntt[0])
config_args = cntt[2:(len(cntt)-1)].decode('utf-8')
ack = pm[3]
config_list.append((config_id, config_args, ack, pm[2]))
return render_template('public/dev-conf.html', devname=session['devname'], config_list=config_list)
else:
return render_template('public/dev-conf.html', devname=session['devname'])
else:
argslen = len(request.form['arg']) + 1
args = bytearray(argslen + 2)
args[0] = int(request.form['confid'])
args[1] = argslen
bstr = bytes(request.form['arg'])
bstr = bytes(request.form['arg'].encode('utf-8'))
i = 0
while i < argslen - 1:
args[2+i] = bstr[i]
......@@ -237,6 +251,19 @@ def dev_conf():
else:
return redirect(url_for('index'))
@app.route('/dev-conf-rm')
def dev_conf_rm():
if 'name' in session and 'appkey' in session and 'devid' in session:
res = pend.delete(session['appkey'], session['devid'], request.args.get('conf')+'_')
if res[0]:
flash('Configuration message successfully removed.','success')
return redirect(url_for('dev_conf'))
else:
flash('Error removing configuration message: {}'.format(res[1]), 'danger')
return redirect(url_for('dev_conf'))
else:
return redirect(url_for('index'))
@app.route('/delete-dev')
def delete_dev():
......
--
-- PostgreSQL database dump
--
-- Dumped from database version 9.6.17
-- Dumped by pg_dump version 9.6.17
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: applications; Type: TABLE; Schema: public; Owner: pi
--
CREATE TABLE public.applications (
name character varying(30) NOT NULL,
app_key character varying(30) NOT NULL,
username character varying(30),
description character varying(200)
);
-- ALTER TABLE public.applications OWNER TO pi;
--
-- Name: pend_msgs; Type: TABLE; Schema: public; Owner: pi
--
CREATE TABLE public.pend_msgs (
app_key character varying(30) NOT NULL,
dev_id numeric(3,0) NOT NULL,
msg character varying(150) NOT NULL,
ack boolean DEFAULT false NOT NULL
);
-- ALTER TABLE public.pend_msgs OWNER TO pi;
--
-- Name: users; Type: TABLE; Schema: public; Owner: pi
--
CREATE TABLE public.users (
name character varying(30) NOT NULL,
password character varying(100) NOT NULL
);
-- ALTER TABLE public.users OWNER TO pi;
--
-- Name: applications applications_app_key_key; Type: CONSTRAINT; Schema: public; Owner: pi
--
ALTER TABLE ONLY public.applications
ADD CONSTRAINT applications_app_key_key UNIQUE (app_key);
--
-- Name: applications applications_pkey; Type: CONSTRAINT; Schema: public; Owner: pi
--
ALTER TABLE ONLY public.applications
ADD CONSTRAINT applications_pkey PRIMARY KEY (name, app_key);
--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: pi
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (name);
--
-- Name: applications applications_username_fkey; Type: FK CONSTRAINT; Schema: public; Owner: pi
--
ALTER TABLE ONLY public.applications
ADD CONSTRAINT applications_username_fkey FOREIGN KEY (username) REFERENCES public.users(name);
--
-- PostgreSQL database dump complete
--
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