|
|
@ -1,3 +1,5 @@ |
|
|
|
from string import ascii_lowercase |
|
|
|
|
|
|
|
from flask import Flask, abort, redirect, url_for, render_template |
|
|
|
from flask_migrate import Migrate |
|
|
|
from flask_sqlalchemy import SQLAlchemy |
|
|
@ -65,17 +67,14 @@ def vote_from_all(id: int, word: str): |
|
|
|
@app.route('/vote/<int:id>/<string:word>/<string:vote>') |
|
|
|
@app.route('/vote/<int:id>/<string:word>/<string:vote>/') |
|
|
|
def vote_for(vote: str, id: int, word: str): |
|
|
|
print(word) |
|
|
|
db_word = db.session.query(Word).filter_by(id=id, text=word).scalar() |
|
|
|
|
|
|
|
if db_word is None: |
|
|
|
abort(404) |
|
|
|
|
|
|
|
if Locale.is_left(vote): |
|
|
|
print("left") |
|
|
|
db_word.left = Word.left + 1 |
|
|
|
elif Locale.is_right(vote): |
|
|
|
print("right") |
|
|
|
db_word.right = Word.right + 1 |
|
|
|
else: |
|
|
|
abort(400) |
|
|
@ -86,24 +85,57 @@ def vote_for(vote: str, id: int, word: str): |
|
|
|
|
|
|
|
@app.route('/results') |
|
|
|
@app.route('/results/') |
|
|
|
@app.route('/results/<string:sort>') |
|
|
|
@app.route('/results/<string:sort>/') |
|
|
|
def results_index(sort: str = "id"): |
|
|
|
if sort == "left": |
|
|
|
def sort_key(x): |
|
|
|
return x.left_percentage |
|
|
|
elif sort == "right": |
|
|
|
def sort_key(x): |
|
|
|
return x.right_percentage |
|
|
|
else: |
|
|
|
def sort_key(x): |
|
|
|
return x.word |
|
|
|
@app.route('/results/<string:filter>') |
|
|
|
@app.route('/results/<string:filter>/') |
|
|
|
@app.route('/results/<int:nb>') |
|
|
|
@app.route('/results/<int:nb>/') |
|
|
|
@app.route('/results/<int:offset>/<int:nb>') |
|
|
|
@app.route('/results/<int:offset>/<int:nb>/') |
|
|
|
@app.route('/results/<int:nb>/<string:filter>') |
|
|
|
@app.route('/results/<int:nb>/<string:filter>/') |
|
|
|
@app.route('/results/<int:offset>/<int:nb>/<string:filter>') |
|
|
|
@app.route('/results/<int:offset>/<int:nb>/<string:filter>/') |
|
|
|
def results_index(nb=100, offset=0, filter=""): |
|
|
|
offset = abs(offset) |
|
|
|
nb = min(100, abs(nb)) |
|
|
|
if not not filter: |
|
|
|
if filter.lower() not in ascii_lowercase: |
|
|
|
filter = "" |
|
|
|
|
|
|
|
link_prev = url_for("results_index", nb=nb, offset=max(offset - nb, 0), filter=filter) if filter else url_for( |
|
|
|
"results_index", nb=nb, offset=max(offset - nb, 0)) |
|
|
|
link_next = url_for("results_index", nb=nb, offset=offset + nb, filter=filter) if filter else url_for( |
|
|
|
"results_index", nb=nb, offset=offset + nb) |
|
|
|
|
|
|
|
results = [ |
|
|
|
WordResult(result.text, result.left, result.right, url_for('vote_from_all', id=result.id, word=result.text)) for |
|
|
|
result in |
|
|
|
db.session.query(Word).from_statement(text("SELECT * FROM word WHERE word.right <> 0 OR word.left <> 0"))] |
|
|
|
results.sort(key=sort_key, reverse=True) |
|
|
|
return render_template("pages/results.html", color=UIColor.color_random(), results=results) |
|
|
|
db.session.query(Word).from_statement(text( |
|
|
|
"SELECT * FROM word WHERE (word.right <> 0 OR word.left <> 0) AND (word.text LIKE '{0}%' OR word.text LIKE '{1}%') LIMIT {2},{3}".format( |
|
|
|
filter.lower(), filter.upper(), str(offset), str(nb))))] |
|
|
|
return render_template("pages/results.html", color=UIColor.color_random(), results=results, next=link_next, |
|
|
|
prev=link_prev, letters=ascii_lowercase, min_result=offset, max_result=offset + nb, |
|
|
|
filter=filter) |
|
|
|
|
|
|
|
|
|
|
|
# @app.route('/results/s/<string:sort>') |
|
|
|
# @app.route('/results/s/<string:sort>/') |
|
|
|
# def results_sorted(sort: str = "id"): |
|
|
|
# if sort == "left": |
|
|
|
# def sort_key(x): |
|
|
|
# return 100 - x.right_percentage |
|
|
|
# elif sort == "right": |
|
|
|
# def sort_key(x): |
|
|
|
# return 100 - x.left_percentage |
|
|
|
# else: |
|
|
|
# def sort_key(x): |
|
|
|
# return x.word |
|
|
|
# results = [ |
|
|
|
# WordResult(result.text, result.left, result.right, url_for('vote_from_all', id=result.id, word=result.text)) for |
|
|
|
# result in |
|
|
|
# db.session.query(Word).from_statement(text("SELECT * FROM word WHERE word.right <> 0 OR word.left <> 0"))] |
|
|
|
# results.sort(key=sort_key) |
|
|
|
# return render_template("pages/results.html", color=UIColor.color_random(), results=results) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|