"Before clean"

This commit is contained in:
Doug Le Tough 2018-02-28 10:25:08 +01:00
parent 738596a13f
commit ecfc6232a8
11 changed files with 279 additions and 36 deletions

View File

@ -54,7 +54,7 @@ class Tetawebapp_turns(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
role_id = db.Column(db.Integer, db.ForeignKey('participer_thsf_roles.id'), nullable=False) role_id = db.Column(db.Integer, db.ForeignKey('participer_thsf_roles.id'), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('participer_thsf_users.id'), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('participer_thsf_users.id'), nullable=True)
wday = db.Column(db.Enum('J', 'V', 'S', 'D'), nullable=False) wday = db.Column(db.Enum('Jeudi', 'Vendredi', 'Samedi', 'Dimanche'), nullable=False)
start_time = db.Column(db.Time, nullable=False) start_time = db.Column(db.Time, nullable=False)
end_time = db.Column(db.Time, nullable=False) end_time = db.Column(db.Time, nullable=False)
@ -73,21 +73,24 @@ def get_menu(page):
The value MUST be 0.""" The value MUST be 0."""
menu = [[u'Accueil', {u'/': [u'/']}, 0], menu = [[u'Accueil', {u'/': [u'/']}, 0],
[u'Mon compte', {u'/account': [u'/account', u'/account/update']}, 0], [u'Mon compte', {u'/account': [u'/account', u'/account/update']}, 0],
[u'Mes tours de staff', {u'/turn': [u'/turn']}, 0], [u'Mes tours de staff', {u'/turns': [u'/turns']}, 0],
[u'Feuilles de staff', {u'/staff_sheets': [u'/staff_sheet']}, 0], [u'Feuilles de staff', {u'/staff_sheets': [u'/staff_sheet']}, 0],
[u'Déconnexion', {u'/logout': [u'/logout']}, 0], [u'Déconnexion', {u'/logout': [u'/logout']}, 0],
] ]
if session['is_admin']: if session['is_admin']:
menu = [[u'Accueil', {u'/': [u'/']}, 0], menu = [[u'Accueil', {u'/': [u'/']}, 0],
[u'Tours de staff', {u'/staff': [u'/staff']}, 0], [u'Tours de staff', {u'/turns': [u'/turns', u'/turn/<ID>', u'/turn/new', u'/turn/add', u'/turn/delete/<ID>', u'/turn/update/<ID>']}, 0],
[u'Feuilles de staff', {u'/staff_sheets': [u'/staff_sheet']}, 0], [u'Feuilles de staff', {u'/staff_sheets': [u'/staff_sheet']}, 0],
[u'Liste des staffers', {u'/users': [u'/users', u'/account/<ID>']}, 0], [u'Liste des staffers', {u'/users': [u'/users', u'/account/<ID>', u'/account/delete/<ID>']}, 0],
[u'Déconnexion', {u'/logout': [u'/logout']}, 0], [u'Déconnexion', {u'/logout': [u'/logout']}, 0],
] ]
#~ print '[+] Page: %s' % page
for item in menu: for item in menu:
for url in item[1]: for url in item[1]:
for route in item[1][url]: for route in item[1][url]:
#~ print " [+] Route: %s" %route
if route == page: if route == page:
#~ print " [+] Selected page: %s" % page
item[2] = 1 item[2] = 1
return menu return menu
# This should never happen # This should never happen
@ -272,6 +275,44 @@ def delete_user(user_id):
print "------------------------------" print "------------------------------"
return False return False
def save_turn(role_id, day, start, end):
""" Save a new turn """
turn = Tetawebapp_turns(role_id=role_id.encode('utf-8'),
wday=day.encode('utf-8'),
start_time=start.encode('utf-8'),
end_time=end.encode('utf-8'),
)
try:
db.session.add(turn)
commit = db.session.commit()
except Exception as e:
db.session.rollback()
print "[+] Error at save_turn:"
print "------------------------------"
print "%s" % e.message
print "------------------------------"
return False
if commit != None:
return False
return True
def drop_turn(turn_id):
""" Delete staff turn """
try:
Tetawebapp_turns.query.filter_by(id=int(turn_id)).delete()
db.session.commit()
return True
except ValueError as e:
print e
return False
except Exception as e:
db.session.rollback()
print "[+] Error at drop_turn:"
print "------------------------------"
print "%s" % e.message
print "------------------------------"
return False
def check_user_info(): def check_user_info():
""" Check user info and send appropriate message if info are not complete""" """ Check user info and send appropriate message if info are not complete"""
message = '' message = ''
@ -302,6 +343,7 @@ def page_not_found(e):
@app.route("/login", methods=['GET', 'POST']) @app.route("/login", methods=['GET', 'POST'])
def login(): def login():
""" Login """
try: try:
login = request.form.get('login').encode('utf-8') login = request.form.get('login').encode('utf-8')
password = request.form.get('password').encode('utf-8') password = request.form.get('password').encode('utf-8')
@ -327,6 +369,7 @@ def login():
@app.route("/register", methods=['GET', 'POST']) @app.route("/register", methods=['GET', 'POST'])
def register(): def register():
""" Allow self registration """
try: try:
login = request.form.get('login').encode('utf-8') login = request.form.get('login').encode('utf-8')
password = request.form.get('password').encode('utf-8') password = request.form.get('password').encode('utf-8')
@ -402,6 +445,23 @@ def update_account():
except AttributeError: except AttributeError:
return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide") return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")
@app.route("/logout", methods=['GET', 'POST'])
@check_session
def logout():
""" Logout user """
# Remove session token
session['token'] = None
session['login'] = None
session['is_admin'] = 0
# Return user to index page
response = app.make_response(render_template('login_or_register.html', message=''))
# Push token to cookie
sync_cookies(response, session)
return response
########################################################################
# Admin zone
########################################################################
@app.route("/users", methods=['GET', 'POST']) @app.route("/users", methods=['GET', 'POST'])
@check_session @check_session
def list_users(): def list_users():
@ -476,21 +536,106 @@ def delete_account(ID):
# User is not logged in # User is not logged in
return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide") return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")
@app.route("/logout", methods=['GET', 'POST']) @app.route("/turns", methods=['GET', 'POST'])
@check_session @check_session
def logout(): def list_turn():
""" Logout user """ """ List staff turns """
# Remove session token try:
session['token'] = None if session['is_admin']:
session['login'] = None page = str(request.url_rule)
# Return user to index page menu = get_menu(page)
response = app.make_response(render_template('login_or_register.html', message='')) turns = Tetawebapp_turns.query.join(Tetawebapp_roles, Tetawebapp_turns.role_id==Tetawebapp_roles.id).add_columns(Tetawebapp_roles.role).order_by(Tetawebapp_turns.role_id).all()
# Push token to cookie message = ''
sync_cookies(response, session) return render_template('list_turns.html', menu=menu, page=page, turns=turns, message=message)
return response except AttributeError:
# User is not logged in
return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")
@app.route("/turn/new", methods=['GET', 'POST'])
@check_session
def new_turn():
""" New turn form """
try:
if session['is_admin']:
page = str(request.url_rule)
menu = get_menu(page)
roles = Tetawebapp_roles.query.order_by(Tetawebapp_roles.id).all()
days = ['Jeudi', 'Vendredi', 'Samedi', 'Dimanche']
return render_template('new_turn.html', menu=menu, page=page, roles=roles, days=days)
except AttributeError:
# User is not logged in
return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")
@app.route("/turn/add", methods=['GET', 'POST'])
@check_session
def add_turn():
""" Add staff turn """
try:
if session['is_admin']:
role_id = request.form.get('role_id').encode('utf-8')
day = request.form.get('day').encode('utf-8')
start = request.form.get('start').encode('utf-8')
end = request.form.get('end').encode('utf-8')
page = str(request.url_rule)
menu = get_menu(page)
turns = Tetawebapp_turns.query.join(Tetawebapp_roles, Tetawebapp_turns.role_id==Tetawebapp_roles.id).add_columns(Tetawebapp_roles.role).order_by(Tetawebapp_turns.role_id).all()
message = "Erreur lors de l'enregistrement.".decode('utf-8')
if save_turn(role_id, day, start, end):
turns = Tetawebapp_turns.query.join(Tetawebapp_roles, Tetawebapp_turns.role_id==Tetawebapp_roles.id).add_columns(Tetawebapp_roles.role).order_by(Tetawebapp_turns.role_id).all()
message=''
return render_template('list_turns.html', menu=menu, page=page, turns=turns, message=message)
# Error while saving turn
roles = Tetawebapp_roles.query.order_by(Tetawebapp_roles.id).all()
days = ['Jeudi', 'Vendredi', 'Samedi', 'Dimanche']
return render_template('new_turn.html', menu=menu, page=page, roles=roles, days=days, message=message)
# User is not admin
return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")
except AttributeError as e:
# User is not logged in
return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")
@app.route("/turn/<ID>", methods=['GET', 'POST'])
@check_session
def update_turn_by_id(ID):
#~ @app.route("/turn/update/<ID>", methods=['GET', 'POST'])
#~ @check_session
#~ def update_turn(ID):
#~ """ Update given staff turn """
#~ try:
#~ if session['is_admin']:
#~ page = str(request.url_rule)
#~ menu = get_menu(page)
#~ turn = Tetawebapp_turns.query.filter_by(id=ID).join(Tetawebapp_roles, Tetawebapp_turns.role_id==Tetawebapp_roles.id).add_columns(Tetawebapp_roles.role).order_by(Tetawebapp_turns.role_id).all()
#~ return render_template('update_turn.html', menu=menu, page=page, turn=turn)
#~ # User is not admin
#~ return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")
#~ except AttributeError:
#~ # User is not logged in
#~ return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")
@app.route("/turn/delete/<ID>", methods=['GET', 'POST'])
@check_session
def delete_turn(ID):
""" Delete given staff turn """
try:
if session['is_admin']:
message = 'Erreur lors de la suppression.'
page = str(request.url_rule)
menu = get_menu(page)
turns = Tetawebapp_turns.query.join(Tetawebapp_roles, Tetawebapp_turns.role_id==Tetawebapp_roles.id).add_columns(Tetawebapp_roles.role).order_by(Tetawebapp_turns.role_id).all()
if drop_turn(ID):
message = ''
turns = Tetawebapp_turns.query.join(Tetawebapp_roles, Tetawebapp_turns.role_id==Tetawebapp_roles.id).add_columns(Tetawebapp_roles.role).order_by(Tetawebapp_turns.role_id).all()
return render_template('list_turns.html', menu=menu, turns=turns, message=message)
return render_template('list_turns.html', menu=menu, turns=turns, message=message)
# User is not admin
return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")
except AttributeError:
# User is not logged in
return render_template('login_or_register.html', message="Utilisateur ou mot de passe invalide")

