abhishek thakur commited on
Commit
a1d6fad
·
unverified ·
1 Parent(s): c42fc7e

Allow admins to always see LB (#26)

Browse files
competitions/app.py CHANGED
@@ -62,6 +62,11 @@ class User(BaseModel):
62
  user_token: str
63
 
64
 
 
 
 
 
 
65
  class UserSubmissionUpdate(BaseModel):
66
  user_token: str
67
  submission_ids: str
@@ -167,9 +172,15 @@ async def get_submission_info(request: Request):
167
  return resp
168
 
169
 
170
- @app.get("/leaderboard/{lb}", response_class=JSONResponse)
171
- async def get_leaderboard(request: Request, lb: str):
172
- if DISABLE_PUBLIC_LB == 1 and lb == "public":
 
 
 
 
 
 
173
  return {"response": "Public leaderboard is disabled by the competition host."}
174
 
175
  leaderboard = Leaderboard(
@@ -180,12 +191,12 @@ async def get_leaderboard(request: Request, lb: str):
180
  token=HF_TOKEN,
181
  scoring_metric=COMP_INFO.scoring_metric,
182
  )
183
- if lb == "private":
184
- current_utc_time = datetime.datetime.utcnow()
185
- if current_utc_time < COMP_INFO.end_date:
186
  return {"response": "Private leaderboard will be available after the competition ends."}
187
- df = leaderboard.fetch(private=lb == "private")
188
- logger.info(df)
189
  if len(df) == 0:
190
  return {"response": "No teams yet. Why not make a submission?"}
191
  resp = {"response": df.to_markdown(index=False)}
@@ -261,7 +272,7 @@ async def new_submission(
261
  start_date = datetime.datetime.strptime(START_DATE, "%Y-%m-%d")
262
  if todays_date < start_date:
263
  comp_org = COMPETITION_ID.split("/")[0]
264
- if not utils.can_user_submit_before_start(token, comp_org):
265
  return {"response": "Competition has not started yet!"}
266
 
267
  sub = Submissions(
 
62
  user_token: str
63
 
64
 
65
+ class UserLB(BaseModel):
66
+ user_token: str
67
+ lb: str
68
+
69
+
70
  class UserSubmissionUpdate(BaseModel):
71
  user_token: str
72
  submission_ids: str
 
172
  return resp
173
 
174
 
175
+ @app.post("/leaderboard", response_class=JSONResponse)
176
+ async def fetch_leaderboard(request: Request, user_lb: UserLB):
177
+ if USE_OAUTH == 1:
178
+ if request.session.get("oauth_info") is not None:
179
+ user_lb.user_token = request.session.get("oauth_info")["access_token"]
180
+
181
+ is_user_allowed = utils.is_user_admin(user_lb.user_token, COMPETITION_ID)
182
+
183
+ if DISABLE_PUBLIC_LB == 1 and user_lb.lb == "public" and not is_user_allowed:
184
  return {"response": "Public leaderboard is disabled by the competition host."}
185
 
186
  leaderboard = Leaderboard(
 
191
  token=HF_TOKEN,
192
  scoring_metric=COMP_INFO.scoring_metric,
193
  )
194
+ if user_lb.lb == "private":
195
+ current_utc_time = datetime.datetime.now()
196
+ if current_utc_time < COMP_INFO.end_date and not is_user_allowed:
197
  return {"response": "Private leaderboard will be available after the competition ends."}
198
+ df = leaderboard.fetch(private=user_lb.lb == "private")
199
+
200
  if len(df) == 0:
201
  return {"response": "No teams yet. Why not make a submission?"}
202
  resp = {"response": df.to_markdown(index=False)}
 
272
  start_date = datetime.datetime.strptime(START_DATE, "%Y-%m-%d")
273
  if todays_date < start_date:
274
  comp_org = COMPETITION_ID.split("/")[0]
275
+ if not utils.is_user_admin(token, comp_org):
276
  return {"response": "Competition has not started yet!"}
277
 
278
  sub = Submissions(
competitions/templates/index.html CHANGED
@@ -66,19 +66,32 @@
66
  articleLoadingSpinner.classList.add('hidden');
67
  });
68
  }
69
-
70
- function fetchAndDisplayPublicLeaderboard() {
71
  const articleLoadingSpinner = document.getElementById('articleLoadingSpinner');
72
  articleLoadingSpinner.classList.remove('hidden');
73
- fetch('/leaderboard/public')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  .then(response => {
75
  if (!response.ok) {
76
  throw new Error('Network response was not ok');
77
  }
78
- return response.json(); // Parse the JSON response
79
  })
80
  .then(data => {
81
- // Populate the 'content' div with the HTML from the response
82
  const contentDiv = document.getElementById('content');
83
  contentDiv.innerHTML = marked.parse(data.response);
84
  articleLoadingSpinner.classList.add('hidden');
@@ -89,27 +102,14 @@
89
  });
90
  }
91
 
 
 
 
 
92
  function fetchAndDisplayPrivateLeaderboard() {
93
- const articleLoadingSpinner = document.getElementById('articleLoadingSpinner');
94
- articleLoadingSpinner.classList.remove('hidden');
95
- fetch('/leaderboard/private')
96
- .then(response => {
97
- if (!response.ok) {
98
- throw new Error('Network response was not ok');
99
- }
100
- return response.json(); // Parse the JSON response
101
- })
102
- .then(data => {
103
- // Populate the 'content' div with the HTML from the response
104
- const contentDiv = document.getElementById('content');
105
- contentDiv.innerHTML = marked.parse(data.response);
106
- articleLoadingSpinner.classList.add('hidden');
107
- })
108
- .catch(error => {
109
- console.error('There has been a problem with your fetch operation:', error);
110
- articleLoadingSpinner.classList.add('hidden');
111
- });
112
  }
 
113
  function fetchAndDisplaySubmissions() {
114
  const userToken = document.getElementById('user_token').value;
115
  const apiEndpoint = '/my_submissions';
 
66
  articleLoadingSpinner.classList.add('hidden');
67
  });
