Exemple #1
0
def add_contest():
    """
    Adds or edits a contest

    Note:
        must be called from within a request context

    Returns:
        a redirect to the contest view page
    """
    name = request.form.get("name")
    activate_date = request.form.get("activate_date")
    activate_time = request.form.get("activate_time")
    start_date = request.form.get("start_date")
    start_time = request.form.get("start_time")
    freeze_date = request.form.get("freeze_date")
    freeze_time = request.form.get("freeze_time")
    end_date = request.form.get("end_date")
    end_time = request.form.get("end_time")
    deactivate_date = request.form.get("deactivate_date")
    deactivate_time = request.form.get("deactivate_time")
    is_public = request.form.get("is_public")
    user_usernames = request.form.get("users")
    problem_slugs = request.form.get("problems")

    if activate_date is not "" and activate_time is not "":
        activate_date_time = util.strs_to_dt(activate_date, activate_time)
    else:
        activate_date_time = None

    if freeze_date is not "" and freeze_time is not "":
        freeze_date_time = util.strs_to_dt(freeze_date, freeze_time)
    else:
        freeze_date_time = None

    if deactivate_date is not "" and deactivate_time is not "":
        deactivate_date_time = util.strs_to_dt(deactivate_date,
                                               deactivate_time)
    else:
        deactivate_date_time = None

    if name is None:
        error = "Failed to add contest due to undefined contest name."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("contests.contests_view"))

    # convert is_public to a bool
    is_public_bool = util.checkbox_result_to_bool(is_public)
    if is_public_bool is None:
        error = "Failed to add contest '{}' due to invalid is_public check.".format(
            name)
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("contests.contests_view"))

    contest_id = util.i(request.form.get("contest_id"))
    if contest_id:  # edit
        contest = model.Contest.query.filter_by(id=int(contest_id)).one()
        contest.name = name
        contest.is_public = is_public_bool

        contest.activate_time = activate_date_time
        contest.start_time = util.strs_to_dt(start_date, start_time)
        contest.freeze_time = freeze_date_time
        contest.end_time = util.strs_to_dt(end_date, end_time)
        contest.deactivate_time = deactivate_date_time

        contest.users = users_from_usernames(user_usernames.split(), model)
        contest.problems = problems_from_slugs(problem_slugs.split(), model)
    else:  # add
        if is_dup_contest_name(name):
            error = "Failed to add contest '{}' as contest already exists.".format(
                name)
            current_app.logger.info(error)
            flash(error, "danger")
            return redirect(url_for("contests.contests_view"))

        contest = model.Contest(
            name=name,
            is_public=is_public_bool,
            activate_time=activate_date_time,
            start_time=util.strs_to_dt(start_date, start_time),
            freeze_time=freeze_date_time,
            end_time=util.strs_to_dt(end_date, end_time),
            deactivate_time=deactivate_date_time,
            users=users_from_usernames(user_usernames.split(), model),
            problems=problems_from_slugs(problem_slugs.split(), model),
        )
        db_session.add(contest)

    db_session.commit()

    # If a problem is added to a contest, the cached runs will be
    # inaccurate. Clear the run cache to fix this.
    util.invalidate_cache(util.RUN_CACHE_NAME)

    return redirect(url_for("contests.contests_view"))
Exemple #2
0
def add_problem():
    """
    Adds or edits a problem

    Note:
        must be called from within a request context

    Returns:
        a redirect to the problem view page
    """
    model = util.get_model()

    problem_type_id = request.form.get("problem_type_id")
    problem_type = model.ProblemType.query.filter_by(id=problem_type_id).one()
    slug = request.form.get("slug")
    name = request.form.get("name")
    is_enabled = request.form.get("is_enabled")
    problem_statement = request.form.get("problem_statement")
    sample_input = request.form.get("sample_input")
    sample_output = request.form.get("sample_output")
    secret_input = request.form.get("secret_input")
    secret_output = request.form.get("secret_output")

    if problem_type is None:
        error = "Failed to add problem due to undefined problem_type."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("problems.problems_view"))

    if name is None:
        error = "Failed to add problem due to undefined problem name."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("problems.problems_view"))

    is_enabled_bool = util.checkbox_result_to_bool(is_enabled)
    if is_enabled_bool is None:
        error = "Failed to add problem \'{}\' due to invalid is_enabled check.".format(
            name)
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("problems.problems_view"))

    if slug is None:
        error = "Failed to add problem due to undefined problem slug."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("problems.problems_view"))

    problem_id = request.form.get('problem_id')
    if problem_id:  # edit
        problem = model.Problem.query.filter_by(id=problem_id).one()
        problem.problem_type = problem_type
        problem.slug = slug
        problem.name = name
        problem.is_enabled = is_enabled_bool
        problem.problem_statement = problem_statement
        problem.sample_input = sample_input
        problem.sample_output = sample_output
        problem.secret_input = secret_input
        problem.secret_output = secret_output
    else:  # add
        # check if is duplicate
        if is_dup_problem_slug(slug):
            error = "Failed to add problem \'{}\' as problem already exists.".format(
                slug)
            current_app.logger.info(error)
            flash(error, "danger")
            return redirect(url_for("problems.problems_view"))

        problem = model.Problem(problem_type, slug, name, problem_statement,
                                sample_input, sample_output, secret_input,
                                secret_output)
        problem.is_enabled = is_enabled_bool
        model.db.session.add(problem)

    model.db.session.commit()

    return redirect(url_for("problems.problems_view"))
