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: ...@@ -11,4 +11,5 @@ else:
mail = Mail(app) mail = Mail(app)
from app import views from app import views
from app import views_admin
from app.helpers import maintainer from app.helpers import maintainer
...@@ -13,6 +13,7 @@ def get_user_data_count(cur, username): ...@@ -13,6 +13,7 @@ def get_user_data_count(cur, username):
for a in apps: for a in apps:
devs.append(dd.get_list(a[1])[1]) devs.append(dd.get_list(a[1])[1])
if apps != [] and devs != []:
query = 'WITH t AS (' query = 'WITH t AS ('
i = 0 i = 0
for a in apps: for a in apps:
...@@ -25,6 +26,8 @@ def get_user_data_count(cur, username): ...@@ -25,6 +26,8 @@ def get_user_data_count(cur, username):
cur.execute(query, ()) cur.execute(query, ())
return (True,cur.fetchone()) return (True,cur.fetchone())
else:
return (True,(0,))
@with_psql @with_psql
...@@ -35,6 +38,7 @@ def get_user_data_count_per_hour(cur, username, hour): ...@@ -35,6 +38,7 @@ def get_user_data_count_per_hour(cur, username, hour):
for a in apps: for a in apps:
devs.append(dd.get_list(a[1])[1]) devs.append(dd.get_list(a[1])[1])
if apps != [] and devs != []:
utc_hour = utc_roundhour(hour) utc_hour = utc_roundhour(hour)
query = 'WITH t AS (' query = 'WITH t AS ('
...@@ -50,6 +54,8 @@ def get_user_data_count_per_hour(cur, username, hour): ...@@ -50,6 +54,8 @@ def get_user_data_count_per_hour(cur, username, hour):
cur.execute(query, ()) cur.execute(query, ())
return (True,cur.fetchone()) return (True,cur.fetchone())
else:
return (True, (0,))
@with_psql @with_psql
...@@ -62,6 +68,7 @@ def get_user_data_count_per_hour_period(cur, username, period): ...@@ -62,6 +68,7 @@ def get_user_data_count_per_hour_period(cur, username, period):
utc_hour = [utc_roundhour(x) for x in range(period,-1,-1)] utc_hour = [utc_roundhour(x) for x in range(period,-1,-1)]
if apps != [] and devs != []:
query = 'WITH t AS (' query = 'WITH t AS ('
i = 0 i = 0
for a in apps: for a in apps:
...@@ -78,6 +85,9 @@ def get_user_data_count_per_hour_period(cur, username, period): ...@@ -78,6 +85,9 @@ def get_user_data_count_per_hour_period(cur, username, period):
cur.execute(query, ()) cur.execute(query, ())
return (True,cur.fetchall()) return (True,cur.fetchall())
else:
return (True, (0,))
@with_psql @with_psql
...@@ -88,6 +98,7 @@ def get_user_data_count_per_day(cur, username, day=0): ...@@ -88,6 +98,7 @@ def get_user_data_count_per_day(cur, username, day=0):
for a in apps: for a in apps:
devs.append(dd.get_list(a[1])[1]) devs.append(dd.get_list(a[1])[1])
if apps != [] and devs != []:
utc_day = utc_roundday(day) utc_day = utc_roundday(day)
query = 'WITH t AS (' query = 'WITH t AS ('
...@@ -102,6 +113,8 @@ def get_user_data_count_per_day(cur, username, day=0): ...@@ -102,6 +113,8 @@ def get_user_data_count_per_day(cur, username, day=0):
cur.execute(query, ()) cur.execute(query, ())
return (True,cur.fetchone()) return (True,cur.fetchone())
else:
return (True, (0,))
@with_psql @with_psql
...@@ -112,6 +125,7 @@ def get_user_data_count_per_day_period(cur, username, period): ...@@ -112,6 +125,7 @@ def get_user_data_count_per_day_period(cur, username, period):
for a in apps: for a in apps:
devs.append(dd.get_list(a[1])[1]) devs.append(dd.get_list(a[1])[1])
if apps != [] and devs != []:
utc_hour = [utc_roundday(x) for x in range(period,-1,-1)] utc_hour = [utc_roundday(x) for x in range(period,-1,-1)]
query = 'WITH t AS (' query = 'WITH t AS ('
...@@ -130,6 +144,8 @@ def get_user_data_count_per_day_period(cur, username, period): ...@@ -130,6 +144,8 @@ def get_user_data_count_per_day_period(cur, username, period):
cur.execute(query, ()) cur.execute(query, ())
return (True,cur.fetchall()) return (True,cur.fetchall())
else:
return (True, (0,))
@with_psql @with_psql
......
This diff is collapsed.
This diff is collapsed.
from app import app from app import app
from flask import session,flash,redirect,url_for
from binascii import hexlify from binascii import hexlify
import os import os
import psycopg2 import psycopg2
import binascii import binascii
from datetime import datetime from datetime import datetime
from functools import wraps
def rand_str(length): def rand_str(length):
if length % 2 == 0: if length % 2 == 0:
...@@ -75,6 +77,19 @@ def with_psql(f): ...@@ -75,6 +77,19 @@ def with_psql(f):
return res return res
return _with_psql 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(): def clean_data_folder():
try: try:
filelist = [f for f in os.listdir(app.config['DATA_DOWNLOAD_DIR_OS'])] filelist = [f for f in os.listdir(app.config['DATA_DOWNLOAD_DIR_OS'])]
......
...@@ -172,7 +172,7 @@ ...@@ -172,7 +172,7 @@
{% with messages = get_flashed_messages(with_categories=true) %} {% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %} {% if messages %}
{% for category, message in 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"> <div class="alert alert-{{ category }} alert-dismissible show fade" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> <button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span> <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 @@ ...@@ -16,6 +16,7 @@
</div> </div>
<div class="card-body"> <div class="card-body">
<div class="row"> <div class="row">
{% if apps %}
{% for a in apps %} {% for a in apps %}
<div class="col-lg-6 col-md-6" style="margin-bottom : 30px;"> <div class="col-lg-6 col-md-6" style="margin-bottom : 30px;">
<div class="card"> <div class="card">
...@@ -27,6 +28,11 @@ ...@@ -27,6 +28,11 @@
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
{% else %}
<div class="col-lg-12">
<center><p> There are no created applications. </p></center>
</div>
{% endif %}
</div> </div>
<div class="row" style="margin-top: 30px;"> <div class="row" style="margin-top: 30px;">
......
...@@ -96,7 +96,7 @@ ...@@ -96,7 +96,7 @@
<p> Total: <strong> {{ total }} </strong> messages. </p> <p> Total: <strong> {{ total }} </strong> messages. </p>
<table class="table" id="table_{{ k }}"> <table class="table" id="table_{{ k }}">
<thead> <thead>
<th> Time </th> <th> Timestamp </th>
<th> {{ k }} </th> <th> {{ k }} </th>
</thead> </thead>
<tbody id="table_{{ k }}_body"> <tbody id="table_{{ k }}_body">
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
</div> </div>
<!-- Navbar items --> <!-- Navbar items -->
<ul class="navbar-nav ml-auto"> <ul class="navbar-nav ml-auto">
{% if users_signup %} {% if config['USERS_SIGNUP'] %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link nav-link-icon" href="/register"> <a class="nav-link nav-link-icon" href="/register">
<i class="ni ni-circle-08"></i> <i class="ni ni-circle-08"></i>
...@@ -113,7 +113,7 @@ ...@@ -113,7 +113,7 @@
<a href="#" class="text-light"><small>Forgot password?</small></a> <a href="#" class="text-light"><small>Forgot password?</small></a>
</div> </div>
<div class="col-6 text-right"> <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> <a href="/register" class="text-light"><small>Create new account</small></a>
{% endif %} {% endif %}
</div> </div>
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
</div> </div>
<!-- Navbar items --> <!-- Navbar items -->
<ul class="navbar-nav ml-auto"> <ul class="navbar-nav ml-auto">
{% if users_signup %} {% if config['USERS_SIGNUP'] %}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link nav-link-icon" href="/register"> <a class="nav-link nav-link-icon" href="/register">
<i class="ni ni-circle-08"></i> <i class="ni ni-circle-08"></i>
......
...@@ -612,8 +612,8 @@ def data_csv(): ...@@ -612,8 +612,8 @@ def data_csv():
return redirect(utl_for('index')) return redirect(utl_for('index'))
@app.route('/administration', methods=['GET', 'POST']) @app.route('/administration_old', methods=['GET', 'POST'])
def administration(): def administration_old():
if 'role' in session and session['role'] == 'admin': if 'role' in session and session['role'] == 'admin':
user_cnt = ud.get_count() user_cnt = ud.get_count()
apps_cnt = ad.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): ...@@ -2,7 +2,7 @@ class Config(object):
DEBUG = False DEBUG = False
TESTING = False TESTING = False
SECRET_KEY = b'ksj^*(s90*Dklds;osdj' SECRET_KEY = 'al1DuE8cFpk3EJNlFHG73Fd'
DB_NAME = 'iotserver' DB_NAME = 'iotserver'
DB_USERNAME = 'pi' 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