forked from picoCTF/picoCTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoreboards.py
73 lines (59 loc) · 2.24 KB
/
scoreboards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Module for dealing with scoreboards.
An event can have multiple scoreboards with different eligibility criteria.
A team is initially eligible to appear on any scoreboards for which
its founding member is eligible.
When a new member joins the team, the team's eligibility for a given scoreboard
will be revoked if the new member does not fit its criteria.
(However, by default, members are prevented from joining a team if doing
so would cause the team to lose any existing scoreboard eligibilities.)
Scoreboards can also have optional metadata, such as a sponsor, logo, etc.
"""
import api
def get_all_scoreboards():
"""Return a list of all scoreboards in the database."""
db = api.db.get_conn()
scoreboards = db.scoreboards.find({}, {"_id": False})
if not scoreboards:
return []
else:
return list(scoreboards)
def get_scoreboard(sid):
"""Return a scoreboard from the database, or None if it does not exist."""
db = api.db.get_conn()
return db.scoreboards.find_one({"sid": sid}, {"_id": False})
def add_scoreboard(
name, eligibility_conditions=None, priority=1, sponsor=None, logo=None
):
"""
Add a scoreboard to the database.
Args:
name (str): name of the scoreboard
eligibility_conditions (dict): mongodb query to find eligible users
priority (int): optional, determines scoreboard tab ordering
sponsor (str): optional, sponsor of the scoreboard
logo (str): optional, URL of a logo image for the scoreboard
Returns:
ID of the newly created scoreboard
"""
if eligibility_conditions is None:
eligibility_conditions = {}
db = api.db.get_conn()
sid = api.common.token()
db.scoreboards.insert(
{
"sid": sid,
"name": name,
"eligibility_conditions": eligibility_conditions,
"priority": priority,
"sponsor": sponsor,
"logo": logo,
}
)
return sid
def is_eligible(user, scoreboard):
"""Determine whether a given user is eligible to appear on a scoreboard."""
search_query = scoreboard["eligibility_conditions"]
search_query["uid"] = user["uid"]
db = api.db.get_conn()
return db.users.find_one(search_query) is not None