Beispiel #1
0
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())