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

administration view added, flash msg fixed, access level custom decorator added

parent 89a3d682
......@@ -11,4 +11,5 @@ else:
mail = Mail(app)
from app import views
from app import views_admin
from app.helpers import maintainer
......@@ -12,19 +12,22 @@ def get_user_data_count(cur, username):
for a in apps:
devs.append(dd.get_list(a[1])[1])
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT COUNT(*) FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-9]
query += ') SELECT SUM(count) FROM t'
cur.execute(query, ())
return (True,cur.fetchone())
if apps != [] and devs != []:
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT COUNT(*) FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-9]
query += ') SELECT SUM(count) FROM t'
cur.execute(query, ())
return (True,cur.fetchone())
else:
return (True,(0,))
@with_psql
......@@ -35,21 +38,24 @@ def get_user_data_count_per_hour(cur, username, hour):
for a in apps:
devs.append(dd.get_list(a[1])[1])
utc_hour = utc_roundhour(hour)
if apps != [] and devs != []:
utc_hour = utc_roundhour(hour)
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {}'.format(utc_hour, utc_hour+60*60)
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {}'.format(utc_hour, utc_hour+60*60)
#print(query)
cur.execute(query, ())
return (True,cur.fetchone())
#print(query)
cur.execute(query, ())
return (True,cur.fetchone())
else:
return (True, (0,))
@with_psql
......@@ -62,22 +68,26 @@ def get_user_data_count_per_hour_period(cur, username, period):
utc_hour = [utc_roundhour(x) for x in range(period,-1,-1)]
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT * FROM ('
for uh in utc_hour:
query +=' SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {} UNION ALL'.format(uh, uh+60*60)
query = query[0:-9]
query += ') w'
if apps != [] and devs != []:
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT * FROM ('
for uh in utc_hour:
query +=' SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {} UNION ALL'.format(uh, uh+60*60)
query = query[0:-9]
query += ') w'
cur.execute(query, ())
return (True,cur.fetchall())
else:
return (True, (0,))
cur.execute(query, ())
return (True,cur.fetchall())
@with_psql
......@@ -88,20 +98,23 @@ def get_user_data_count_per_day(cur, username, day=0):
for a in apps:
devs.append(dd.get_list(a[1])[1])
utc_day = utc_roundday(day)
if apps != [] and devs != []:
utc_day = utc_roundday(day)
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {}'.format(utc_day, utc_day+24*60*60)
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {}'.format(utc_day, utc_day+24*60*60)
cur.execute(query, ())
return (True,cur.fetchone())
cur.execute(query, ())
return (True,cur.fetchone())
else:
return (True, (0,))
@with_psql
......@@ -112,24 +125,27 @@ def get_user_data_count_per_day_period(cur, username, period):
for a in apps:
devs.append(dd.get_list(a[1])[1])
utc_hour = [utc_roundday(x) for x in range(period,-1,-1)]
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT * FROM ('
for uh in utc_hour:
query +=' SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {} UNION ALL'.format(uh, uh+24*60*60)
query = query[0:-9]
query += ') w'
cur.execute(query, ())
return (True,cur.fetchall())
if apps != [] and devs != []:
utc_hour = [utc_roundday(x) for x in range(period,-1,-1)]
query = 'WITH t AS ('
i = 0
for a in apps:
for d in devs[i]:
query += 'SELECT utc FROM dev_{}_{} UNION ALL '.format(a[1], d[1])
i += 1
query = query[0:-10]
query += ') SELECT * FROM ('
for uh in utc_hour:
query +=' SELECT COUNT(*) FROM t WHERE utc > {} AND utc < {} UNION ALL'.format(uh, uh+24*60*60)
query = query[0:-9]
query += ') w'
cur.execute(query, ())
return (True,cur.fetchall())
else:
return (True, (0,))
@with_psql
......
This diff is collapsed.
This diff is collapsed.
from app import app
from flask import session,flash,redirect,url_for
from binascii import hexlify
import os
import psycopg2
import binascii
from datetime import datetime
from functools import wraps
def rand_str(length):
if length % 2 == 0:
......@@ -75,6 +77,19 @@ def with_psql(f):
return res
return _with_psql
def restricted(access_level):
def user_control(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'role' in session and session['role'] != access_level:
flash('Access level "{}" required for this page.'.format(access_level), 'danger')
return redirect(url_for('index'))
return f(*args, **kwargs)
return decorated_function
return user_control
def clean_data_folder():
try:
filelist = [f for f in os.listdir(app.config['DATA_DOWNLOAD_DIR_OS'])]
......
......@@ -172,7 +172,7 @@
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="col-md-6" style="position: absolute; top:25%; right:30px;">
<div class="col-md-6" style="position: absolute; top:17%; right:30px;">
<div class="alert alert-{{ category }} alert-dismissible show fade" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
......
{% extends 'logged_layout.html' %}
{% block title %} Administration - HPC&amp;A IoT {% endblock %}
{% block location %}
<a class="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block" href="/administration">Administration</a>
{% endblock %}
{% block body %}
<!-- Page content -->
<div class="container-fluid mt--7">
<!-- Table -->
<div class="row">
<div class="col">
<div class="card shadow">
<div class="card-header bg-transparent">
<h3 class="mb-0">Server Administration</h3>
</div>
<div class="card-body">
<form action="/administration" method="post" id="administration">
<div class="custom-control custom-control-alternative custom-checkbox">
<input class="custom-control-input" id="signup" type="checkbox" name="signup" {% if config['USERS_SIGNUP'] %} checked {% endif %}>
<label class="custom-control-label" for="signup">
<span class="text-muted">Allow users autonomous registration</span>
</label>
</div>
<br>
</form>
<div class="row" style="margin-top: 30px;">
<div class="col-lg-3">
<button type="submit" form="administration" class="btn btn-primary btn-block">Save</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
......@@ -16,6 +16,7 @@
</div>
<div class="card-body">
<div class="row">
{% if apps %}
{% for a in apps %}
<div class="col-lg-6 col-md-6" style="margin-bottom : 30px;">
<div class="card">
......@@ -27,6 +28,11 @@
</div>
</div>
{% endfor %}
{% else %}
<div class="col-lg-12">
<center><p> There are no created applications. </p></center>
</div>
{% endif %}
</div>
<div class="row" style="margin-top: 30px;">
......
......@@ -96,7 +96,7 @@
<p> Total: <strong> {{ total }} </strong> messages. </p>
<table class="table" id="table_{{ k }}">
<thead>
<th> Time </th>
<th> Timestamp </th>
<th> {{ k }} </th>
</thead>
<tbody id="table_{{ k }}_body">
......
......@@ -34,7 +34,7 @@
</div>
<!-- Navbar items -->
<ul class="navbar-nav ml-auto">
{% if users_signup %}
{% if config['USERS_SIGNUP'] %}
<li class="nav-item">
<a class="nav-link nav-link-icon" href="/register">
<i class="ni ni-circle-08"></i>
......@@ -113,7 +113,7 @@
<a href="#" class="text-light"><small>Forgot password?</small></a>
</div>
<div class="col-6 text-right">
{% if users_signup %}
{% if config['USERS_SIGNUP'] %}
<a href="/register" class="text-light"><small>Create new account</small></a>
{% endif %}
</div>
......
......@@ -34,7 +34,7 @@
</div>
<!-- Navbar items -->
<ul class="navbar-nav ml-auto">
{% if users_signup %}
{% if config['USERS_SIGNUP'] %}
<li class="nav-item">
<a class="nav-link nav-link-icon" href="/register">
<i class="ni ni-circle-08"></i>
......
......@@ -612,8 +612,8 @@ def data_csv():
return redirect(utl_for('index'))
@app.route('/administration', methods=['GET', 'POST'])
def administration():
@app.route('/administration_old', methods=['GET', 'POST'])
def administration_old():
if 'role' in session and session['role'] == 'admin':
user_cnt = ud.get_count()
apps_cnt = ad.get_count()
......
from app import app
from flask import render_template, request, redirect, url_for, session, flash
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 app.dao.notification.notification as nfs
import app.dao.trigger.trigger as tr
import app.dao.notification_queue.notification_queue as nq
import app.dao.misc.misc as md
#import app.helpers.misc as misc
from app.helpers.misc import restricted
#import binascii
MAX_PG = 5
MAX_PG_ENTRIES_USERS = 10
MAX_PG_ENTRIES_DATA = 10
MAX_PG_ENTRIES_GRAPH_HOURS = 24
@app.route('/administration', methods=['GET', 'POST'])
@restricted(access_level='admin')
def administration():
if request.method == 'GET':
user_cnt = ud.get_count()[1][0]
apps_cnt = ad.get_count()[1][0]
devs_cnt = dd.get_count_all()
info = [user_cnt, apps_cnt, devs_cnt]
return render_template('new/admin/administration.html', info=info)
elif request.method == 'POST':
if request.form.getlist('signup') and request.form.getlist('signup')[0] == 'on':
app.config['USERS_SIGNUP'] = True
else:
app.config['USERS_SIGNUP'] = False
return redirect(request.url)
......@@ -2,7 +2,7 @@ class Config(object):
DEBUG = False
TESTING = False
SECRET_KEY = b'ksj^*(s90*Dklds;osdj'
SECRET_KEY = 'al1DuE8cFpk3EJNlFHG73Fd'
DB_NAME = 'iotserver'
DB_USERNAME = 'pi'
......
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