Commit cd94cc7e authored by Sli's avatar Sli

Interface d'admin et modification de la bdd

parent 562f1d0f
......@@ -88,3 +88,7 @@ Réception :
"available": 7 // Nombre de places restantes sur le billet
}
```
## Interface d'administration
Il est possible d'accéder à /admin pour obtenir une liste de toutes les validations effectuées et d'annuler le passage d'un billet.
\ No newline at end of file
CREATE Table ticket (
id INTEGER PRIMARY KEY AUTOINCREMENT,
verifKey TEXT UNIQUE NOT NULL,
availablePlaces INTEGER NOT NULL
availablePlaces INTEGER NOT NULL,
totalPlaces INTEGER NOT NULL
);
This diff is collapsed.
This diff is collapsed.
......@@ -3,15 +3,25 @@
# @Author: klmp200
# @Date: 2016-07-03 17:57:28
# @Last Modified by: klmp200
# @Last Modified time: 2016-07-04 17:50:18
# @Last Modified time: 2016-11-04 21:16:27
from bottle import Bottle, static_file, request
from bottle import Bottle, static_file, request, template, redirect
from bottle.ext import sqlite
app = Bottle()
plugin = sqlite.Plugin(dbfile='../data/sqliteDB.db')
app.install(plugin)
def serialize_table(table):
obj = list()
dict_tmp = {}
for row in table:
dict_tmp = {}
for key in row.keys():
dict_tmp[key] = row[key]
obj.append(dict_tmp)
return obj
@app.route('/app.apk')
def GetApp():
return static_file("app.apk", root="../data/")
......@@ -26,14 +36,68 @@ def HomePage():
def GetKeys():
return static_file("keys.json", root="../data/")
@app.route('/delete/<db_id>')
def DeleteTicket(db, db_id=None):
"""
Delete a given ticket by id
"""
if db_id:
db.execute('DELETE from ticket where id=:id',
{"id": db_id})
redirect('/admin')
@app.route('/media/<file:path>')
def Media(file=""):
"""
Provide media files
"""
return static_file(file, root="./media/")
@app.route('/admin', method='GET')
def DisplayAdmin(db):
"""
Get data from database based on get data:
id where id the id in database
verifKey the verification key of the ticket
"""
table = []
form = {}
form['key'] = ""
form['id'] = ""
if request.query.verifKey:
request.query.verifKey = request.query.verifKey.upper()
form['key'] = request.query.verifKey
if request.query.id:
form['id'] = request.query.id
if not request.query.id and not request.query.verifKey:
table = db.execute('SELECT * from ticket').fetchall()
elif request.query.id and not request.query.verifKey:
table = db.execute('SELECT * from ticket where id=:id',
{"id": request.query.id}).fetchall()
elif not request.query.id and request.query.verifKey:
table = db.execute('SELECT * from ticket where verifKey=:key',
{"key": request.query.verifKey})
else:
table = db.execute('SELECT * from ticket where verifKey=:key and id=:id',
{"key": request.query.verifKey,
"id": request.query.id})
tickets = serialize_table(table)
tickets = tickets[::-1]
if request.query.ajax:
return dict(data=tickets)
else:
return template('admin.simple', table=tickets, form=form)
@app.route('/validate', method='POST')
def Validate(db):
try:
send = request.json
ticket = db.execute('SELECT * from ticket where verifKey=:key',
{"key": send['verif']}).fetchone()
ticket = db.execute('SELECT * from ticket where verifKey=:key and totalPlaces=:nb',
{"key": send['verif'], "nb": send['nb']}).fetchone()
if (ticket is None):
message = NewEntry(db, send)
......@@ -53,8 +117,8 @@ def NewEntry(db, data):
"valid": True
}
db.execute('INSERT into ticket(verifKey, availablePlaces) values (?, ?)',
(data['verif'], available))
db.execute('INSERT into ticket(verifKey, availablePlaces, totalPlaces) values (?, ?, ?)',
(data['verif'], available, data['nb']))
return dict(response)
......
<!DOCTYPE html>
<html>
<head>
<title>Admin</title>
<link rel="stylesheet" type="text/css" href="/media/css/pure-min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="pure-g">
<div class="pure-u-1-24 pure-u-md-1-12 pure-u-sm-1-24"></div>
<div class="pure-u-22-24 pure-u-md-10-12 pure-u-sm-22-24">
<h1>Database administration</h1>
<h2>Recherche</h2>
<form class="pure-form" method="get" action="/admin">
Recherche par id :
<input type="text" name="id" value="{{form['id']}}"/>
Clef de vérification :
<input type="text" name="verifKey" value="{{form['key']}}"/>
<button type="submit" class="pure-button pure-button-primary">Rechercher</button>
</form>
<h2>Données</h2>
<table class="pure-table pure-table-bordered">
<thead>
<tr>
<th>Clef de vérification</th>
<th>Places restantes</th>
<th>Places total disponible</th>
<th>Supprimer ticket</th>
</tr>
</thead>
<tbody id="Tickets">
% for element in table:
<tr>
<td>{{element['verifKey']}}</td>
<td>{{element['availablePlaces']}}</td>
<td>{{element['totalPlaces']}}</td>
<td><a href='/delete/{{element["id"]}}' class="pure-button pure-button-primary">Supprimer</a></td>
</tr>
% end
</tbody>
</table>
</div>
<div class="pure-u-1-24 pure-u-md-1-12 pure-u-sm-1-24"></div>
</div>
<footer>
<script type="text/javascript" src="/media/js/jquery.js"></script>
<script type="text/javascript">
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
</script>
<script>
setInterval(function (){
var args = {
"id": getUrlParameter('id'),
"verifKey": getUrlParameter('verifKey'),
"ajax": 'l'
};
$.get("/admin", args).done(function(data){
var to_write = "";
data['data'].forEach(function(ticket){
to_write += "<tr>";
to_write += "<td>" + ticket['verifKey'] + "</td>";
to_write += "<td>" + ticket['availablePlaces'] + "</td>";
to_write += "<td>" + ticket['totalPlaces'] + "</td>";
to_write += "<td><a href='/delete/" + ticket['id'] + "' class='pure-button pure-button-primary'>Supprimer</a></td>";
to_write += "</tr>";
});
$("#Tickets").empty();
$("#Tickets").prepend(to_write);
});
}, 1000);
</script>
</footer>
</body>
</html>
\ No newline at end of file
Markdown is supported
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