Autocomplétion

This commit is contained in:
klafyvel 2018-01-13 15:45:29 +01:00 committed by root
parent 79191a406e
commit e16e4be753
2 changed files with 44 additions and 14 deletions

View file

@ -10,6 +10,7 @@ import MySQLdb
from time import time, localtime, strftime from time import time, localtime, strftime
import locale import locale
import random import random
import json
# configuration # configuration
DEBUG = True DEBUG = True
@ -164,11 +165,8 @@ def get_players_not_banned():
not_banned = [{'id': row[0], 'firstname': row[1], 'name': row[2]} for row in rows] not_banned = [{'id': row[0], 'firstname': row[1], 'name': row[2]} for row in rows]
# Ensuite on applique les règles d'immunité # Ensuite on applique les règles d'immunité
with open(IMMUNITY_FILE, 'r') as f:
immunity = f.read()
result = [] result = []
for user in not_banned: for user in not_banned:
if user['firstname']+' '+user['name'] not in immunity:
result.append(user) result.append(user)
return result return result
@ -419,5 +417,18 @@ def play():
else: else:
return render_template('precampagne.html', user=player,stats=statistiques()) return render_template('precampagne.html', user=player,stats=statistiques())
@app.route('/complete')
def complete():
v = request.args['v']
con = connect_sql()
cur = con.cursor()
cur.execute("""select id, firstname, name from players where
name like "{value}%" or firstname like "{value}%";""".format(value=v))
rows = [[i[0], i[1]+' '+i[2]] for i in cur.fetchall()]
con.close()
return json.dumps(rows)
if __name__ == '__main__': if __name__ == '__main__':
app.run() app.run()

View file

@ -1,19 +1,38 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block content %} {% block content %}
<script>
$(function(){
$('#word').keyup(function(e){
var input = $(this).val();
$.ajax({
type: "get",
url: "complete",
data: {v: input},
async: true,
success: function(data){
var outWords = $.parseJSON(data);
$('#auto').html('');
for(x = 0; x < outWords.length; x++){
$('#auto').prepend('<option value="' + outWords[x][0] + '">'+outWords[x][1]+'</option>'); //Fills the #auto div with the options
}
}
})
})
});
</script>
<h2>Trancher un joueur</h2> <h2>Trancher un joueur</h2>
<p> <p>
Ta cible sera notifiée quelle que soit l'issue de ta tentative. Question Ta cible sera notifiée quelle que soit l'issue de ta tentative. Question
chance, c'est 50-50. chance, c'est 50-50.
</p> </p>
<form id="select" action="" method="post"> <form id="select" action="" method="post">
<select name="target_id"> <label for="word">Écrit le nom de ta victime </label>
<p><option value="none">(sélectionner un joueur)</option> <input type="text" name="search" id="word">
{% for player in players %} <label for="auto">et choisit-la :</label>
<option value="{{ player.id }}"> <select id="auto" name="target_id">
{{ "%s %s" % (player.firstname, player.name) }} <option value="none">(sélectionner un joueur)</option>
</option></p> </select>
{% endfor %}
</select>
<input type="submit" value="Tchak !" /> <input type="submit" value="Tchak !" />
</form> </form>
<h2> <h2>