from code.util import register, auth

def root(params, setHeader, user):
    setHeader("Location", "/problems")
    return 302

def login(params, setHeader, user):
    username = params["username"]
    password = params["password"]
    user = auth.checkPassword(username, password)
    if user:
        setHeader("Set-Cookie", f"user={user.id}")
        setHeader("Set-Cookie", f"userType={user.type}")
        return "ok"
    else:
        return "Incorrect username / password";

def logout(params, setHeader, user):
    setHeader("Location", "/login")
    setHeader("Set-Cookie", "user=deleted; expires=Thu, 01 Jan 1970 00:00:00 GMT;")
    setHeader("Set-Cookie", "userType=deleted; expires=Thu, 01 Jan 1970 00:00:00 GMT")
    return 302

register.get("/", "loggedin", root)
register.post("/login", "any", login)
register.get("/logout", "any", logout)
Exemple #2
0
from code.util import register
from code.util.db import Contest, Problem
import json


def deleteContest(params, setHeader, user):
    id = params["id"]
    Contest.get(id).delete()
    return "ok"


def editContest(params, setHeader, user):
    id = params.get("id")
    contest = Contest.get(id) or Contest()

    contest.name = params["name"]
    contest.start = int(params["start"])
    contest.end = int(params["end"])
    contest.scoreboardOff = int(params["scoreboardOff"])
    contest.problems = [
        Problem.get(id) for id in json.loads(params["problems"])
    ]

    contest.save()

    return contest.id


register.post("/deleteContest", "admin", deleteContest)
register.post("/editContest", "admin", editContest)
Exemple #3
0
    lang = params["language"]
    code = params["code"]
    type = params["type"]
    submission = addSubmission(probId, lang, code, user, type)
    runCode(submission)
    return submission.toJSON()


def changeResult(params, setHeader, user):
    id = params["id"]
    sub = Submission.get(id)
    if not sub:
        return "Error: incorrect id"
    sub.result = params["result"]
    sub.save()
    return "ok"


def rejudge(params, setHeader, user):
    id = params["id"]
    submission = Submission.get(id)
    if os.path.exists(f"/tmp/{id}"):
        shutil.rmtree(f"/tmp/{id}")
    runCode(submission)
    return submission.result


register.post("/submit", "loggedin", submit)
register.post("/changeResult", "admin", changeResult)
register.post("/rejudge", "admin", rejudge)
def submit(params, setHeader, user):
    probId = params["problem"]
    lang   = params["language"]
    code   = params["code"]
    type   = params["type"]
    submission = addSubmission(probId, lang, code, user, type)
    runCode(submission)
    return submission.toJSON()

def changeResult(params, setHeader, user):
    id = params["id"]
    sub = Submission.get(id)
    if not sub:
        return "Error: incorrect id"
    sub.result = params["result"]
    sub.save()
    return "ok"

def rejudge(params, setHeader, user):
    id = params["id"]
    submission = Submission.get(id)
    if os.path.exists(f"/tmp/{id}"):
        shutil.rmtree(f"/tmp/{id}")
    runCode(submission)
    return submission.result

register.post("/submit", "loggedin", submit)
register.post("/changeResult", "admin", changeResult)
register.post("/rejudge", "admin", rejudge)
Exemple #5
0
        div(cls="modal",
            tabindex="-1",
            role="dialog",
            contents=[
                div(cls="modal-dialog",
                    role="document",
                    contents=[div(id="modal-content")])
            ]))


def judge_submission(params, user):
    submission = Submission.get(params[0])
    force = params[1] == "force"
    if submission.checkout is not None and not force:
        return f"CONFLICT:{User.get(submission.checkout).username}"
    return SubmissionCard(submission, user, force)


def judge_submission_close(params, setHeader, user):
    submission = Submission.get(params["id"])
    if submission.version == int(params["version"]):
        submission.checkout = None
        submission.save()
    return "ok"


register.web("/judgeSubmission/([a-zA-Z0-9-]*)(?:/(force))?", "admin",
             judge_submission)
register.post("/judgeSubmissionClose", "admin", judge_submission_close)
register.web("/judge", "admin", judge)
Exemple #6
0
from code.util import register
from code.util.db import User
from code.util.auth import generatePassword
import logging

def createUser(params, setHeader, user):
    newPassword = generatePassword()
    user = User(
        params["username"],
        newPassword,
        params["type"]
    )
    user.save()
    return newPassword