View File

@ -54,7 +54,7 @@ CREATE TABLE participer_thsf_roles (
\echo ********************************* \echo *********************************
\echo * Creating participer_thsf_turns table \echo * Creating participer_thsf_turns table
\echo ********************************* \echo *********************************
CREATE TYPE dow AS ENUM ('J', 'V', 'S', 'D'); CREATE TYPE dow AS ENUM ('Jeudi', 'Vendredi', 'Samedi', 'Dimanche');
CREATE TABLE participer_thsf_turns ( CREATE TABLE participer_thsf_turns (
id serial primary key, id serial primary key,
role_id integer not NULL, role_id integer not NULL,

View File

@ -37,3 +37,25 @@ function delete_account(id) {
document.location='/account/delete/'+id; document.location='/account/delete/'+id;
} }
} }
function delete_turn(id) {
if (confirm("La suppression d'un tour de staff est définitive.\n\nConfirmer ?")) {
document.location='/turn/delete/'+id;
}
}
function save_turn() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var s_start = start.split(':');
var s_end = end.split(':');
var regTime = new RegExp('^[0-9]{2}:[0-9]{2}:[0-9]{2}$','i');
if (! regTime.test(start) || s_start[0] > 23 || s_start[1] > 59 || s_start[2] > 59){
alert("Heure de début invalide.\n\nVeuillez respecter le format HH:MM:SS");
return false;
}
if (! regTime.test(end)){
alert("Heure de fin invalide.\n\nVeuillez respecter le format HH:MM:SS");
return false;
}
}