Exemple #3
0
    def test_checkbox_result_to_bool(self):
        """test the checkbox_result_to_bool function"""

        self.assertEqual(util.checkbox_result_to_bool("on"), True)
        self.assertEqual(util.checkbox_result_to_bool("off"), False)
Exemple #4
0
def add_lang():
    """
    Adds or edits a language

    Note:
        must be called from within a request context

    Returns:
        a redirect to the language view page
    """
    name = request.form.get("name")
    syntax_mode = request.form.get("syntax_mode")
    is_enabled = request.form.get("is_enabled")
    run_script = request.form.get("run_script", "").replace('\r\n', '\n')
    default_template = request.form.get("default_template",
                                        "").replace('\r\n', '\n')

    if name is None:
        error = "Failed to add language due to undefined language name."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("languages.languages_view"))

    if syntax_mode is None:
        error = "Failed to add language due to undefined syntax_mode."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("languages.languages_view"))

    # convert is_enabled to a bool
    is_enabled_bool = util.checkbox_result_to_bool(is_enabled)
    if is_enabled_bool is None:
        error = "Failed to add language \'{}\' due to invalid is_enabled check.".format(
            name)
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("languages.languages_view"))

    lang_id = util.i(request.form.get('lang_id'))
    if lang_id:  # edit
        lang = model.Language.query.filter_by(id=lang_id).one()
        lang.name = name
        lang.syntax_mode = syntax_mode
        lang.is_enabled = is_enabled_bool
        lang.run_script = run_script
        lang.default_template = default_template
    else:  # add
        if is_dup_lang_name(name):
            error = "Failed to add language \'{}\' as language already exists.".format(
                name)
            current_app.logger.info(error)
            flash(error, "danger")
            return redirect(url_for("languages.languages_view"))

        lang = model.Language(name, syntax_mode, is_enabled_bool, run_script,
                              default_template)
        db_session.add(lang)

    db_session.commit()

    return redirect(url_for("languages.languages_view"))
Exemple #5
0
def add_contest():
    """
    Adds or edits a contest

    Note:
        must be called from within a request context

    Returns:
        a redirect to the contest view page
    """
    model = util.get_model()

    name = request.form.get("name")
    activate_date = request.form.get("activate_date")
    activate_time = request.form.get("activate_time")
    start_date = request.form.get("start_date")
    start_time = request.form.get("start_time")
    freeze_date = request.form.get("freeze_date")
    freeze_time = request.form.get("freeze_time")
    end_date = request.form.get("end_date")
    end_time = request.form.get("end_time")
    deactivate_date = request.form.get("deactivate_date")
    deactivate_time = request.form.get("deactivate_time")
    is_public = request.form.get("is_public")
    user_emails = request.form.get("users")
    problem_slugs = request.form.get("problems")

    if activate_date is not "" and activate_time is not "":
        activate_date_time = model.strs_to_dt(activate_date, activate_time)
    else:
        activate_date_time = None

    if freeze_date is not "" and freeze_time is not "":
        freeze_date_time = model.strs_to_dt(freeze_date, freeze_time)
    else:
        freeze_date_time = None

    if deactivate_date is not "" and deactivate_time is not "":
        deactivate_date_time = model.strs_to_dt(deactivate_date,
                                                deactivate_time)
    else:
        deactivate_date_time = None

    if name is None:
        error = "Failed to add contest due to undefined contest name."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("contests.contests_view"))

    # convert is_public to a bool
    is_public_bool = util.checkbox_result_to_bool(is_public)
    if is_public_bool is None:
        error = "Failed to add contest \'{}\' due to invalid is_public check.".format(
            name)
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("contests.contests_view"))

    contest_id = request.form.get('contest_id')
    if contest_id:  # edit
        contest = model.Contest.query.filter_by(id=contest_id).one()
        contest.name = name
        contest.is_public = is_public_bool

        contest.activate_time = activate_date_time
        contest.start_time = model.strs_to_dt(start_date, start_time)
        contest.freeze_time = freeze_date_time
        contest.end_time = model.strs_to_dt(end_date, end_time)
        contest.deactivate_time = deactivate_date_time

        contest.users = users_from_emails(user_emails.split(), model)
        contest.problems = problems_from_slugs(problem_slugs.split(), model)
    else:  # add
        if is_dup_contest_name(name):
            error = "Failed to add contest \'{}\' as contest already exists.".format(
                name)
            current_app.logger.info(error)
            flash(error, "danger")
            return redirect(url_for("contests.contests_view"))

        contest = model.Contest(
            name=name,
            is_public=is_public_bool,
            activate_time=activate_date_time,
            start_time=model.strs_to_dt(start_date, start_time),
            freeze_time=freeze_date_time,
            end_time=model.strs_to_dt(end_date, end_time),
            deactivate_time=deactivate_date_time,
            users=users_from_emails(user_emails.split(), model),
            problems=problems_from_slugs(problem_slugs.split(), model))
        model.db.session.add(contest)

    model.db.session.commit()

    return redirect(url_for("contests.contests_view"))