Rudimentary implementation of config interface
This commit is contained in:
parent
5b549e0e70
commit
bc6c1247e1
9 changed files with 108 additions and 5 deletions
|
@ -3,6 +3,7 @@ import logger
|
||||||
import models
|
import models
|
||||||
import problem
|
import problem
|
||||||
import user
|
import user
|
||||||
|
import settings
|
||||||
import stats
|
import stats
|
||||||
import team
|
import team
|
||||||
import utils
|
import utils
|
||||||
|
|
|
@ -3,6 +3,8 @@ from decorators import admins_only, api_wrapper
|
||||||
from models import db, Problems, Files
|
from models import db, Problems, Files
|
||||||
from schemas import verify_to_schema, check
|
from schemas import verify_to_schema, check
|
||||||
|
|
||||||
|
import settings
|
||||||
|
|
||||||
import cPickle as pickle
|
import cPickle as pickle
|
||||||
|
|
||||||
blueprint = Blueprint("admin", __name__)
|
blueprint = Blueprint("admin", __name__)
|
||||||
|
@ -28,6 +30,16 @@ def problem_data():
|
||||||
problems_return.sort(key=lambda prob: prob["value"])
|
problems_return.sort(key=lambda prob: prob["value"])
|
||||||
return { "success": 1, "problems": problems_return }
|
return { "success": 1, "problems": problems_return }
|
||||||
|
|
||||||
|
@blueprint.route("/settings", methods=["GET"])
|
||||||
|
@api_wrapper
|
||||||
|
@admins_only
|
||||||
|
def settings_data():
|
||||||
|
# data = settings.get_all()
|
||||||
|
settings_return = {}
|
||||||
|
settings_return["ctf_begin"] = settings.get("ctf_begin")
|
||||||
|
settings_return["ctf_end"] = settings.get("ctf_end")
|
||||||
|
return { "success": 1, "settings": settings_return }
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@blueprint.route("/problems/submit", methods=["POST"])
|
@blueprint.route("/problems/submit", methods=["POST"])
|
||||||
@api_wrapper
|
@api_wrapper
|
||||||
|
|
|
@ -211,3 +211,12 @@ class TeamInvitations(db.Model):
|
||||||
self.rtype = rtype
|
self.rtype = rtype
|
||||||
self.frid = frid
|
self.frid = frid
|
||||||
self.toid = toid
|
self.toid = toid
|
||||||
|
|
||||||
|
class Settings(db.Model):
|
||||||
|
sid = db.Column(db.Integer, primary_key=True)
|
||||||
|
key = db.Column(db.Text, unique=True)
|
||||||
|
value = db.Column(db.Text)
|
||||||
|
|
||||||
|
def __init__(self, key, value):
|
||||||
|
self.key = key
|
||||||
|
self.value = value
|
||||||
|
|
36
server/api/settings.py
Normal file
36
server/api/settings.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
from flask import Blueprint, request
|
||||||
|
|
||||||
|
from models import db, Settings
|
||||||
|
from decorators import admins_only, api_wrapper, WebException
|
||||||
|
|
||||||
|
blueprint = Blueprint("setting", __name__)
|
||||||
|
|
||||||
|
@blueprint.route("/update", methods=["POST"])
|
||||||
|
@admins_only
|
||||||
|
@api_wrapper
|
||||||
|
def update_setting():
|
||||||
|
for key in request.form.keys():
|
||||||
|
value = request.form[key]
|
||||||
|
set(key, value)
|
||||||
|
return { "success": 1, "message": "Success!" }
|
||||||
|
|
||||||
|
def set(key, value):
|
||||||
|
setting = Settings.query.filter_by(key=key).first()
|
||||||
|
if setting:
|
||||||
|
setting.value = value
|
||||||
|
else:
|
||||||
|
setting = Settings(key, value)
|
||||||
|
db.session.add(setting)
|
||||||
|
db.session.commit()
|
||||||
|
return setting
|
||||||
|
|
||||||
|
def get_all():
|
||||||
|
settings = {}
|
||||||
|
for setting in Settings.query.all():
|
||||||
|
settings[setting.key] = setting.value
|
||||||
|
|
||||||
|
def get(key):
|
||||||
|
setting = Settings.query.filter_by(key=key).first()
|
||||||
|
if setting:
|
||||||
|
return setting.value
|
||||||
|
return set(key, None)
|
|
@ -24,7 +24,7 @@ if not os.path.exists("pfp"):
|
||||||
os.makedirs("pfp")
|
os.makedirs("pfp")
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
from api.models import db, Files, Teams, Problems, Solves, Users
|
from api.models import db, Files, Teams, Problems, Settings, Solves, Users
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@ app.secret_key = config.SECRET_KEY
|
||||||
|
|
||||||
app.register_blueprint(api.admin.blueprint, url_prefix="/api/admin")
|
app.register_blueprint(api.admin.blueprint, url_prefix="/api/admin")
|
||||||
app.register_blueprint(api.problem.blueprint, url_prefix="/api/problem")
|
app.register_blueprint(api.problem.blueprint, url_prefix="/api/problem")
|
||||||
|
app.register_blueprint(api.settings.blueprint, url_prefix="/api/settings")
|
||||||
app.register_blueprint(api.stats.blueprint, url_prefix="/api/stats")
|
app.register_blueprint(api.stats.blueprint, url_prefix="/api/stats")
|
||||||
app.register_blueprint(api.team.blueprint, url_prefix="/api/team")
|
app.register_blueprint(api.team.blueprint, url_prefix="/api/team")
|
||||||
app.register_blueprint(api.user.blueprint, url_prefix="/api/user")
|
app.register_blueprint(api.user.blueprint, url_prefix="/api/user")
|
||||||
|
|
|
@ -18,9 +18,6 @@ SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
UPLOAD_FOLDER = os.path.normpath("../web/files")
|
UPLOAD_FOLDER = os.path.normpath("../web/files")
|
||||||
GRADER_FOLDER = os.path.normpath("graders")
|
GRADER_FOLDER = os.path.normpath("graders")
|
||||||
|
|
||||||
CTF_BEGIN = 0 # To be used later
|
|
||||||
CTF_END = 0 # To be used later
|
|
||||||
|
|
||||||
MG_HOST = ""
|
MG_HOST = ""
|
||||||
MG_API_KEY = ""
|
MG_API_KEY = ""
|
||||||
ADMIN_EMAIL = ""
|
ADMIN_EMAIL = ""
|
||||||
|
|
|
@ -68,6 +68,10 @@ app.config(function($routeProvider, $locationProvider) {
|
||||||
templateUrl: "pages/admin/problems.html",
|
templateUrl: "pages/admin/problems.html",
|
||||||
controller: "adminProblemsController"
|
controller: "adminProblemsController"
|
||||||
})
|
})
|
||||||
|
.when("/admin/settings", {
|
||||||
|
templateUrl: "pages/admin/settings.html",
|
||||||
|
controller: "adminSettingsController"
|
||||||
|
})
|
||||||
.otherwise({
|
.otherwise({
|
||||||
templateUrl: "pages/404.html",
|
templateUrl: "pages/404.html",
|
||||||
controller: "mainController"
|
controller: "mainController"
|
||||||
|
@ -247,6 +251,16 @@ app.controller("adminProblemsController", ["$controller", "$scope", "$http", fun
|
||||||
});
|
});
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
|
app.controller("adminSettingsController", ["$controller", "$scope", "$http", function($controller, $scope, $http) {
|
||||||
|
$controller("adminController", { $scope: $scope });
|
||||||
|
api_call("GET", "/api/admin/settings", {}, function(result) {
|
||||||
|
if (result["success"] == 1) {
|
||||||
|
$scope.settings = result["settings"];
|
||||||
|
}
|
||||||
|
$scope.$apply();
|
||||||
|
});
|
||||||
|
}]);
|
||||||
|
|
||||||
app.controller("settingsController", ["$controller", "$scope", "$http", function($controller, $scope, $http) {
|
app.controller("settingsController", ["$controller", "$scope", "$http", function($controller, $scope, $http) {
|
||||||
$controller("loginController", { $scope: $scope });
|
$controller("loginController", { $scope: $scope });
|
||||||
api_call("GET", "/api/user/info", {}, function(result) {
|
api_call("GET", "/api/user/info", {}, function(result) {
|
||||||
|
|
33
web/pages/admin/settings.html
Normal file
33
web/pages/admin/settings.html
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
<div>
|
||||||
|
<h1>CTF Settings</h1>
|
||||||
|
<div ng-if="settings">
|
||||||
|
<div id="settings_msg"/>
|
||||||
|
<form class="form-horizontal" onsubmit="update_settings(); return false;" id="update_settings_form">
|
||||||
|
<input class="form-control" name="ctf_begin" value="{{ settings['ctf_begin'] }}"/>
|
||||||
|
<br>
|
||||||
|
<input class="form-control" name="ctf_end" value="{{ settings['ctf_end'] }}"/>
|
||||||
|
<br>
|
||||||
|
<input type="submit" class="btn btn-success" value="Update"/>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var update_settings = function() {
|
||||||
|
var input = "#update_settings_form form input";
|
||||||
|
var data = $("#update_settings_form").serializeObject();
|
||||||
|
$(input).attr("disabled", "disabled");
|
||||||
|
api_call("POST", "/api/settings/update", data, function(result) {
|
||||||
|
if (result["success"] == 1) {
|
||||||
|
display_message("settings_msg", "success", result["message"], function() {
|
||||||
|
$(input).removeAttr("disabled");
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
display_message("settings_msg", "danger", result["message"], function() {
|
||||||
|
$(input).removeAttr("disabled");
|
||||||
|
});
|
||||||
|
console.log(result["error"]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
Loading…
Reference in a new issue