Minor refactor

This commit is contained in:
James Wang 2016-01-17 02:19:02 +00:00
parent 6d0219782b
commit f1d476b713
3 changed files with 11 additions and 11 deletions

View file

@ -40,7 +40,7 @@ def api_wrapper(f):
response = make_response(result) response = make_response(result)
# Setting CSRF token # Setting CSRF token
if "token" not in session: if "csrf_token" not in session:
token = utils.generate_string() token = utils.generate_string()
response.set_cookie("csrf_token", token) response.set_cookie("csrf_token", token)
session["csrf_token"] = token session["csrf_token"] = token

View file

@ -7,7 +7,7 @@ from flask import current_app as app
from werkzeug import secure_filename from werkzeug import secure_filename
from models import db, Files, Problems, Solves, Teams from models import db, Files, Problems, Solves, Teams
from decorators import admins_only, api_wrapper, login_required from decorators import admins_only, api_wrapper, login_required, WebException
blueprint = Blueprint("problem", __name__) blueprint = Blueprint("problem", __name__)
@ -24,7 +24,7 @@ def problem_add():
name_exists = Problems.query.filter_by(name=name).first() name_exists = Problems.query.filter_by(name=name).first()
if name_exists: if name_exists:
return { "success": 0, "message": "Problem name already taken." } raise WebException("Problem name already taken.")
problem = Problems(name, category, description, hint, flag, value) problem = Problems(name, category, description, hint, flag, value)
db.session.add(problem) db.session.add(problem)
db.session.commit() db.session.commit()
@ -57,7 +57,7 @@ def problem_delete():
Problems.query.filter_by(pid=pid).delete() Problems.query.filter_by(pid=pid).delete()
db.session.commit() db.session.commit()
return { "success": 1, "message": "Success!" } return { "success": 1, "message": "Success!" }
return { "success": 0, "message": "Problem does not exist!" } raise WebException("Problem does not exist!")
@blueprint.route("/update", methods=["POST"]) @blueprint.route("/update", methods=["POST"])
@admins_only @admins_only
@ -86,7 +86,7 @@ def problem_update():
db.session.commit() db.session.commit()
return { "success": 1, "message": "Success!" } return { "success": 1, "message": "Success!" }
return { "success": 0, "message": "Problem does not exist!" } raise WebException("Problem does not exist!")
@blueprint.route("/submit", methods=["POST"]) @blueprint.route("/submit", methods=["POST"])
@api_wrapper @api_wrapper
@ -113,10 +113,10 @@ def problem_submit():
else: else:
logger.log("submissions.log", logger.WARNING, "%s has incorrectly submitted %s to %s" % (team.name, flag, problem.name)) logger.log("submissions.log", logger.WARNING, "%s has incorrectly submitted %s to %s" % (team.name, flag, problem.name))
return { "success": 0, "message": "Incorrect." } raise WebException("Incorrect.")
else: else:
return { "success": 0, "message": "Problem does not exist!" } raise WebException("Problem does not exist!")
@blueprint.route("/data", methods=["POST"]) @blueprint.route("/data", methods=["POST"])
#@api_wrapper # Disable atm due to json serialization issues: will fix #@api_wrapper # Disable atm due to json serialization issues: will fix

View file

@ -27,7 +27,7 @@ def user_forgot_password(token=None):
if token is not None: if token is not None:
user = get_user(reset_token=token).first() user = get_user(reset_token=token).first()
if user is None: if user is None:
return { "success": 0, "message": "Invalid reset token"} raise WebException("Invalid reset token.")
# We are viewing the actual reset form # We are viewing the actual reset form
if request.method == "GET": if request.method == "GET":
@ -38,7 +38,7 @@ def user_forgot_password(token=None):
password = params.get("password") password = params.get("password")
confirm_password = params.get("confirm_password") confirm_password = params.get("confirm_password")
if password != confirm_password: if password != confirm_password:
return { "success": 0, "message": "Passwords do not match." } raise WebException("Passwords do not match.")
else: else:
user.password = utils.hash_password(password) user.password = utils.hash_password(password)
user.reset_token = None user.reset_token = None
@ -51,7 +51,7 @@ def user_forgot_password(token=None):
user = get_user(email=email).first() user = get_user(email=email).first()
if user is None: if user is None:
return { "success": 0, "message": "User with that email does not exist." } raise WebException("User with that email does not exist.")
token = utils.generate_string(length=64) token = utils.generate_string(length=64)
user.reset_token = token user.reset_token = token
@ -66,7 +66,7 @@ def user_forgot_password(token=None):
if "Queued" in response["message"]: if "Queued" in response["message"]:
return { "success": 1, "message": "Email sent to %s" % email } return { "success": 1, "message": "Email sent to %s" % email }
else: else:
return { "success": 0, "message": response["message"] } raise WebException(response["message"])
@blueprint.route("/register", methods=["POST"]) @blueprint.route("/register", methods=["POST"])
@api_wrapper @api_wrapper