2017-09-12 07:39:18 +00:00
|
|
|
import os
|
2017-02-02 08:33:54 +00:00
|
|
|
from flask import Flask, session, url_for, redirect, request, render_template, abort
|
2017-02-01 07:21:18 +00:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2017-09-12 09:41:13 +00:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
2017-09-11 10:57:41 +00:00
|
|
|
import bcrypt
|
2017-02-01 07:21:18 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2017-02-01 09:37:53 +00:00
|
|
|
app.secret_key = "pepsecret"
|
2017-02-01 07:21:18 +00:00
|
|
|
|
|
|
|
# SQL
|
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
|
2017-09-12 07:39:18 +00:00
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
2017-02-01 07:21:18 +00:00
|
|
|
db = SQLAlchemy(app)
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
class User(db.Model):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Utente per il login sul sito dell'inventario."""
|
2017-09-12 07:39:18 +00:00
|
|
|
__tablename__ = "website_users"
|
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
uid = db.Column(db.Integer, primary_key=True)
|
2017-09-12 07:40:47 +00:00
|
|
|
username = db.Column(db.String, unique=True)
|
2017-09-11 10:57:41 +00:00
|
|
|
passwd = db.Column(db.LargeBinary)
|
2017-02-01 07:21:18 +00:00
|
|
|
|
|
|
|
def __init__(self, username, passwd):
|
|
|
|
self.username = username
|
|
|
|
self.passwd = passwd
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<User {}>".format(self.username, self.passwd)
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
class Ente(db.Model):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Ente (Unione Terre di Castelli, Comune di Vignola...)."""
|
2017-09-12 07:39:18 +00:00
|
|
|
__tablename__ = "enti"
|
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
eid = db.Column(db.Integer, primary_key=True)
|
2017-09-12 07:40:47 +00:00
|
|
|
nomeente = db.Column(db.String)
|
|
|
|
nomebreveente = db.Column(db.String)
|
2017-02-01 07:21:18 +00:00
|
|
|
servizi = db.relationship("Servizio", backref='ente', lazy='dynamic')
|
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
def __init__(self, nomeente, nomebreveente):
|
|
|
|
self.nomeente = nomeente
|
|
|
|
self.nomebreveente = nomebreveente
|
2017-02-01 07:21:18 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2017-02-01 09:37:53 +00:00
|
|
|
return "<Ente {}>".format(self.nomebreveente)
|
2017-02-01 07:21:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Servizio(db.Model):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Servizio di un ente (Anagrafe, Ufficio Scuola, Sindaco)."""
|
2017-09-12 07:39:18 +00:00
|
|
|
__tablename__ = "servizi"
|
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
sid = db.Column(db.Integer, primary_key=True)
|
2017-09-12 07:39:18 +00:00
|
|
|
eid = db.Column(db.Integer, db.ForeignKey('enti.eid'))
|
2017-09-12 07:40:47 +00:00
|
|
|
nomeservizio = db.Column(db.String)
|
|
|
|
locazione = db.Column(db.String)
|
2017-02-01 07:21:18 +00:00
|
|
|
impiegati = db.relationship("Impiegato", backref='servizio', lazy='dynamic')
|
|
|
|
|
2017-09-11 13:56:42 +00:00
|
|
|
def __init__(self, eid, nomeservizio, locazione):
|
2017-02-01 07:21:18 +00:00
|
|
|
self.eid = eid
|
2017-02-01 09:37:53 +00:00
|
|
|
self.nomeservizio = nomeservizio
|
2017-09-11 13:56:42 +00:00
|
|
|
self.locazione = locazione
|
2017-02-01 07:21:18 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2017-02-01 09:37:53 +00:00
|
|
|
return "<Servizio {}>".format(self.nomeservizio)
|
2017-02-01 07:21:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Impiegato(db.Model):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Impiegato in uno dei servizi."""
|
2017-09-12 07:39:18 +00:00
|
|
|
__tablename__ = "impiegati"
|
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
iid = db.Column(db.Integer, primary_key=True)
|
2017-09-12 07:39:18 +00:00
|
|
|
sid = db.Column(db.Integer, db.ForeignKey('servizi.sid'))
|
2017-09-12 07:40:47 +00:00
|
|
|
nomeimpiegato = db.Column(db.String)
|
|
|
|
username = db.Column(db.String, unique=True)
|
|
|
|
passwd = db.Column(db.String)
|
2017-02-02 08:33:54 +00:00
|
|
|
dispositivi = db.relationship("Accesso", backref='impiegato', lazy='dynamic')
|
2017-02-01 07:21:18 +00:00
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
def __init__(self, sid, nomeimpiegato, username, passwd):
|
2017-02-01 07:21:18 +00:00
|
|
|
self.sid = sid
|
2017-02-01 09:37:53 +00:00
|
|
|
self.nomeimpiegato = nomeimpiegato
|
2017-02-01 07:21:18 +00:00
|
|
|
self.username = username
|
|
|
|
self.passwd = passwd
|
|
|
|
|
|
|
|
def __repr__(self):
|
2017-02-02 08:33:54 +00:00
|
|
|
return "<Impiegato {}>".format(self.nome)
|
|
|
|
|
|
|
|
|
|
|
|
class Dispositivo(db.Model):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Dispositivo gestito dal CED registrato nell'inventario."""
|
2017-09-12 07:39:18 +00:00
|
|
|
__tablename__ = "dispositivi"
|
|
|
|
|
2017-02-02 08:33:54 +00:00
|
|
|
did = db.Column(db.Integer, primary_key=True)
|
2017-09-12 07:39:18 +00:00
|
|
|
accessi = db.relationship("Accesso", backref='dispositivo', lazy='dynamic')
|
2017-09-12 07:40:47 +00:00
|
|
|
tipo = db.Column(db.String)
|
|
|
|
marca = db.Column(db.String)
|
|
|
|
modello = db.Column(db.String)
|
|
|
|
inv_ced = db.Column(db.String)
|
|
|
|
inv_ente = db.Column(db.String)
|
|
|
|
fornitore = db.Column(db.String)
|
|
|
|
seriale = db.Column(db.String)
|
2017-09-13 06:19:08 +00:00
|
|
|
ip = db.Column(db.String)
|
2017-09-12 07:39:18 +00:00
|
|
|
nid = db.Column(db.Integer, db.ForeignKey('reti.nid'))
|
|
|
|
rete = db.relationship("Rete", backref='dispositivi')
|
2017-02-02 08:33:54 +00:00
|
|
|
|
2017-09-13 06:19:08 +00:00
|
|
|
def __init__(self, tipo, marca, modello, inv_ced, inv_ente, fornitore, nid, seriale, ip):
|
2017-02-02 08:33:54 +00:00
|
|
|
self.tipo = tipo
|
|
|
|
self.marca = marca
|
|
|
|
self.modello = modello
|
|
|
|
self.inv_ced = inv_ced
|
|
|
|
self.inv_ente = inv_ente
|
|
|
|
self.fornitore = fornitore
|
2017-09-11 14:55:23 +00:00
|
|
|
self.nid = nid
|
2017-09-11 15:05:45 +00:00
|
|
|
self.seriale = seriale
|
2017-09-13 06:19:08 +00:00
|
|
|
self.ip=ip
|
2017-02-02 08:33:54 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Dispositivo {}>".format(self.inv_ced)
|
|
|
|
|
|
|
|
|
|
|
|
class Accesso(db.Model):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Tabella di associazione tra dispositivi e impiegati."""
|
2017-09-12 07:39:18 +00:00
|
|
|
__tablename__ = "assoc_accessi"
|
|
|
|
|
|
|
|
iid = db.Column(db.Integer, db.ForeignKey('impiegati.iid'), primary_key=True)
|
|
|
|
did = db.Column(db.Integer, db.ForeignKey('dispositivi.did'), primary_key=True)
|
2017-02-02 08:33:54 +00:00
|
|
|
|
|
|
|
def __init__(self, iid, did):
|
|
|
|
self.iid = iid
|
|
|
|
self.did = did
|
|
|
|
|
|
|
|
def __repr__(self):
|
2017-02-03 12:04:11 +00:00
|
|
|
return "<Accesso {} su {}>".format(self.iid, self.did)
|
2017-02-02 08:33:54 +00:00
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
|
2017-09-12 07:39:18 +00:00
|
|
|
class Rete(db.Model):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Configurazione di rete di uno o più computer."""
|
2017-09-12 07:39:18 +00:00
|
|
|
__tablename__ = "reti"
|
|
|
|
|
2017-09-11 14:55:23 +00:00
|
|
|
nid = db.Column(db.Integer, primary_key=True)
|
2017-09-12 07:40:47 +00:00
|
|
|
nome = db.Column(db.String)
|
|
|
|
network_ip = db.Column(db.String, unique=True, nullable=False)
|
2017-09-12 07:39:18 +00:00
|
|
|
subnet = db.Column(db.Integer, nullable=False)
|
2017-09-12 07:40:47 +00:00
|
|
|
primary_dns = db.Column(db.String)
|
|
|
|
secondary_dns = db.Column(db.String)
|
2017-09-11 14:55:23 +00:00
|
|
|
|
2017-09-12 07:39:18 +00:00
|
|
|
def __init__(self, nome, network_ip, subnet, primary_dns, secondary_dns):
|
2017-09-12 06:26:58 +00:00
|
|
|
self.nome = nome
|
2017-09-12 07:39:18 +00:00
|
|
|
self.network_ip = network_ip
|
|
|
|
self.subnet = subnet
|
|
|
|
self.primary_dns = primary_dns
|
|
|
|
self.secondary_dns = secondary_dns
|
2017-09-11 14:55:23 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Rete {},{}>".format(self.nid, self.nome)
|
|
|
|
|
|
|
|
|
2017-09-11 13:00:04 +00:00
|
|
|
class FakeAccesso:
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Hackerino usato nel caso in cui non ci sia nessun impiegato assegnato a un dispositivo.
|
|
|
|
Viva il duck typing!"""
|
2017-09-11 13:00:04 +00:00
|
|
|
def __init__(self, dispositivo):
|
|
|
|
self.did = dispositivo.did
|
|
|
|
self.iid = None
|
|
|
|
self.dispositivo = dispositivo
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
if key == 0:
|
|
|
|
return self.dispositivo
|
|
|
|
|
2017-09-12 06:26:58 +00:00
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
# Funzioni del sito
|
|
|
|
def login(username, password):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Controlla se l'username e la password di un utente del sito sono corrette."""
|
2017-02-01 07:21:18 +00:00
|
|
|
user = User.query.filter_by(username=username).first()
|
2017-09-12 09:39:21 +00:00
|
|
|
return user is not None and bcrypt.checkpw(bytes(password, encoding="utf-8"), user.passwd)
|
2017-02-01 07:21:18 +00:00
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-09-12 07:39:18 +00:00
|
|
|
def subnet_to_string(integer):
|
|
|
|
"""Converte una subnet mask in numero in una stringa"""
|
|
|
|
still_int = (0xFFFFFFFF << (32 - integer)) & 0xFFFFFFFF
|
|
|
|
return f"{still_int >> 24}.{(still_int >> 16) & 0xFF}.{(still_int >> 8)}.{still_int & 0xFF}"
|
|
|
|
|
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
# Sito
|
|
|
|
@app.route('/')
|
|
|
|
def page_home():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina principale del sito:
|
|
|
|
se non sei loggato reindirizza alla pagina del login,
|
|
|
|
se sei loggato effettua il logout e dopo reindirizza al login"""
|
2017-02-01 07:21:18 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 07:21:18 +00:00
|
|
|
else:
|
2017-09-13 07:17:23 +00:00
|
|
|
return redirect(url_for('page_dashboard'))
|
2017-02-01 07:21:18 +00:00
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
|
|
def page_login():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di login:
|
|
|
|
accetta richieste GET per la visualizzazione della pagina
|
|
|
|
e richieste POST con form data per il login"""
|
2017-02-01 07:21:18 +00:00
|
|
|
if request.method == 'GET':
|
|
|
|
goldfish = url_for("static", filename="goldfish.png")
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("login.htm", goldfish=goldfish)
|
2017-02-01 07:21:18 +00:00
|
|
|
else:
|
|
|
|
if login(request.form['username'], request.form['password']):
|
|
|
|
session['username'] = request.form['username']
|
|
|
|
return redirect(url_for('page_dashboard'))
|
|
|
|
else:
|
2017-09-13 07:14:56 +00:00
|
|
|
return render_template('error.htm', error="Username o password non validi.")
|
2017-02-01 07:21:18 +00:00
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-09-13 07:17:23 +00:00
|
|
|
@app.route('/logout')
|
|
|
|
def page_logout():
|
|
|
|
"""Pagina di logout:
|
|
|
|
slogga l'utente se visitata"""
|
|
|
|
if 'username' in session:
|
|
|
|
session.pop('username')
|
|
|
|
return redirect(url_for('page_login'))
|
|
|
|
|
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
@app.route('/dashboard')
|
|
|
|
def page_dashboard():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Dashboard del sito:
|
|
|
|
Conteggia i servizi e visualizza la navbar
|
|
|
|
Sì, è un po' inutile."""
|
2017-02-02 07:46:15 +00:00
|
|
|
enti = Ente.query.all()
|
|
|
|
conteggioservizi = dict()
|
2017-09-12 06:26:58 +00:00
|
|
|
goldfish = url_for("static", filename="goldfish.png")
|
2017-02-02 07:46:15 +00:00
|
|
|
for ente in enti:
|
|
|
|
conteggioservizi[ente.nomeente] = Servizio.query.join(Ente).filter_by(eid=ente.eid).count()
|
|
|
|
conteggioutenti = dict()
|
|
|
|
for ente in enti:
|
|
|
|
conteggioutenti[ente.nomeente] = Impiegato.query.join(Servizio).join(Ente).filter_by(eid=ente.eid).count()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("dashboard.htm", type="main", user=session["username"],
|
2017-09-12 09:15:36 +00:00
|
|
|
conteggioutenti=conteggioutenti, conteggioservizi=conteggioservizi, goldfish=goldfish)
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
|
|
|
|
@app.route('/ente_add', methods=['GET', 'POST'])
|
|
|
|
def page_ente_add():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di creazione nuovo ente:
|
|
|
|
come tutte le altre pagine di creazione del sito,
|
|
|
|
accetta GET per visualizzare la pagina
|
|
|
|
e POST con form data per l'aggiunta effettiva."""
|
2017-02-01 07:21:18 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 07:21:18 +00:00
|
|
|
if request.method == 'GET':
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("ente/add.htm", type="ente", user=session["username"])
|
2017-02-01 07:21:18 +00:00
|
|
|
else:
|
2017-02-01 09:37:53 +00:00
|
|
|
nuovoent = Ente(request.form['nomeente'], request.form['nomebreveente'])
|
2017-02-01 07:21:18 +00:00
|
|
|
db.session.add(nuovoent)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_ente_list'))
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
@app.route('/ente_del/<int:eid>')
|
|
|
|
def page_ente_del(eid):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di cancellazione ente:
|
|
|
|
accetta richieste GET per cancellare l'ente specificato."""
|
2017-02-01 07:21:18 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 09:46:30 +00:00
|
|
|
ente = Ente.query.get_or_404(eid)
|
2017-02-01 09:37:53 +00:00
|
|
|
servizi = Servizio.query.filter_by(eid=ente.eid).all()
|
|
|
|
for serv in servizi:
|
|
|
|
impiegati = Impiegato.query.filter_by(sid=serv.sid).all()
|
|
|
|
for imp in impiegati:
|
|
|
|
db.session.delete(imp)
|
|
|
|
db.session.delete(serv)
|
2017-02-01 07:21:18 +00:00
|
|
|
db.session.delete(ente)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_ente_list'))
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
@app.route('/ente_list')
|
|
|
|
def page_ente_list():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di elenco degli enti disponibili sul sito."""
|
2017-02-01 07:21:18 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 07:21:18 +00:00
|
|
|
enti = Ente.query.all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("ente/list.htm", enti=enti, type="ente", user=session["username"])
|
2017-02-01 07:21:18 +00:00
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 07:21:18 +00:00
|
|
|
@app.route('/ente_show/<int:eid>', methods=['GET', 'POST'])
|
|
|
|
def page_ente_show(eid):
|
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 07:21:18 +00:00
|
|
|
if request.method == "GET":
|
2017-09-12 09:46:30 +00:00
|
|
|
ente = Ente.query.get_or_404(eid)
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("ente/show.htm", ente=ente, user=session["username"])
|
2017-02-01 07:21:18 +00:00
|
|
|
else:
|
2017-09-12 09:46:30 +00:00
|
|
|
ente = Ente.query.get_or_404(eid)
|
2017-02-01 09:37:53 +00:00
|
|
|
ente.nomeente = request.form["nomeente"]
|
|
|
|
ente.nomebreveente = request.form["nomebreveente"]
|
2017-02-01 07:21:18 +00:00
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_ente_list'))
|
2017-02-01 09:37:53 +00:00
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
@app.route('/serv_add', methods=['GET', 'POST'])
|
|
|
|
def page_serv_add():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di creazione nuovo servizio:
|
|
|
|
come tutte le altre pagine di creazione del sito,
|
|
|
|
accetta GET per visualizzare la pagina
|
|
|
|
e POST con form data per l'aggiunta effettiva."""
|
2017-02-01 09:37:53 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 09:37:53 +00:00
|
|
|
if request.method == 'GET':
|
|
|
|
enti = Ente.query.all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("servizio/add.htm", enti=enti, type="serv", user=session["username"])
|
2017-02-01 09:37:53 +00:00
|
|
|
else:
|
2017-09-11 13:56:42 +00:00
|
|
|
nuovoserv = Servizio(request.form['eid'], request.form['nomeservizio'], request.form['locazione'])
|
2017-02-01 09:37:53 +00:00
|
|
|
db.session.add(nuovoserv)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_serv_list'))
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
@app.route('/serv_del/<int:sid>')
|
|
|
|
def page_serv_del(sid):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di cancellazione servizio:
|
|
|
|
accetta richieste GET per cancellare il servizio specificato."""
|
2017-02-01 09:37:53 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 09:46:30 +00:00
|
|
|
serv = Servizio.query.get_or_404(sid)
|
2017-02-01 09:37:53 +00:00
|
|
|
impiegati = Impiegato.query.filter_by(sid=serv.sid).all()
|
|
|
|
for imp in impiegati:
|
|
|
|
db.session.delete(imp)
|
|
|
|
db.session.delete(serv)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_serv_list'))
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
@app.route('/serv_list')
|
|
|
|
def page_serv_list():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di elenco dei servizi registrati sul sito."""
|
2017-02-01 09:37:53 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 09:37:53 +00:00
|
|
|
serv = Servizio.query.join(Ente).all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("servizio/list.htm", serv=serv, type="serv", user=session["username"])
|
2017-02-01 09:37:53 +00:00
|
|
|
|
|
|
|
|
2017-09-11 13:00:04 +00:00
|
|
|
@app.route('/serv_list/<int:eid>')
|
|
|
|
def page_serv_list_plus(eid):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di elenco dei servizi registrati sul sito, filtrati per ente."""
|
2017-09-11 13:00:04 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-11 13:00:04 +00:00
|
|
|
serv = Servizio.query.join(Ente).filter_by(eid=eid).all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("servizio/list.htm", serv=serv, type="serv", user=session["username"])
|
2017-09-11 13:00:04 +00:00
|
|
|
|
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
@app.route('/serv_show/<int:sid>', methods=['GET', 'POST'])
|
|
|
|
def page_serv_show(sid):
|
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 09:37:53 +00:00
|
|
|
if request.method == "GET":
|
2017-09-12 09:46:30 +00:00
|
|
|
serv = Servizio.query.get_or_404(sid)
|
2017-02-01 09:37:53 +00:00
|
|
|
enti = Ente.query.all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("servizio/show.htm", serv=serv, enti=enti, user=session["username"])
|
2017-02-01 09:37:53 +00:00
|
|
|
else:
|
2017-09-12 09:46:30 +00:00
|
|
|
serv = Servizio.query.get_or_404(sid)
|
2017-02-01 09:37:53 +00:00
|
|
|
serv.eid = request.form["eid"]
|
|
|
|
serv.nomeservizio = request.form["nomeservizio"]
|
2017-09-11 13:56:42 +00:00
|
|
|
serv.locazione = request.form["locazione"]
|
2017-02-01 09:37:53 +00:00
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_serv_list'))
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
@app.route('/imp_add', methods=['GET', 'POST'])
|
|
|
|
def page_imp_add():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di creazione nuovo impiegato:
|
|
|
|
come tutte le altre pagine di creazione del sito,
|
|
|
|
accetta GET per visualizzare la pagina
|
|
|
|
e POST con form data per l'aggiunta effettiva."""
|
2017-02-01 09:37:53 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 09:37:53 +00:00
|
|
|
if request.method == 'GET':
|
2017-02-02 08:33:54 +00:00
|
|
|
servizi = Servizio.query.join(Ente).all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("impiegato/add.htm", servizi=servizi, type="imp", user=session["username"])
|
2017-02-01 09:37:53 +00:00
|
|
|
else:
|
2017-09-11 09:43:50 +00:00
|
|
|
nuovoimp = Impiegato(request.form['sid'], request.form['nomeimpiegato'], request.form['username'],
|
|
|
|
request.form['passwd'],)
|
2017-02-01 09:37:53 +00:00
|
|
|
db.session.add(nuovoimp)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_imp_list'))
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
@app.route('/imp_del/<int:iid>')
|
|
|
|
def page_imp_del(iid):
|
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 09:46:30 +00:00
|
|
|
imp = Impiegato.query.get_or_404(iid)
|
2017-02-01 09:37:53 +00:00
|
|
|
db.session.delete(imp)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_imp_list'))
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
@app.route('/imp_list')
|
|
|
|
def page_imp_list():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di elenco degli impiegati registrati nell'inventario."""
|
2017-02-01 09:37:53 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 09:37:53 +00:00
|
|
|
impiegati = Impiegato.query.join(Servizio).join(Ente).all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("impiegato/list.htm", impiegati=impiegati, type="imp", user=session["username"])
|
2017-02-01 09:37:53 +00:00
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
@app.route('/imp_list/<int:sid>')
|
|
|
|
def page_imp_list_plus(sid):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di elenco degli impiegati registrati nell'inventario, filtrati per servizio."""
|
2017-02-01 09:37:53 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 09:37:53 +00:00
|
|
|
impiegati = Impiegato.query.join(Servizio).filter_by(sid=sid).join(Ente).all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("impiegato/list.htm", impiegati=impiegati, user=session["username"])
|
2017-02-01 09:37:53 +00:00
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-01 09:37:53 +00:00
|
|
|
@app.route('/imp_show/<int:iid>', methods=['GET', 'POST'])
|
|
|
|
def page_imp_show(iid):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di cancellazione impiegato:
|
|
|
|
accetta richieste GET per cancellare l'impiegato specificato."""
|
2017-02-01 09:37:53 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-01 09:37:53 +00:00
|
|
|
if request.method == "GET":
|
2017-09-12 09:46:30 +00:00
|
|
|
imp = Impiegato.query.get_or_404(iid)
|
2017-02-01 09:37:53 +00:00
|
|
|
servizi = Servizio.query.all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("impiegato/show.htm", imp=imp, servizi=servizi, user=session["username"])
|
2017-02-01 09:37:53 +00:00
|
|
|
else:
|
2017-09-12 09:46:30 +00:00
|
|
|
imp = Impiegato.query.get_or_404(iid)
|
2017-02-01 09:37:53 +00:00
|
|
|
imp.sid = request.form["sid"]
|
|
|
|
imp.nomeimpiegato = request.form["nomeimpiegato"]
|
|
|
|
imp.username = request.form["username"]
|
|
|
|
imp.passwd = request.form["passwd"]
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_imp_list'))
|
2017-02-02 14:44:26 +00:00
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-09-12 09:39:21 +00:00
|
|
|
@app.route('/imp_details/<int:iid>')
|
|
|
|
def page_imp_details(iid):
|
|
|
|
"""Pagina dei dettagli di un impiegato"""
|
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 09:46:30 +00:00
|
|
|
impiegato = Impiegato.query.filter_by(iid=iid).first_or_404()
|
2017-09-12 09:39:21 +00:00
|
|
|
return render_template("impiegato/details.htm", imp=impiegato, type="imp", user=session["username"])
|
|
|
|
|
|
|
|
|
2017-02-02 14:44:26 +00:00
|
|
|
@app.route('/disp_add', methods=['GET', 'POST'])
|
|
|
|
def page_disp_add():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di creazione nuovo dispositivo:
|
|
|
|
come tutte le altre pagine di creazione del sito,
|
|
|
|
accetta GET per visualizzare la pagina
|
|
|
|
e POST con form data per l'aggiunta effettiva."""
|
2017-02-02 14:44:26 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-02 14:44:26 +00:00
|
|
|
if request.method == 'GET':
|
2017-09-11 14:45:49 +00:00
|
|
|
serial = request.args.get("scanned_barcode")
|
2017-09-11 09:43:50 +00:00
|
|
|
opzioni = ["Centralino", "Dispositivo generico di rete", "Marcatempo", "PC", "Portatile", "POS", "Router",
|
|
|
|
"Server", "Stampante di rete", "Switch", "Telefono IP", "Monitor", "Scanner", "Stampante locale"]
|
2017-09-12 07:39:18 +00:00
|
|
|
reti = Rete.query.all()
|
2017-02-02 14:44:26 +00:00
|
|
|
impiegati = Impiegato.query.all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("dispositivo/add.htm", impiegati=impiegati, opzioni=opzioni, reti=reti,
|
2017-09-12 06:26:58 +00:00
|
|
|
type="dev", user=session["username"], serial=serial)
|
2017-02-02 14:44:26 +00:00
|
|
|
else:
|
2017-09-12 10:04:45 +00:00
|
|
|
if request.form["inv_ced"]:
|
|
|
|
try:
|
|
|
|
int(request.form["inv_ced"])
|
|
|
|
except ValueError:
|
|
|
|
return render_template("error.htm", error="Il campo Inventario CED deve contenere un numero.")
|
|
|
|
if request.form["inv_ente"]:
|
|
|
|
try:
|
|
|
|
int(request.form["inv_ente"])
|
|
|
|
except ValueError:
|
|
|
|
return render_template("error.htm", error="Il campo Inventario ente deve contenere un numero.")
|
2017-09-11 09:43:50 +00:00
|
|
|
nuovodisp = Dispositivo(request.form['tipo'], request.form['marca'], request.form['modello'],
|
2017-09-12 06:26:58 +00:00
|
|
|
request.form['inv_ced'], request.form['inv_ente'], request.form['fornitore'],
|
2017-09-13 06:19:08 +00:00
|
|
|
request.form['rete'], request.form['seriale'], request.form['ip'])
|
2017-02-02 14:44:26 +00:00
|
|
|
db.session.add(nuovodisp)
|
|
|
|
db.session.commit()
|
2017-02-03 12:04:11 +00:00
|
|
|
# Trova tutti gli utenti, edizione sporco hack in html
|
|
|
|
users = list()
|
|
|
|
while True:
|
|
|
|
# Trova tutti gli utenti esistenti
|
|
|
|
userstring = 'utente{}'.format(len(users))
|
|
|
|
if userstring in request.form:
|
|
|
|
users.append(request.form[userstring])
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
for user in users:
|
|
|
|
nuovologin = Accesso(int(user), nuovodisp.did)
|
|
|
|
db.session.add(nuovologin)
|
|
|
|
db.session.commit()
|
2017-02-02 14:44:26 +00:00
|
|
|
return redirect(url_for('page_disp_list'))
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-02 14:44:26 +00:00
|
|
|
@app.route('/disp_del/<int:did>')
|
|
|
|
def page_disp_del(did):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di cancellazione dispositivo:
|
|
|
|
accetta richieste GET per cancellare il dispositivo specificato."""
|
2017-02-02 14:44:26 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 10:04:45 +00:00
|
|
|
disp = Dispositivo.query.filter_by(did=did).join(Accesso).first_or_404()
|
|
|
|
for accesso in disp.accessi:
|
|
|
|
db.session.delete(accesso)
|
2017-02-02 14:44:26 +00:00
|
|
|
db.session.delete(disp)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_disp_list'))
|
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-02-02 14:44:26 +00:00
|
|
|
@app.route('/disp_list')
|
|
|
|
def page_disp_list():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di elenco dei dispositivi registrati nell'inventario."""
|
2017-02-02 14:44:26 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-02-03 12:04:11 +00:00
|
|
|
accessi = list()
|
2017-02-02 14:44:26 +00:00
|
|
|
dispositivi = Dispositivo.query.all()
|
2017-02-03 12:04:11 +00:00
|
|
|
for dispositivo in dispositivi:
|
|
|
|
accesso = Accesso.query.join(Dispositivo).filter_by(did=dispositivo.did).join(Impiegato).all()
|
2017-09-11 13:00:04 +00:00
|
|
|
if not accesso:
|
2017-09-11 13:22:44 +00:00
|
|
|
# oh dio mio a cosa stavo pensando viva il duck typing
|
2017-09-11 13:00:04 +00:00
|
|
|
accessi.append([FakeAccesso(dispositivo)])
|
|
|
|
else:
|
|
|
|
accessi.append(accesso)
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("dispositivo/list.htm", accessi=accessi, type="disp", user=session["username"])
|
2017-02-02 14:44:26 +00:00
|
|
|
|
2017-09-11 09:43:50 +00:00
|
|
|
|
2017-09-11 13:00:04 +00:00
|
|
|
@app.route('/disp_details/<int:did>')
|
2017-09-12 09:39:21 +00:00
|
|
|
def page_disp_details(did):
|
|
|
|
"""Pagina di dettagli di un dispositivo, contenente anche gli utenti che vi hanno accesso."""
|
2017-09-11 13:00:04 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-11 13:42:28 +00:00
|
|
|
disp = Dispositivo.query.filter_by(did=did).first_or_404()
|
2017-09-11 13:22:44 +00:00
|
|
|
accessi = Accesso.query.filter_by(did=did).all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("dispositivo/details.htm", disp=disp, accessi=accessi, type="disp",
|
2017-09-12 06:26:58 +00:00
|
|
|
user=session["username"])
|
2017-09-11 10:47:52 +00:00
|
|
|
|
|
|
|
|
2017-09-12 10:50:28 +00:00
|
|
|
@app.route('/disp_show/<int:did>', methods=['GET', 'POST'])
|
|
|
|
def page_disp_show(did):
|
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 10:50:28 +00:00
|
|
|
if request.method == 'GET':
|
|
|
|
disp = Dispositivo.query.filter_by(did=did).first_or_404()
|
2017-09-12 13:11:23 +00:00
|
|
|
accessi = Accesso.query.filter_by(did=did).all()
|
|
|
|
impiegati = Impiegato.query.all()
|
|
|
|
opzioni = ["Centralino", "Dispositivo generico di rete", "Marcatempo", "PC", "Portatile", "POS", "Router",
|
|
|
|
"Server", "Stampante di rete", "Switch", "Telefono IP", "Monitor", "Scanner", "Stampante locale"]
|
|
|
|
reti = Rete.query.all()
|
|
|
|
return render_template("dispositivo/show.htm", dispositivo=disp, accessi=accessi, impiegati=impiegati,
|
|
|
|
type="disp", user=session["username"], opzioni=opzioni, reti=reti)
|
2017-09-12 10:50:28 +00:00
|
|
|
else:
|
|
|
|
if request.form["inv_ced"]:
|
|
|
|
try:
|
|
|
|
int(request.form["inv_ced"])
|
|
|
|
except ValueError:
|
|
|
|
return render_template("error.htm", error="Il campo Inventario CED deve contenere un numero.")
|
|
|
|
if request.form["inv_ente"]:
|
|
|
|
try:
|
|
|
|
int(request.form["inv_ente"])
|
|
|
|
except ValueError:
|
|
|
|
return render_template("error.htm", error="Il campo Inventario ente deve contenere un numero.")
|
2017-09-12 13:11:23 +00:00
|
|
|
disp = Dispositivo.query.get_or_404(did)
|
|
|
|
accessi = Accesso.query.filter_by(did=did).all()
|
2017-09-12 10:50:28 +00:00
|
|
|
disp.tipo = request.form['tipo']
|
|
|
|
disp.marca = request.form['marca']
|
|
|
|
disp.modello = request.form['modello']
|
|
|
|
disp.inv_ced = request.form['inv_ced']
|
|
|
|
disp.inv_ente = request.form['inv_ente']
|
|
|
|
disp.fornitore = request.form['fornitore']
|
2017-09-12 13:11:23 +00:00
|
|
|
disp.nid = int(request.form['rete'])
|
2017-09-13 06:19:08 +00:00
|
|
|
disp.ip = request.form['ip']
|
2017-09-12 10:50:28 +00:00
|
|
|
# Trova tutti gli utenti, edizione sporco hack in html
|
|
|
|
users = list()
|
|
|
|
while True:
|
|
|
|
# Trova tutti gli utenti esistenti
|
|
|
|
userstring = 'utente{}'.format(len(users))
|
|
|
|
if userstring in request.form:
|
|
|
|
users.append(request.form[userstring])
|
|
|
|
else:
|
|
|
|
break
|
2017-09-12 13:11:23 +00:00
|
|
|
for accesso in accessi:
|
|
|
|
db.session.delete(accesso)
|
2017-09-12 10:50:28 +00:00
|
|
|
for user in users:
|
2017-09-12 13:11:23 +00:00
|
|
|
nuovologin = Accesso(int(user), disp.did)
|
2017-09-12 10:50:28 +00:00
|
|
|
db.session.add(nuovologin)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_disp_list'))
|
|
|
|
|
|
|
|
|
2017-09-11 14:55:23 +00:00
|
|
|
@app.route('/net_add', methods=['GET', 'POST'])
|
|
|
|
def page_net_add():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di creazione nuova rete:
|
|
|
|
come tutte le altre pagine di creazione del sito,
|
|
|
|
accetta GET per visualizzare la pagina
|
|
|
|
e POST con form data per l'aggiunta effettiva."""
|
2017-09-11 14:55:23 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-11 14:55:23 +00:00
|
|
|
if request.method == 'GET':
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("net/add.htm", type="net", user=session["username"])
|
2017-09-11 14:55:23 +00:00
|
|
|
else:
|
2017-09-12 07:39:18 +00:00
|
|
|
nuovonet = Rete(nome=request.form["nome"], network_ip=request.form["network_ip"], subnet=request.form["subnet"],
|
|
|
|
primary_dns=request.form["primary_dns"], secondary_dns=request.form["secondary_dns"])
|
2017-09-11 14:55:23 +00:00
|
|
|
db.session.add(nuovonet)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_net_list'))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/net_del/<int:nid>')
|
2017-09-12 07:39:18 +00:00
|
|
|
def page_net_del(nid):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di cancellazione rete:
|
|
|
|
accetta richieste GET per cancellare la rete specificata."""
|
2017-09-11 14:55:23 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:14:56 +00:00
|
|
|
return abort(403)
|
2017-09-12 09:46:30 +00:00
|
|
|
rete = Rete.query.get_or_404(nid)
|
2017-09-11 14:55:23 +00:00
|
|
|
db.session.delete(rete)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_net_list'))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/net_list')
|
|
|
|
def page_net_list():
|
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 07:39:18 +00:00
|
|
|
reti = Rete.query.all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("net/list.htm", reti=reti, type="net", user=session["username"])
|
2017-09-11 14:55:23 +00:00
|
|
|
|
|
|
|
|
2017-09-11 15:31:20 +00:00
|
|
|
@app.route('/net_details/<int:nid>')
|
2017-09-12 07:39:18 +00:00
|
|
|
def page_net_details(nid):
|
2017-09-11 15:31:20 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 09:46:30 +00:00
|
|
|
net = Rete.query.filter_by(nid=nid).first_or_404()
|
2017-09-12 07:39:18 +00:00
|
|
|
dispositivi = Dispositivo.query.join(Rete).filter_by(nid=nid).all()
|
|
|
|
subnet = subnet_to_string(net.subnet)
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("net/details.htm", net=net, subnet=subnet, dispositivi=dispositivi, type="net",
|
2017-09-12 06:26:58 +00:00
|
|
|
user=session["username"])
|
2017-09-11 15:31:20 +00:00
|
|
|
|
|
|
|
|
2017-09-12 08:48:21 +00:00
|
|
|
@app.route('/user_list')
|
|
|
|
def page_user_list():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di elenco degli utenti che possono connettersi al sito.
|
|
|
|
Le password sono hashate."""
|
2017-09-12 08:48:21 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 08:48:21 +00:00
|
|
|
utenti = User.query.all()
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("user/list.htm", utenti=utenti, type="user", user=session["username"])
|
2017-09-12 08:48:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/user_del/<int:uid>')
|
|
|
|
def page_user_del(uid):
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di cancellazione impiegato:
|
|
|
|
accetta richieste GET per cancellare l'utente specificato."""
|
2017-09-12 08:48:21 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 09:46:30 +00:00
|
|
|
utente = User.query.get_or_404(uid)
|
2017-09-12 08:48:21 +00:00
|
|
|
db.session.delete(utente)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_user_list'))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/user_add', methods=['GET', 'POST'])
|
|
|
|
def page_user_add():
|
2017-09-12 09:39:21 +00:00
|
|
|
"""Pagina di creazione nuovo utente del sito:
|
|
|
|
come tutte le altre pagine di creazione del sito,
|
|
|
|
accetta GET per visualizzare la pagina
|
|
|
|
e POST con form data per l'aggiunta effettiva.
|
|
|
|
Le password vengono hashate con bcrypt."""
|
2017-09-12 08:48:21 +00:00
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 08:48:21 +00:00
|
|
|
if request.method == 'GET':
|
2017-09-12 09:11:19 +00:00
|
|
|
return render_template("user/add.htm", type="user", user=session["username"])
|
2017-09-12 08:48:21 +00:00
|
|
|
else:
|
|
|
|
p = bytes(request.form["passwd"], encoding="utf-8")
|
|
|
|
cenere = bcrypt.hashpw(p, bcrypt.gensalt())
|
|
|
|
nuovo = User(request.form['username'], cenere)
|
|
|
|
db.session.add(nuovo)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('page_user_list'))
|
|
|
|
|
|
|
|
|
2017-09-13 08:24:59 +00:00
|
|
|
@app.route('/query', methods=['GET', 'POST'])
|
|
|
|
def page_query():
|
|
|
|
"""Pagina delle query manuali:
|
|
|
|
in GET visualizza la pagina per fare una query,
|
|
|
|
mentre in POST visualizza i risultati."""
|
|
|
|
if 'username' not in session:
|
|
|
|
return abort(403)
|
|
|
|
if request.method == 'GET':
|
|
|
|
return render_template("query.htm", user=session["username"], type="query")
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
result = db.engine.execute("SELECT" + request.form["query"] + ";")
|
|
|
|
except Exception as e:
|
|
|
|
return render_template("query.htm", query=request.form["query"], error=repr(e), user=session["username"],
|
|
|
|
type="query")
|
|
|
|
return render_template("query.htm", query=request.form["query"], result=result, user=session["username"],
|
|
|
|
type="query")
|
|
|
|
|
|
|
|
|
2017-09-12 13:11:23 +00:00
|
|
|
@app.route('/smecds', methods=['GET'])
|
|
|
|
def page_smecds():
|
2017-09-12 10:50:04 +00:00
|
|
|
"""Pagina che visualizza i credits del sito"""
|
|
|
|
if 'username' not in session:
|
2017-09-13 07:23:26 +00:00
|
|
|
return abort(403)
|
2017-09-12 10:50:04 +00:00
|
|
|
if request.method == 'GET':
|
2017-09-12 13:11:23 +00:00
|
|
|
return render_template("smecds.htm", type="main", user=session["username"])
|
2017-09-12 10:50:04 +00:00
|
|
|
|
|
|
|
|
2017-09-13 07:06:59 +00:00
|
|
|
@app.errorhandler(403)
|
|
|
|
def page_403(_):
|
2017-09-13 08:24:59 +00:00
|
|
|
return render_template('403.htm', user=session["username"])
|
2017-09-13 07:06:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_404(_):
|
2017-09-13 08:24:59 +00:00
|
|
|
return render_template('404.htm', user=session["username"])
|
2017-09-13 07:06:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(500)
|
|
|
|
def page_500(e):
|
2017-09-13 08:24:59 +00:00
|
|
|
return render_template('500.htm', e=e, user=session["username"])
|
2017-09-13 07:06:59 +00:00
|
|
|
|
|
|
|
|
2017-09-11 10:47:52 +00:00
|
|
|
if __name__ == "__main__":
|
2017-09-12 09:39:21 +00:00
|
|
|
# Se non esiste il database, crealo e inizializzalo!
|
2017-09-12 07:39:18 +00:00
|
|
|
if not os.path.isfile("data.db"):
|
|
|
|
db.create_all()
|
|
|
|
try:
|
2017-09-12 09:39:21 +00:00
|
|
|
# L'utente predefinito è "stagista" "smecds".
|
2017-09-12 07:39:18 +00:00
|
|
|
nuovapassword = bcrypt.hashpw(b"smecds", bcrypt.gensalt())
|
|
|
|
nuovouser = User('stagista', nuovapassword)
|
|
|
|
db.session.add(nuovouser)
|
2017-09-12 09:39:21 +00:00
|
|
|
# Crea una rete nulla da utilizzare quando non ci sono altre reti disponibili
|
2017-09-12 07:39:18 +00:00
|
|
|
retenulla = Rete(nome="Sconosciuta", network_ip="0.0.0.0", subnet=0, primary_dns="0.0.0.0",
|
|
|
|
secondary_dns="0.0.0.0")
|
|
|
|
db.session.add(retenulla)
|
|
|
|
db.session.commit()
|
2017-09-12 09:41:13 +00:00
|
|
|
except IntegrityError:
|
2017-09-12 09:39:21 +00:00
|
|
|
# Se queste operazioni sono già state compiute in precedenza, annullale
|
2017-09-12 07:39:18 +00:00
|
|
|
db.session.rollback()
|
2017-09-12 09:39:21 +00:00
|
|
|
# Esegui il sito in modalità debug
|
2017-09-11 10:47:52 +00:00
|
|
|
app.run(debug=True)
|