コード例 #1
0
ファイル: contributor_view.py プロジェクト: nnonchev/CTFe
def create_challenge(
    *,
    challenge_create: challenge_schemas.Create,
    db_contributor: User = Depends(auth_ops.get_current_user),
    session: Session = Depends(dal.get_session),
) -> contributor_schemas.Details:
    """ Create challenge and assign this contributor to it """

    from CTFe.operations import challenge_ops

    # Challenge name is already taken
    conditions = and_(
        Challenge.name == challenge_create.name,
    )

    db_challenge = challenge_ops.query_challenges_by_(
        session, conditions).first()

    if db_challenge is not None:
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail=f"The name: { challenge_create.name } is already taken",
        )

    contributor_ops.create_challenge(
        session, challenge_create, db_contributor)

    return db_contributor
コード例 #2
0
async def create_attempt(
        *,
        attempt_create: attempt_schemas.Create,
        session: Session = Depends(dal.get_session),
) -> attempt_schemas.Details:
    """ Create new attempt DB record """

    conditions = and_(Team.id == attempt_create.team_id, )

    db_team = team_ops.query_teams_by_(session, conditions).first()
    if not db_team:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Team not found",
        )

    conditions = and_(Challenge.id == attempt_create.challenge_id, )

    db_challenge = challenge_ops.query_challenges_by_(session,
                                                      conditions).first()

    if not db_challenge:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Challenge not found",
        )

    db_attempt = attempt_ops.create_attempt(session, attempt_create)

    return db_attempt
コード例 #3
0
ファイル: contributor_view.py プロジェクト: nnonchev/CTFe
def remove_challenge(
    *,
    challenge_id: int,
    db_contributor: User = Depends(auth_ops.get_current_user),
    session: Session = Depends(dal.get_session),
) -> contributor_schemas.Details:
    """ Delete a challenge related to this contributor """

    conditions = and_(
        Challenge.id == challenge_id,
        Challenge.owner_id == db_contributor.id,
    )

    db_challenge = challenge_ops.query_challenges_by_(
        session, conditions).first()

    if db_challenge is None:
        raise HTTPException(
            status.HTTP_404_NOT_FOUND,
            detail="Challenge not found",
        )

    challenge_ops.delete_challenge(session, db_challenge)

    return db_contributor
コード例 #4
0
ファイル: challenge_view.py プロジェクト: nnonchev/CTFe
def remove_file(
    *,
    id: int,
    background_task: BackgroundTasks,
    session: Session = Depends(dal.get_session),
) -> challenge_schemas.Details:
    """ Remove uploaded file associated with a challenge record """

    conditions = and_(
        Challenge.id == id,
    )

    db_challenge = challenge_ops.query_challenges_by_(
        session, conditions).first()

    if db_challenge is None:
        raise HTTPException(
            status.HTTP_404_NOT_FOUND,
            detail="Challenge not found"
        )

    if db_challenge.file_name is None:
        raise HTTPException(
            status.HTTP_401_UNAUTHORIZED,
            detail="There is no file associated with this challenge"
        )

    filename = db_challenge.file_name
    background_task.add_task(challenge_ops.remove_file, filename)

    challenge_update = challenge_schemas.Update(file_name=None)
    db_challenge = challenge_ops.update_challenge(
        session, db_challenge, challenge_update)

    return db_challenge
コード例 #5
0
ファイル: challenge_view.py プロジェクト: nnonchev/CTFe
async def update_challenge(
    *,
    id: int,
    challenge_update: challenge_schemas.Update,
    session: Session = Depends(dal.get_session)
) -> challenge_schemas.Details:
    """ Update challenge record from DB """

    conditions = and_(
        Challenge.id == id,
    )

    db_challenge = challenge_ops.query_challenges_by_(
        session, conditions).first()

    if db_challenge is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Challenge not found",
        )

    db_challenge = challenge_ops.update_challenge(
        session, db_challenge, challenge_update)

    return db_challenge
コード例 #6
0
ファイル: challenge_view.py プロジェクト: nnonchev/CTFe
async def get_all_challenges(
    *,
    session: Session = Depends(dal.get_session)
) -> List[challenge_schemas.Details]:
    """ Get all challenge records from DB """

    db_challenges = challenge_ops.query_challenges_by_(session).all()

    return db_challenges
コード例 #7
0
ファイル: contributor_view.py プロジェクト: nnonchev/CTFe
def list_challenges(
    *,
    db_contributor: User = Depends(auth_ops.get_current_user),
    session: Session = Depends(dal.get_session),
) -> challenge_schemas.Details:
    """ List all challenges created by the current contributor """

    conditions = and_(
        Challenge.owner_id == db_contributor.id,
    )

    db_challenges = challenge_ops.query_challenges_by_(
        session, conditions).all()

    return db_challenges
コード例 #8
0
ファイル: challenge_view.py プロジェクト: nnonchev/CTFe
async def get_challenge_by_name(
    *,
    name: str,
    session: Session = Depends(dal.get_session)
) -> challenge_schemas.Details:
    """ Get challenge record from DB """

    conditions = and_(
        Challenge.name == name,
    )

    db_challenge = challenge_ops.query_challenges_by_(
        session, conditions).first()

    if db_challenge is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Challenge not found",
        )

    return db_challenge
コード例 #9
0
ファイル: challenge_view.py プロジェクト: nnonchev/CTFe
async def delete_challenge(
    *,
    id: int,
    session: Session = Depends(dal.get_session)
):
    """ Delete challenge record from DB """

    conditions = and_(
        Challenge.id == id,
    )

    db_challenge = challenge_ops.query_challenges_by_(
        session, conditions).first()

    if db_challenge is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Challenge not found",
        )

    challenge_ops.delete_challenge(session, db_challenge)
コード例 #10
0
ファイル: challenge_view.py プロジェクト: nnonchev/CTFe
def upload_file(
    *,
    id: int,
    challenge_file: UploadFile = File(...),
    background_task: BackgroundTasks,
    session: Session = Depends(dal.get_session),
) -> challenge_schemas.Details:
    """ Upload file associated with a challenge record """

    conditions = and_(
        Challenge.id == id,
    )

    db_challenge = challenge_ops.query_challenges_by_(
        session, conditions).first()

    if db_challenge is None:
        raise HTTPException(
            status.HTTP_404_NOT_FOUND,
            detail="Challenge not found"
        )

    if db_challenge.file_name is not None:
        raise HTTPException(
            status.HTTP_401_UNAUTHORIZED,
            detail="There is already a file associated with this challenge"
        )

    filename = uuid4().hex + f"_{ challenge_file.filename }"
    background_task.add_task(challenge_ops.store_file,
                             filename, challenge_file)

    challenge_update = challenge_schemas.Update(file_name=filename)
    db_challenge = challenge_ops.update_challenge(
        session, db_challenge, challenge_update)

    return db_challenge
コード例 #11
0
ファイル: challenge_view.py プロジェクト: nnonchev/CTFe
async def create_challenge(
    *,
    challenge_create: challenge_schemas.Create,
    session: Session = Depends(dal.get_session),
) -> challenge_schemas.Details:
    """ Create new challenge DB record """

    # Make sure unique fields are not already used
    conditions = and_(
        Challenge.name == challenge_create.name,
    )

    db_challenge = challenge_ops.query_challenges_by_(
        session, conditions).first()

    if db_challenge is not None:
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail=f"The name: { challenge_create.name } is already taken",
        )

    db_challenge = challenge_ops.create_challenge(session, challenge_create)

    return db_challenge