68
  }
69
+ function fetchAndDisplayLeaderboard(leaderboardType) {
 
70
  const articleLoadingSpinner = document.getElementById('articleLoadingSpinner');
71
  articleLoadingSpinner.classList.remove('hidden');
72
+
73
+ const userToken = document.getElementById('user_token').value;
74
+ const payload = {
75
+ user_lb: {
76
+ lb: leaderboardType,
77
+ user_token: userToken
78
+ }
79
+ };
80
+
81
+ fetch('/leaderboard', {
82
+ method: 'POST',
83
+ headers: {
84
+ 'Content-Type': 'application/json'
85
+ },
86
+ body: JSON.stringify(payload)
87
+ })
88
  .then(response => {
89
  if (!response.ok) {
90
  throw new Error('Network response was not ok');
91
  }
92
+ return response.json();
93
  })
94
  .then(data => {
 
95
  const contentDiv = document.getElementById('content');
96
  contentDiv.innerHTML = marked.parse(data.response);
97
  articleLoadingSpinner.classList.add('hidden');
 
102
  });
103
  }
104
 
105
+ function fetchAndDisplayPublicLeaderboard() {
106
+ fetchAndDisplayLeaderboard('public');
107
+ }
108
+
109
  function fetchAndDisplayPrivateLeaderboard() {
110
+ fetchAndDisplayLeaderboard('private');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  }
112
+
113
  function fetchAndDisplaySubmissions() {
114
  const userToken = document.getElementById('user_token').value;
115
  const apiEndpoint = '/my_submissions';
competitions/utils.py CHANGED
@@ -224,7 +224,7 @@ def install_requirements(requirements_fname):
224
  return
225
 
226
 
227
- def can_user_submit_before_start(user_token, competition_organization):
228
  user_info = user_authentication(token=user_token)
229
  user_orgs = user_info.get("orgs", [])
230
  for org in user_orgs:
 
224
  return
225
 
226
 
227
+ def is_user_admin(user_token, competition_organization):
228
  user_info = user_authentication(token=user_token)
229
  user_orgs = user_info.get("orgs", [])
230
  for org in user_orgs: