def contest_submissions_view(contest_id, team_id=None): # Use DOMjudge team id so the pages also support non via_user teams if team_id and not ModuleAPI.can_write('domjudge'): return abort(403) session = DOMjudgeAPI.login(DOMJUDGE_ADMIN_USERNAME, DOMJUDGE_ADMIN_PASSWORD) if not team_id: team_id = DOMjudgeAPI.get_teamid_for_userid( current_user.id, 3, session) return render_contest_submissions_view(contest_id, team_id=team_id)
def contest_submissions_view(contest_id, team_id=None): # Use DOMjudge team id so the pages also support non via_user teams if team_id and not role_service.user_has_role(current_user, Roles.DOMJUDGE_ADMIN): return abort(403) session = DOMjudgeAPI.login(app.config['DOMJUDGE_ADMIN_USERNAME'], app.config['DOMJUDGE_ADMIN_PASSWORD']) if not team_id: team_id = DOMjudgeAPI.get_teamid_for_userid( current_user.id, 3, session) return render_contest_submissions_view(contest_id, team_id=team_id)
def render_contest_submissions_view(contest_id, view_all=False, team_id=None): admin = role_service.user_has_role(current_user, Roles.DOMJUDGE_ADMIN) if view_all and not admin: return abort(403) r = DOMjudgeAPI.request_get('api/contests') if not r: return redirect(url_for('domjudge.contest_list')) if str(contest_id) not in r.json(): flash(_("Contest does not exist."), 'danger') return redirect(url_for('domjudge.contest_list')) contest = r.json()[str(contest_id)] session = DOMjudgeAPI.login(app.config['DOMJUDGE_ADMIN_USERNAME'], app.config['DOMJUDGE_ADMIN_PASSWORD']) r = DOMjudgeAPI.request_get('api/submissions?cid={}'.format(contest_id), session=session) if not r: return render_template('domjudge/submissions.htm') submissions = r.json() r = DOMjudgeAPI.request_get('api/judgings?cid={}'.format(contest_id), session=session) if not r: return render_template('domjudge/submissions.htm') judgings = r.json() DOMjudgeAPI.logout(session) r = DOMjudgeAPI.request_get('api/languages') if not r: return render_template('domjudge/submissions.htm') languages = {} for lang in r.json(): languages[lang['id']] = lang teams = get_teams() if not teams: return render_template('domjudge/submissions.htm') r = DOMjudgeAPI.request_get('api/problems?cid={}'.format(contest_id)) if not r: return render_template('domjudge/submissions.htm') problems = {} for p in r.json(): problems[p['id']] = p judgings_dict = {} for j in judgings: judgings_dict[j['submission']] = j submission_states = {'queued': _('Pending'), 'pending': _('Pending'), 'judging': _('Judging'), 'too-late': _('Too late'), 'correct': _('Correct'), 'compiler-error': _('Compiler Error'), 'timelimit': _('Timelimit'), 'run-error': _('Run Error'), 'no-output': _('No Output'), 'wrong-answer': _('Wrong Answer') } for s in submissions: s['timestr'] = dt.datetime.fromtimestamp(s['time']).strftime(DT_FORMAT) m = VIA_USER_TEAM.match(teams[s['team']]['origname']) if m: s['userid'] = int(m.group(1)) else: s['userid'] = -1 s['team_id'] = teams[s['team']]['id'] s['team'] = teams[s['team']]['name'] s['problem'] = problems[s['problem']] s['language'] = languages[s['language']]['name'] s_id = s['id'] if s_id not in judgings_dict: outcome = 'pending' else: outcome = judgings_dict[s_id]['outcome'] if outcome == 'queued': outcome = 'pending' s['result'] = submission_states[outcome] s['result_class'] = 'domjudge-submission-' + outcome submissions.sort(key=lambda x: x['time'], reverse=True) return render_template('domjudge/submissions.htm', view_all=view_all, team=team_id, domjudge_url=app.config['DOMJUDGE_URL'], admin=admin, contest=contest, submissions=submissions)
def contest_problem_submit(contest_id, problem_id): r = DOMjudgeAPI.request_get('api/languages') if not r: return render_template('domjudge/problem/submit.htm', darken_color=darken_color, contest_id=contest_id) languages = r.json() r = DOMjudgeAPI.request_get('api/contests') if not r: return render_template('domjudge/problem/submit.htm', contest_id=contest_id) if str(contest_id) not in r.json(): flash(_("Contest does not exist."), 'danger') return redirect(url_for('domjudge.contest_list')) contest = r.json()[str(contest_id)] contest['start'] = dt.datetime.fromtimestamp(contest['start']) \ .strftime(DT_FORMAT) contest['end'] = dt.datetime.fromtimestamp(contest['end']) \ .strftime(DT_FORMAT) r = DOMjudgeAPI.request_get('api/problems?cid={}'.format(contest_id)) if not r: return render_template('domjudge/problem/submit.htm', darken_color=darken_color, contest_id=contest_id) problem = None for p in r.json(): if p['id'] == problem_id: problem = p break if not problem: flash(_('Problem does not exist.'), 'danger') return redirect(url_for('domjudge.contest_problems_list', darken_color=darken_color, contest_id=contest_id)) if request.method == 'POST': file = request.files.get('file', None) language = request.form.get('language', None) error = False if not file or file.filename == '': flash(_('No file uploaded.'), 'danger') error = True if not language: flash(_('Invalid language.'), 'danger') error = True else: valid = False for lang in languages: if lang['id'] == language: valid = True break if not valid: flash(_('Invalid language.'), 'danger') error = True if error: return render_template('domjudge/problem/submit.htm', problem=problem, contest=contest, contest_id=contest_id, darken_color=darken_color, languages=languages) dom_username = "******".format(current_user.id) dom_teamname = 'via_user_team_{}'.format(current_user.id) session = DOMjudgeAPI.login(dom_username, app.config['DOMJUDGE_USER_PASSWORD'], flash_on_error=False) # Check if user exists if not session: # User does not exist session = DOMjudgeAPI.login(app.config['DOMJUDGE_ADMIN_USERNAME'], app.config['DOMJUDGE_ADMIN_PASSWORD']) # Admin login failed, just give a 'request failed' error flash if not session: return render_template('domjudge/problem/submit.htm', darken_color=darken_color, contest_id=contest_id) # Get the id of the 'viaduct_user' team category viaduct_user_cat_id = DOMjudgeAPI.get_viaduct_category_id(session) if not viaduct_user_cat_id: flash('Team category viaduct_user not found on DOMjudge.', 'danger') return render_template('domjudge/problem/submit.htm', darken_color=darken_color, contest_id=contest_id) # Check if the team already exists. This should normally # not be the case, but things can go wrong if we # create a new team anyway, since team names are not unique. user_team_id = DOMjudgeAPI.get_teamid_for_user(dom_teamname, viaduct_user_cat_id, session) if not user_team_id: r = DOMjudgeAPI.add_team(dom_teamname, dom_username, viaduct_user_cat_id, session) if not r: return render_template('domjudge/problem/submit.htm', darken_color=darken_color, contest_id=contest_id) # Get the id of the newly created team user_team_id = DOMjudgeAPI.get_teamid_for_user( dom_teamname, viaduct_user_cat_id, session) # Create the user r = DOMjudgeAPI.add_user( dom_username, app.config['DOMJUDGE_USER_PASSWORD'], current_user.first_name + " " + current_user.last_name, current_user.email, user_team_id, session) if not r: return render_template('domjudge/problem/submit.htm', darken_color=darken_color, contest_id=contest_id) DOMjudgeAPI.logout(session) # Login as the new user session = DOMjudgeAPI.login(dom_username, app.config['DOMJUDGE_USER_PASSWORD']) if not session: return render_template('domjudge/problem/submit.htm', darken_color=darken_color, contest_id=contest_id) r = DOMjudgeAPI.submit(contest['shortname'], language, problem['shortname'], file, session) if not r: return render_template('domjudge/problem/submit.htm', darken_color=darken_color, contest_id=contest_id) flash(_("Submission successful.")) return redirect(url_for('domjudge.contest_view', darken_color=darken_color, contest_id=contest_id)) else: return render_template('domjudge/problem/submit.htm', darken_color=darken_color, **locals())