View File

@ -370,7 +370,7 @@ input.upload {
} }
form { form {
width: 400px; width: 450px;
text-align: center; text-align: center;
line-height: 40px; line-height: 40px;
} }
@ -379,8 +379,9 @@ form > label {
float: left; float: left;
} }
form > input[type='text'], form > input[type='password'] { form > input[type='text'], form > input[type='password'], form > select {
float: right; float: right;
width: 200px;
} }
div.table_header { div.table_header {

View File

@ -1,5 +1,5 @@
{% extends "index.html" %} {% extends "index.html" %}
{% block title %}Articles{% endblock %} {% block title %}Mon compte{% endblock %}
{% block main %} {% block main %}
<article> <article>
<h3>Informations personnelles</h3> <h3>Informations personnelles</h3>
@ -24,5 +24,6 @@
<label>Numéro de téléphone: </label><input id='phone' name='phone' type='text' value='{{ phone }}'/><br/> <label>Numéro de téléphone: </label><input id='phone' name='phone' type='text' value='{{ phone }}'/><br/>
<label>Régime alimentaire et remarques: </label><input id='diet' name='diet' type='text' value='{{ diet }}'/><br/> <label>Régime alimentaire et remarques: </label><input id='diet' name='diet' type='text' value='{{ diet }}'/><br/>
<input type='submit' value='Update' onclick='javascript:return update_account();'> <input type='submit' value='Update' onclick='javascript:return update_account();'>
</form>
</article> </article>
{% endblock %} {% endblock %}

View File

@ -1,5 +1,5 @@
{% extends "index.html" %} {% extends "index.html" %}
{% block title %}Articles{% endblock %} {% block title %}Modification de compte{% endblock %}
{% block main %} {% block main %}
<article> <article>
<h3>Informations du compte</h3> <h3>Informations du compte</h3>
@ -11,5 +11,6 @@
<label>Numéro de téléphone: </label><input id='phone' name='phone' type='text' value='{{ user.phone }}'/><br/> <label>Numéro de téléphone: </label><input id='phone' name='phone' type='text' value='{{ user.phone }}'/><br/>
<label>Régime alimentaire et remarques: </label><input id='diet' name='diet' type='text' value='{{ user.diet }}'/><br/> <label>Régime alimentaire et remarques: </label><input id='diet' name='diet' type='text' value='{{ user.diet }}'/><br/>
<input type='submit' value='Update' onclick='javascript:return update_account();'> <input type='submit' value='Update' onclick='javascript:return update_account();'>
</form>
</article> </article>
{% endblock %} {% endblock %}

29
templates/list_turns.html Normal file
View File

@ -0,0 +1,29 @@
{% extends "index.html" %}
{% block title %}Liste des tours de staff{% endblock %}
{% block main %}
<article>
<h3>Liste des tours de staff enregistrés <input class='add' value='' title='Ajouter un tour de staff' onclick='javascript:document.location="/turn/new"'/></h3>
<div class='table_header'>
<div class='border_right' style='width: 40px;'>ID</div>
<div class='border_right'>Role</div>
<div class='border_right'>Jour</div>
<div class='border_right'>Début</div>
<div class='border_right'>Fin</div>
<div style='width: 50px;'>Action</div>
</div>
{% set row_class = cycler('odd', 'even') %}
{% for turn in turns %}
<div class='table_row {{ row_class.next() }}'>
<div class='border_right' style='width: 40px;'>{{ turn[0].id }}</div>
<div class='border_right'>{{ turn[1] }}</div>
<div class='border_right'>{{ turn[0].wday }}</div>
<div class='border_right'>{{ turn[0].start_time }}</div>
<div class='border_right'>{{ turn[0].end_time }}</div>
<div style='width: 50px;'>
<input class='edit' value='' onclick='javascript:document.location="/turn/{{ turn[0].id }}"' title='Éditer'/>
<input class='trash' value='' onclick='javascript:delete_turn({{ turn[0].id }});' title='Supprimer'/>
</div>
</div>
{% endfor %}
</article>
{% endblock %}

View File

@ -1,5 +1,5 @@
{% extends "index.html" %} {% extends "index.html" %}
{% block title %}Articles{% endblock %} {% block title %}liste des utilisateurs{% endblock %}
{% block main %} {% block main %}
<article> <article>
<h3>Liste des staffers enregistrés</h3> <h3>Liste des staffers enregistrés</h3>

22
templates/new_turn.html Normal file
View File

@ -0,0 +1,22 @@
{% extends "index.html" %}
{% block title %}Nouveau tour de staff{% endblock %}
{% block main %}
<article>
<h3>Nouveau tour de staff</h3>
<form method='POST' action='/turn/add'>
<label>Role: </label><select id='role_id' name='role_id'>
{% for role in roles %}
<option value='{{ role.id }}'>{{ role.role }}</option>
{% endfor %}
</select><br/>
<label>Jour de la semaine: </label><select id='day' name='day'>
{% for day in days %}
<option value='{{ day }}'>{{ day }}</option>
{% endfor %}
</select><br/>
<label>Début (HH:MM:SS) </label><input id='start' name='start' type='text' maxlength=8/><br/>
<label>Fin: (HH:MM:SS) </label><input id='end' name='end' type='text' maxlength=8/><br/>
<input type='submit' value='Enregistrer' onclick='javascript:return save_turn();'/>
</form>
</article>
{% endblock %}

View File

@ -1,5 +1,5 @@
{% extends "index.html" %} {% extends "index.html" %}
{% block title %}Register{% endblock %} {% block title %}Inscription{% endblock %}
{% block nav %}{% endblock %} {% block nav %}{% endblock %}
{% block main %} {% block main %}
<article class='login'> <article class='login'>

View File

@ -0,0 +1,22 @@
{% extends "index.html" %}
{% block title %}Nouveau tour de staff{% endblock %}
{% block main %}
<article>
<h3>Miuse à jour du tour de staff</h3>
<form method='POST' action='/turn/update/{{ turn[0].id }}'>
<label>Role: </label><select id='role_id' name='role_id'>
{% for role in roles %}
<option value='{{ role.id }}'>{{ role.role }}</option>
{% endfor %}
</select><br/>
<label>Jour de la semaine: </label><select id='day' name='day'>
{% for day in days %}
<option value='{{ day }}'>{{ day }}</option>
{% endfor %}
</select><br/>
<label>Début (HH:MM:SS) </label><input id='start' name='start' type='text' maxlength=8/><br/>
<label>Fin: (HH:MM:SS) </label><input id='end' name='end' type='text' maxlength=8/><br/>
<input type='submit' value='Enregistrer' onclick='javascript:return save_turn();'/>
</form>
</article>
{% endblock %}