Example #1
0
def auto_remove(startpost, reason):
    invalidate_cache(startpost)
    if startpost.reply_to == None:
        for post in Post.objects.filter(thread=startpost.pk):
            if reason.cost == 0:
                PostVote.objects.filter(post=post).delete()
                PostEdit.objects.filter(post=post).delete()
                post.delete()
            else:
                PostVote(
                    user=User.objects.get(pk=1),
                    post=post,
                    reason=reason,
                    positive=False,
                    auto=True
                ).save()
                modify_rating(post, reason.cost)
    else:
        for post in Post.objects.filter(reply_to=startpost.pk):
            auto_remove(post, reason)

            if reason.cost == 0:
                PostVote.objects.filter(post=post).delete()
                PostEdit.objects.filter(post=post).delete()
                post.delete()
            else:
                PostVote(
                    user=User.objects.get(pk=1),
                    post=post,
                    reason=reason,
                    positive=False,
                    auto=True
                ).save()
                modify_rating(post, reason.cost)
Example #2
0
def invalidate_caches():
    """
    Invalidates all uwsgi caches
    """
    util.invalidate_cache(util.RUN_CACHE_NAME)
    util.invalidate_cache(util.SCORE_CACHE_NAME)
    return render_template("message.html", message="Invalidated cache")
Example #3
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"))
Example #4
0
 def save(self):
     from util import invalidate_cache
     super(Post, self).save()
     invalidate_cache(self)
Example #5
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
    """
    problem_type_id = request.form.get("problem_type_id")
    problem_type = model.ProblemType.query.filter_by(
        id=int(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=int(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
        db_session.add(problem)

    db_session.commit()

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

    return redirect(url_for("problems.problems_view"))