def deleteUser(params, setHeader, user):
    username = params["username"]
    user = User.getByName(username)
    user.delete()
    return "ok"

register.post("/createUser", "admin", createUser)
register.post("/deleteUser", "admin", deleteUser)
from code.util.db.problem import Problem, Datum
import json

def deleteProblem(params, setHeader, user):
    id = params["id"]
    Problem.get(id).delete()
    return "ok"

def editProblem(params, setHeader, user):
    id = params.get("id")
    problem = Problem.get(id) or Problem()

    problem.title       = params["title"]
    problem.description = params["description"]
    problem.statement   = params["statement"]
    problem.input       = params["input"]
    problem.output      = params["output"]
    problem.constraints = params["constraints"]
    problem.samples     = int(params["samples"])

    testData            = json.loads(params["testData"])
    problem.testData    = [Datum(d["input"], d["output"]) for d in testData]
    problem.tests       = len(testData)

    problem.save()

    return problem.id

register.post("/deleteProblem", "admin", deleteProblem)
register.post("/editProblem", "admin", editProblem)
    submission = Submission.get(id)
    files = [(f"submission.{exts[submission.language]}", submission.code)]
    for i in range(len(submission.inputs)):
        files.append((f"test{i}.txt", submission.inputs[i]))
    # send the json data
    return json.dumps(files)


def rejudgeAll(params, setHeader, user):
    probId = params["probId"]
    # curTime = params["curTime"]
    curTime = time.time() * 1000
    count = 0
    for contestant in filter(lambda c: not c.isAdmin(), User.all()):
        for sub in filter(
                lambda s: s.user.id == contestant.id and s.problem.id == probId
                and s.timestamp < curTime and s.result != "reject" and s.type
                != "test", Submission.all()):
            if os.path.exists(f"/tmp/{id}"):
                shutil.rmtree(f"/tmp/{id}")
            runCode(sub)
            count += 1
    return {"name": Problem.get(probId).title, "count": count}


register.post("/submit", "loggedin", submit)
register.post("/changeResult", "admin", changeResult)
register.post("/rejudge", "admin", rejudge)
register.post("/download", "admin", download)
register.post("/rejudgeAll", "admin", rejudgeAll)
Exemple #9
0
def root(params, setHeader, user):
    setHeader("Location", "/problems")
    return 302


def login(params, setHeader, user):
    username = params["username"]
    password = params["password"]
    user = auth.checkPassword(username, password)
    if user:
        setHeader("Set-Cookie", f"user={user.id}")
        setHeader("Set-Cookie", f"userType={user.type}")
        return "ok"
    else:
        return "Incorrect username / password"


def logout(params, setHeader, user):
    setHeader("Location", "/login")
    setHeader("Set-Cookie",
              "user=deleted; expires=Thu, 01 Jan 1970 00:00:00 GMT;")
    setHeader("Set-Cookie",
              "userType=deleted; expires=Thu, 01 Jan 1970 00:00:00 GMT")
    return 302


register.get("/", "loggedin", root)
register.post("/login", "any", login)
register.get("/logout", "any", logout)
Exemple #10
0
def deleteProblem(params, setHeader, user):
    id = params["id"]
    Problem.get(id).delete()
    return "ok"


def editProblem(params, setHeader, user):
    id = params.get("id")
    problem = Problem.get(id) or Problem()

    problem.title = params["title"]
    problem.description = params["description"]
    problem.statement = params["statement"]
    problem.input = params["input"]
    problem.output = params["output"]
    problem.constraints = params["constraints"]
    problem.samples = int(params["samples"])

    testData = json.loads(params["testData"])
    problem.testData = [Datum(d["input"], d["output"]) for d in testData]
    problem.tests = len(testData)

    problem.save()

    return problem.id


register.post("/deleteProblem", "admin", deleteProblem)
register.post("/editProblem", "admin", editProblem)
from code.util import register
from code.util.db import Contest, Problem
import json

def deleteContest(params, setHeader, user):
    id = params["id"]
    Contest.get(id).delete()
    return "ok"

def editContest(params, setHeader, user):
    id = params.get("id")
    contest = Contest.get(id) or Contest()

    contest.name     = params["name"]
    contest.start    = int(params["start"])
    contest.end      = int(params["end"])
    contest.scoreboardOff = int(params["scoreboardOff"])
    contest.problems = [Problem.get(id) for id in json.loads(params["problems"])]

    contest.save()

    return contest.id

register.post("/deleteContest", "admin", deleteContest)
register.post("/editContest", "admin", editContest)