mirror of
https://github.com/Project-Redacted/Highscores-Server.git
synced 2025-06-05 17:53:12 +00:00
First Commit
This commit is contained in:
parent
7efaf276e4
commit
fda7ff28dd
19 changed files with 735 additions and 0 deletions
64
server/views.py
Normal file
64
server/views.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
from flask import Blueprint, jsonify, render_template_string
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, IntegerField
|
||||
from wtforms.validators import DataRequired
|
||||
|
||||
from server.models import Scores
|
||||
from server.extensions import db, cache
|
||||
|
||||
blueprint = Blueprint('views', __name__)
|
||||
|
||||
|
||||
class ScoreForm(FlaskForm):
|
||||
score = IntegerField('Score', validators=[DataRequired()])
|
||||
difficulty = StringField('Difficulty', validators=[DataRequired()])
|
||||
achievements = StringField('Achievements', validators=[DataRequired()])
|
||||
|
||||
|
||||
@blueprint.route('/', methods=['GET'])
|
||||
@cache.cached(timeout=60)
|
||||
def index():
|
||||
top_scores = Scores.query.order_by(Scores.score.desc()).limit(10).all()
|
||||
return render_template_string('''
|
||||
<h1>Top Scores</h1>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Score</th>
|
||||
<th>Difficulty</th>
|
||||
<th>Achievements</th>
|
||||
</tr>
|
||||
{% for score in top_scores %}
|
||||
<tr>
|
||||
<td>{{ score.score }}</td>
|
||||
<td>{{ score.difficulty }}</td>
|
||||
<td>{{ score.achievements }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
<a href="/post">Post a score</a>
|
||||
''', top_scores=top_scores)
|
||||
|
||||
|
||||
@blueprint.route('/post', methods=['GET', 'POST'])
|
||||
def post():
|
||||
form = ScoreForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
score = Scores(
|
||||
score=form.score.data,
|
||||
difficulty=form.difficulty.data,
|
||||
achievements=form.achievements.data,
|
||||
)
|
||||
db.session.add(score)
|
||||
db.session.commit()
|
||||
return jsonify({'message': 'Success!'})
|
||||
|
||||
return render_template_string('''
|
||||
<form method="POST" action="/post">
|
||||
{{ form.csrf_token }}
|
||||
{{ form.score.label }} {{ form.score(size=20) }}
|
||||
{{ form.difficulty.label }} {{ form.difficulty(size=20) }}
|
||||
{{ form.achievements.label }} {{ form.achievements(size=20) }}
|
||||
<input type="submit" value="Go">
|
||||
</form>
|
||||
''', form=form)
|
Loading…
Add table
Add a link
Reference in a new issue