Skip to content

Commit 59635e0

Browse files
eliasolveluca93
authored andcommitted
Add rws public config and configurable id column
1 parent fcb70ee commit 59635e0

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

cmsranking/Config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# You should have received a copy of the GNU Affero General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from dataclasses import dataclass
19+
from dataclasses import dataclass, field
2020
import logging
2121
import os
2222
import sys
@@ -37,6 +37,11 @@ def default_path(name):
3737
return os.path.join(sys.prefix, name)
3838

3939

40+
@dataclass
41+
class PublicConfig:
42+
show_id_column: bool = False
43+
44+
4045
@dataclass
4146
class Config:
4247
# Connection.
@@ -51,6 +56,9 @@ class Config:
5156
username: str = "usern4me"
5257
password: str = "passw0rd"
5358

59+
# UI.
60+
public: PublicConfig = field(default_factory=PublicConfig)
61+
5462
# Buffers
5563
buffer_size: int = 100
5664

cmsranking/RankingWebServer.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
# Needed for initialization. Do not remove.
4343
import cmsranking.Logger # noqa
4444
from cmscommon.eventsource import EventSource
45-
from cmsranking.Config import load_config
45+
from cmsranking.Config import PublicConfig, load_config
4646
from cmsranking.Contest import Contest
4747
from cmsranking.Entity import InvalidData
4848
from cmsranking.Scoring import ScoringStore
@@ -473,6 +473,32 @@ def wsgi_app(self, environ, start_response):
473473
return response
474474

475475

476+
class PublicConfigHandler:
477+
478+
def __init__(self, pub_config: PublicConfig):
479+
self.pub_config = pub_config
480+
481+
def __call__(self, environ, start_response):
482+
return self.wsgi_app(environ, start_response)
483+
484+
@responder
485+
def wsgi_app(self, environ, start_response):
486+
request = Request(environ)
487+
request.encoding_errors = "strict"
488+
489+
response = Response()
490+
response.status_code = 200
491+
response.mimetype = "application/json"
492+
print(str(self.pub_config))
493+
response.data = json.dumps(
494+
self.pub_config,
495+
default=lambda o: o.__dict__,
496+
sort_keys=True,
497+
)
498+
499+
return response
500+
501+
476502
class RoutingHandler:
477503

478504
def __init__(
@@ -482,20 +508,23 @@ def __init__(
482508
logo_handler: ImageHandler,
483509
score_handler: ScoreHandler,
484510
history_handler: HistoryHandler,
511+
public_config_handler: PublicConfigHandler,
485512
):
486513
self.router = Map([
487514
Rule("/", methods=["GET"], endpoint="root"),
488515
Rule("/history", methods=["GET"], endpoint="history"),
489516
Rule("/scores", methods=["GET"], endpoint="scores"),
490517
Rule("/events", methods=["GET"], endpoint="events"),
491518
Rule("/logo", methods=["GET"], endpoint="logo"),
519+
Rule("/config", methods=["GET"], endpoint="public_config")
492520
], encoding_errors="strict")
493521

494522
self.event_handler = event_handler
495523
self.logo_handler = logo_handler
496524
self.score_handler = score_handler
497525
self.history_handler = history_handler
498526
self.root_handler = root_handler
527+
self.public_config_handler = public_config_handler
499528

500529
def __call__(self, environ, start_response):
501530
return self.wsgi_app(environ, start_response)
@@ -517,6 +546,8 @@ def wsgi_app(self, environ, start_response):
517546
return self.score_handler(environ, start_response)
518547
elif endpoint == "history":
519548
return self.history_handler(environ, start_response)
549+
elif endpoint == 'public_config':
550+
return self.public_config_handler(environ, start_response)
520551

521552

522553
def main() -> int:
@@ -593,7 +624,8 @@ def main() -> int:
593624
os.path.join(config.lib_dir, '%(name)s'),
594625
os.path.join(web_dir, 'img', 'logo.png')),
595626
ScoreHandler(stores),
596-
HistoryHandler(stores))
627+
HistoryHandler(stores),
628+
PublicConfigHandler(config.public))
597629

598630
wsgi_app = SharedDataMiddleware(DispatcherMiddleware(
599631
toplevel_handler, {

cmsranking/static/Config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,24 @@ var Config = new function () {
7373
self.get_history_url = function () {
7474
return "history";
7575
}
76+
77+
self.get_public_config_url = function () {
78+
return "config";
79+
}
80+
};
81+
82+
var PublicConfig = {
83+
show_id_column: false
7684
};
85+
86+
$.ajax({
87+
url: Config.get_public_config_url(),
88+
dataType: "json",
89+
async: false,
90+
success: function (data, status, xhr) {
91+
PublicConfig = data;
92+
},
93+
error: function () {
94+
console.error("Error while getting public configuration data");
95+
}
96+
});

cmsranking/static/Scoreboard.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ var Scoreboard = new function () {
194194
<th class=\"sel\"></th> \
195195
<th class=\"rank\">Rank</th> \
196196
<th colspan=\"10\" class=\"f_name\">First Name</th> \
197-
<th colspan=\"10\" class=\"l_name\">Last Name</th> \
198-
<th class=\"user_id\">ID</th> \
199-
<th class=\"team\">Team</th>";
197+
<th colspan=\"10\" class=\"l_name\">Last Name</th>" +
198+
(PublicConfig.show_id_column ? "<th class=\"user_id\">ID</th>" : "") +
199+
"<th class=\"team\">Team</th>";
200200

201201
var contests = DataStore.contest_list;
202202
for (var i in contests) {
@@ -244,8 +244,8 @@ var Scoreboard = new function () {
244244
<td class=\"sel\"></td> \
245245
<td class=\"rank\">" + user["rank"] + "</td> \
246246
<td colspan=\"10\" class=\"f_name\">" + escapeHTML(user["f_name"]) + "</td> \
247-
<td colspan=\"10\" class=\"l_name\">" + escapeHTML(user["l_name"]) + "</td> \
248-
<td class=\"user_id\">" + user["key"] + "</td>";
247+
<td colspan=\"10\" class=\"l_name\">" + escapeHTML(user["l_name"]) + "</td>" +
248+
(PublicConfig.show_id_column ? "<td class=\"user_id\">" + user["key"] + "</td>" : "");
249249

250250
if (user['team']) {
251251
result += " \

config/cms_ranking.sample.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ buffer_size = 100
2525
#log_dir = "INSTALL_DIR/log/ranking"
2626
# Data directory (the scoreboard data is stored here).
2727
#lib_dir = "INSTALL_DIR/lib/ranking"
28+
29+
# UI
30+
[public]
31+
show_id_column = false

0 commit comments

Comments
 (0)