diff --git a/src/application.py b/src/application.py index 5e67deb..0be4744 100644 --- a/src/application.py +++ b/src/application.py @@ -15,12 +15,13 @@ You should have received a copy of the GNU General Public License along with scoreboard-benchmark . If not, see . """ -from src.models import Result -from src.models import db -import flask -import json import os +import flask + +from src.models import db +from views.errors import error_pages +from views.scoreboard import scoreboard # General Configurations app = flask.Flask(__name__) @@ -33,44 +34,7 @@ app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True db.init_app(app) -@app.before_first_request -def create_test_databases(): - db.drop_all() - db.create_all() - b1 = Result(text="asda", score=100) - b2 = Result(text="i7 flips flops", score=400) - db.session.add(b1) - db.session.add(b2) - db.session.commit() - - -@app.route("/upload", methods=['POST']) -def upload(): - content = flask.request.get_json() - try: - text = flask.escape(content['text']) - score = int(content['score']) - except KeyError: # Json doesn't contain the keys we need. - return json.dumps({'success': False}), 400, {'ContentType': 'application/json'} - except TypeError: # The types from the json object are not correct. - return json.dumps({'success': False}), 400, {'ContentType': 'application/json'} - - entry = Result(text=text, score=score) - db.session.add(entry) - db.session.commit() - - return json.dumps({'success': True}), 200, {'ContentType': 'application/json'} - - -@app.route("/") -def index(): - results = Result.query.all() - return flask.render_template("index.html", results=results) - - -@app.errorhandler(404) -def page_not_found_error(e): - return flask.render_template("404.html"), 404 - if __name__ == "__main__": + app.register_blueprint(scoreboard) + app.register_blueprint(error_pages) app.run("0.0.0.0") diff --git a/src/views/errors.py b/src/views/errors.py new file mode 100644 index 0000000..1c7e65f --- /dev/null +++ b/src/views/errors.py @@ -0,0 +1,24 @@ +""" + Author: Denis Nutiu + This file is part of scoreboard-benchmark. + + scoreboard-benchmark is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + scoreboard-benchmark is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with scoreboard-benchmark . If not, see . +""" +import flask + +error_pages = flask.Blueprint('error_pages', __name__, template_folder='templates') + +@error_pages.app_errorhandler(404) +def page_not_found_error(e): + return flask.render_template("404.html"), 404 diff --git a/src/views/scoreboard.py b/src/views/scoreboard.py new file mode 100644 index 0000000..d34f27a --- /dev/null +++ b/src/views/scoreboard.py @@ -0,0 +1,57 @@ +""" + Author: Denis Nutiu + This file is part of scoreboard-benchmark. + + scoreboard-benchmark is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + scoreboard-benchmark is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with scoreboard-benchmark . If not, see . +""" +from src.models import Result +from src.models import db +import json +import flask + +scoreboard = flask.Blueprint('scoreboard', __name__, template_folder='templates') + +# @scoreboard.before_first_request +# def create_test_databases(): +# db.drop_all() +# db.create_all() +# b1 = Result(text="asda", score=100) +# b2 = Result(text="i7 flips flops", score=400) +# db.session.add(b1) +# db.session.add(b2) +# db.session.commit() + + +@scoreboard.route("/upload", methods=['POST']) +def upload(): + content = flask.request.get_json() + try: + text = flask.escape(content['text']) + score = int(content['score']) + except KeyError: # Json doesn't contain the keys we need. + return json.dumps({'success': False}), 400, {'ContentType': 'application/json'} + except TypeError: # The types from the json object are not correct. + return json.dumps({'success': False}), 400, {'ContentType': 'application/json'} + + entry = Result(text=text, score=score) + db.session.add(entry) + db.session.commit() + + return json.dumps({'success': True}), 200, {'ContentType': 'application/json'} + + +@scoreboard.route("/") +def index(): + results = Result.query.all() + return flask.render_template("index.html", results=results)