예제 #1
0
async def list_problems_in_problem_set(
    problem_set: models.ProblemSet = Depends(
        parse_problem_set_factory(load_problems=True)),
    user: models.User = Depends(parse_user_from_auth),
) -> StandardListResponse[schemas.ProblemWithLatestRecord]:
    problems = await problem_set.get_problems_with_record_states(
        cls=schemas.ProblemWithLatestRecord, user_id=user.id)
    return StandardListResponse(problems)
예제 #2
0
async def get_user_problems(
    user: models.User = Depends(parse_uid),
    query: schemas.PaginationQuery = Depends(parse_pagination_query),
) -> StandardListResponse[schemas.Problem]:
    condition = {"owner": user.id}
    cursor = models.Problem.cursor_find(condition, query)
    res = await models.Problem.to_list(cursor)
    return StandardListResponse(res)
예제 #3
0
async def list_users(
        ordering: schemas.OrderingQuery = Depends(
            parse_ordering_query(["username"])),
        pagination: schemas.PaginationQuery = Depends(parse_pagination_query),
        query: str = Query(""),
) -> StandardListResponse[schemas.User]:
    statement = models.User.find_users_statement(query)
    problem_sets, count = await models.User.execute_list_statement(
        statement, ordering, pagination)
    return StandardListResponse(problem_sets, count)
예제 #4
0
async def list_problem_sets(
    domain: models.Domain = Depends(parse_domain_from_auth),
    ordering: schemas.OrderingQuery = Depends(parse_ordering_query()),
    pagination: schemas.PaginationQuery = Depends(parse_pagination_query),
    include_hidden: bool = Depends(parse_view_hidden_problem_set),
) -> StandardListResponse[schemas.ProblemSet]:
    statement = domain.find_problem_sets_statement(include_hidden)
    problem_sets, count = await models.ProblemSet.execute_list_statement(
        statement, ordering, pagination)
    return StandardListResponse(problem_sets, count)
예제 #5
0
async def list_user_domains(
    role: Optional[List[str]] = Query(None),
    groups: Optional[List[str]] = Query(None),
    ordering: schemas.OrderingQuery = Depends(parse_ordering_query()),
    pagination: schemas.PaginationQuery = Depends(parse_pagination_query),
    user: models.User = Depends(parse_uid),
) -> StandardListResponse[schemas.Domain]:
    statement = user.find_domains_statement(role, groups)
    domains, count = await models.Domain.execute_list_statement(
        statement, ordering, pagination)
    return StandardListResponse(domains, count)
예제 #6
0
async def list_problems(
    domain: models.Domain = Depends(parse_domain_from_auth),
    ordering: schemas.OrderingQuery = Depends(parse_ordering_query()),
    pagination: schemas.PaginationQuery = Depends(parse_pagination_query),
    include_hidden: bool = Depends(parse_view_hidden_problem),
    user: models.User = Depends(parse_user_from_auth),
) -> StandardListResponse[schemas.ProblemWithLatestRecord]:
    statement = domain.find_problems_statement(include_hidden)
    problems, count = await models.Problem.execute_list_statement(
        statement, ordering, pagination)
    result = await models.Problem.get_problems_with_record_states(
        result_cls=schemas.ProblemWithLatestRecord,
        problem_set_id=None,
        problems=problems,
        user_id=user.id,
    )
    return StandardListResponse(result, count)
예제 #7
0
async def clone_problem(
    problem_clone: schemas.ProblemClone,
    domain: models.Domain = Depends(parse_domain_from_auth),
    user: models.User = Depends(parse_user_from_auth),
    auth: Authentication = Depends(),
    session: AsyncSession = Depends(db_session_dependency),
) -> StandardListResponse[schemas.Problem]:
    from_domain = await parse_relevant_domain_with_tag(
        problem_clone.from_domain)
    problems: List[models.Problem] = [
        parse_problem(await parse_problem_without_validation(oid, from_domain),
                      auth) for oid in problem_clone.problems
    ]
    new_group = problem_clone.new_group

    try:
        res = []
        for problem in problems:
            problem_group_id: Optional[UUID]
            if new_group:
                problem_group = models.ProblemGroup()
                # TODO: transaction (since session has already committed here)
                await problem_group.save_model()
                problem_group_id = problem_group.id
                await session.refresh(problem)
                await session.refresh(domain)
            else:
                problem_group_id = problem.problem_group_id
            new_problem = models.Problem(
                domain_id=domain.id,
                owner_id=user.id,
                title=problem.title,
                content=problem.content,
                problem_group_id=problem_group_id,
            )
            await new_problem.save_model()
            res.append(models.Problem.from_orm(new_problem))
            logger.info(f"problem cloned: {new_problem}")
    except Exception as e:
        logger.exception(
            f"problems clone failed: {[problem for problem in problems]}")
        raise e
    return StandardListResponse(res)