コード例 #1
0
ファイル: api_utils.py プロジェクト: Officeyutong/HelloJudge2
def export_problem(id):
    """
    SYZOJ式的导出题目
    """
    problem: Problem = Problem.by_id(id)
    if not problem or not problem.public:
        return ujson.dumps({
            "success": False, "error": {
                "message": "无此题目", "nextUrls": {}
            }
        })
    result = {
        "success": True,
        "obj": {
            "title": problem.title,
            "description": problem.background+"\n"+problem.content,
            "input_format": problem.input_format,
            "output_format": problem.output_format,
            "example": "\n\n".join((f"#### 样例{index} 输入\n{item['input']}\n\n#### 样例{index} 输出\n{item['output']}" for item, index in enumerate(problem.example))),
            "limit_and_hint": problem.hint,
            "time_limit": max((item["time_limit"] for item in problem.subtasks)),
            "memory_limit": max((item["memory_limit"] for item in problem.subtasks)),
            "file_io": problem.using_file_io, "file_io_input_name": problem.input_file_name, "file_io_output_name": problem.output_file_name,
            "type": problem.problem_type, "tags": []
        }
    }
    return ujson.dumps(result)
コード例 #2
0
ファイル: api_utils.py プロジェクト: Officeyutong/HelloJudge2
def testdata_download(id):
    problem: Problem = Problem.by_id(id)
    if not problem or not problem.public:
        return 404
    import pathlib
    import zipfile
    import tempfile
    import flask
    problem_path = pathlib.Path(config.UPLOAD_DIR)/str(problem.id)
    zip_file = tempfile.mktemp("zip")
    zipf = zipfile.ZipFile(zip_file, "w")
    for publicized_file in problem.downloads:
        zipf.write(problem_path/publicized_file, arcname=publicized_file)
    zipf.close()
    return flask.send_file(zip_file, as_attachment=True, attachment_filename="testdata.zip")
コード例 #3
0
ファイル: api_team.py プロジェクト: Officeyutong/HelloJudge2
def show_team():
    """
    参数
    {
        "team_id":"团队ID"
    }
    返回:
    {
        "code":0,
        "data":{
            "canManage":"是否可以管理",
            "id":"团队ID",
            "name":"团队名",
            "description":"团队描述",
            "owner_id":"所有者ID",
            "owner_username":"******",
            "admins":[],//管理员列表[1,2,3]
            "members":[],//用户列表{"username":"******","uid":xxx}
            "create_time":"创建时间",
            "hasPermission":"是否有权限查看详情",
            "problems":[ // 团队题目列表
                {"id":"题目ID","title":"题目名"}
            ],
            "contests":[ // 团队题目列表
                {"id":"比赛ID","name":"比赛名"}
            ],
            "problemsets":[ // 团队题目列表
                {"id":"习题集ID","name":"习题集名"}
            ],
            "tasks":[
                {
                    "name":"任务名",
                    "problems":[
                        {
                            "name":"题目名",
                            "id":"题目ID",
                            scores:[
                                {
                                    "uid":"用户ID",
                                    "username":"******"
                                    "score":"题目得分",
                                    "status":"题目状态",
                                    "submit_id":"提交ID"
                                },
                            ]
                        }
                    ]
                }
            ]
        }
    }
    """
    if not session.get("uid"):
        return make_response(-1, message="请先登录")
    user: User = User.by_id(session.get("uid"))
    team: Team = Team.by_id(request.form["team_id"])
    user_relationship: TeamMember = db.session.query(TeamMember).filter_by(
        uid=user.id, team_id=team.id).one_or_none()
    has_permission = permission_manager.has_permission(
        user.id, f"team.use.{team.id}"
    ) or (not team.private) or user.id == team.owner_id or (user_relationship)
    # result = team.as_dict()
    members = db.session.query(TeamMember.uid, TeamMember.is_admin,
                               User.username).join(User).filter(
                                   TeamMember.team_id == team.id).order_by(
                                       TeamMember.is_admin.desc()).order_by(
                                           User.username.asc()).all()
    result = {
        "id":
        team.id,
        "name":
        team.name,
        "description":
        team.description,
        "owner_id":
        team.owner_id,
        "tasks":
        team.tasks,
        "create_time":
        str(team.create_time),
        "private":
        bool(team.private),
        "team_problems":
        team.team_problems,
        "team_contests":
        team.team_contests,
        "team_problemsets":
        team.team_problemsets,
        "canManage":
        permission_manager.has_permission(session.get("uid", -1),
                                          "team.manage")
    }
    result["owner_username"] = User.by_id(result["owner_id"]).username
    result["hasPermission"] = has_permission
    result["team_problems"] = [{
        "id":
        item,
        "title":
        db.session.query(Problem.title).filter_by(id=item).one().title
    } for item in result["team_problems"]]
    result["team_contests"] = [{
        "id":
        item,
        "name":
        db.session.query(Contest.name).filter_by(id=item).one().name
    } for item in result["team_contests"]]
    result["team_problemsets"] = [{
        "id":
        item,
        "name":
        db.session.query(ProblemSet.name).filter_by(id=item).one().name
    } for item in result["team_problemsets"]]

    if has_permission:
        for item in result["tasks"]:
            problems = []
            for problem in item["problems"]:
                current: Problem = Problem.by_id(problem)
                last = {"id": current.id, "name": current.title, "scores": []}
                for user_ in members:
                    user = user_.uid
                    submit: Submission = db.session.query(Submission).filter(
                        Submission.uid == user).filter(
                            Submission.problem_id == current.id).order_by(
                                Submission.status.asc()).order_by(
                                    Submission.submit_time.desc())
                    if submit.count() == 0:
                        submit = None
                    else:
                        submit = submit.first()
                    if submit:
                        last["scores"].append({
                            "uid": user,
                            "username": User.by_id(user).username,
                            "score": submit.get_total_score(),
                            "status": submit.status,
                            "submit_id": submit.id
                        })
                    else:
                        last["scores"].append({
                            "uid": user,
                            "username": User.by_id(user).username,
                            "score": 0,
                            "status": "unsubmitted"
                        })

                problems.append(last)
            item["problems"] = problems

        result["create_time"] = str(result["create_time"])

        result["admins"] = []
        result["members"] = []
        for x in members:
            result["members"].append({"username": x.username, "uid": x.uid})
            if x.is_admin:
                result["admins"].append(x.uid)
        # result["members"] = list(map(lambda x: {"username": User.by_id(
        #     x).username, "uid": x}, result["members"]))
    else:
        result["description"] = ""
        result["admins"] = []
        result["members"] = []
        result["create_time"] = ""
        result["tasks"] = []

    return make_response(0, message="操作完成", data=result)