Détermination du bord politique de mots divers http://clivage.foxgl.ovh
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

111 lines
3.4 KiB

  1. from flask import Flask, abort, redirect, url_for, render_template
  2. from flask_migrate import Migrate
  3. from flask_sqlalchemy import SQLAlchemy
  4. from sqlalchemy import func, text
  5. from app.config import Config
  6. from app.locale import Locale
  7. from app.utils import WordResult, UIColor
  8. app = Flask(__name__, template_folder='../templates', static_folder='../static')
  9. app.config.from_object(Config)
  10. db = SQLAlchemy(app)
  11. migrate = Migrate(app, db)
  12. from app import models
  13. from app.models import Word
  14. @app.route('/')
  15. def home():
  16. return render_template("pages/home.html", color=UIColor.color_random())
  17. @app.route('/vote')
  18. @app.route('/vote/')
  19. def vote_index():
  20. word = db.session.query(Word).order_by(func.random()).first()
  21. return redirect(url_for('vote_from_all', id=word.id, word=word.text))
  22. @app.route('/vote/<string:word>')
  23. @app.route('/vote/<string:word>/')
  24. def vote_from_word(word: str):
  25. db_word = db.session.query(Word).filter_by(text=word).scalar()
  26. if db_word is not None:
  27. return redirect(url_for('vote_from_all', id=db_word.id, word=db_word.text))
  28. else:
  29. abort(404)
  30. @app.route('/vote/<int:id>')
  31. @app.route('/vote/<int:id>/')
  32. def vote_from_id(id: int):
  33. db_word = db.session.query(Word).filter_by(id=id).scalar()
  34. if db_word is not None:
  35. return redirect(url_for('vote_from_all', id=db_word.id, word=db_word.text))
  36. else:
  37. abort(404)
  38. @app.route('/vote/<int:id>/<string:word>')
  39. @app.route('/vote/<int:id>/<string:word>/')
  40. def vote_from_all(id: int, word: str):
  41. db_word = db.session.query(Word).filter_by(id=id, text=word).scalar()
  42. if db_word is not None:
  43. return render_template("pages/vote.html", color=UIColor.color_from(db_word.text), title=db_word.text,
  44. word=db_word,
  45. url_left=url_for('vote_for', id=id, word=word, vote="left"),
  46. url_right=url_for('vote_for', id=id, word=word, vote="right"),
  47. url_index=url_for('vote_index'))
  48. else:
  49. abort(404)
  50. @app.route('/vote/<int:id>/<string:word>/<string:vote>')
  51. @app.route('/vote/<int:id>/<string:word>/<string:vote>/')
  52. def vote_for(vote: str, id: int, word: str):
  53. print(word)
  54. db_word = db.session.query(Word).filter_by(id=id, text=word).scalar()
  55. if db_word is None:
  56. abort(404)
  57. if Locale.is_left(vote):
  58. print("left")
  59. db_word.left = Word.left + 1
  60. elif Locale.is_right(vote):
  61. print("right")
  62. db_word.right = Word.right + 1
  63. else:
  64. abort(400)
  65. db.session.commit()
  66. return redirect(url_for('vote_index'))
  67. @app.route('/results')
  68. @app.route('/results/')
  69. @app.route('/results/<string:sort>')
  70. @app.route('/results/<string:sort>/')
  71. def results_index(sort: str = "id"):
  72. if sort == "left":
  73. def sort_key(x):
  74. return x.left_percentage
  75. elif sort == "right":
  76. def sort_key(x):
  77. return x.right_percentage
  78. else:
  79. def sort_key(x):
  80. return x.word
  81. results = [
  82. WordResult(result.text, result.left, result.right, url_for('vote_from_all', id=result.id, word=result.text)) for
  83. result in
  84. db.session.query(Word).from_statement(text("SELECT * FROM word WHERE word.right <> 0 OR word.left <> 0"))]
  85. results.sort(key=sort_key, reverse=True)
  86. return render_template("pages/results.html", color=UIColor.color_random(), results=results)
  87. if __name__ == '__main__':
  88. app.run()