示例#1
0
async def register(user_to_create: UserCreate, session: AsyncSession = Depends(Session)):
    for i in count():
        try:
            user = models.User(**user_to_create.dict())
            async with session.begin():
                session.add(user)
                break
        except IntegrityError as e:
            # try 10 times
            if i > 9:
                raise HTTPException(status.HTTP_409_CONFLICT, e)
    async with session.begin():
        session.add(models.Channel(owner_id=user.id, type=models.ChannelType.USER))
    return schemas.UserExternal(**user.__dict__)
示例#2
0
async def delete_all_rubrics(session: AsyncSession,
                             user_id: int,
                             *,
                             delete_links: bool = False) -> None:
    """
    Delete all rubrics.
    Optionally, it is possible to delete all links that related with rubrics [with `delete_links` flag].

    :param session: db connection
    :type session: AsyncSession
    :param user_id: user id
    :type user_id: int
    :param delete_links: to delete links that related with rubrics
    :type delete_links: bool

    :return: None
    :rtype: None
    """

    if delete_links:
        await delete_all_rubric_links_by_user(session, user_id)

    async with session.begin():
        stmt = sa.delete(Rubric).where(Rubric.user_id == user_id)
        await session.execute(stmt)
示例#3
0
async def create_script(
    script_in: ScriptIn, session: AsyncSession = Depends(get_db_session)  # noqa: B008
):
    """Add a new script"""
    async with session.begin():
        try:
            dbobj = await modeldb.create_script(session, script_in)
        except modeldb.ConstraintFailureError as exc:
            raise HTTPException(status_code=409, detail=str(exc)) from None
        session.add(dbobj)

    return dbobj
示例#4
0
async def create(response: CannedResponseIn, db: AsyncSession = Depends(get_db)):
    try:
        # Extract the fields
        r = CannedResponse(**response.dict())

        # Add it to the database
        async with db.begin():
            db.add(r)

        return r
    except IntegrityError:
        raise HTTPException(status_code=409, detail="field 'key' must be unique")
示例#5
0
async def mark_all_bugs_as_watched(session: AsyncSession) -> None:
    """
    Mark all bugs as watched.

    :param session: db connection
    :type session: AsyncSession

    :return: None
    :rtype: None
    """

    async with session.begin():
        stmt = sa.update(Bug).values(is_shown=True)
        await session.execute(stmt)
示例#6
0
async def create_scaling_model(
    scaling_model_in: ApplicationScalingModelIn,
    session: AsyncSession = Depends(get_db_session),  # noqa: B008
):
    """Add a new scaling model"""

    dbobj = db.ScalingModel(**scaling_model_in.dict())

    try:
        async with session.begin():
            session.add(dbobj)
    except IntegrityError:
        raise HTTPException(409) from None

    return dbobj
示例#7
0
async def create_infrastructure(
    infrastructure_in: InfrastructureIn,
    session: AsyncSession = Depends(get_db_session),  # noqa: B008
):
    """Add a new infrastructure"""

    dbobj = db.Infrastructure(**infrastructure_in.dict())

    try:
        async with session.begin():
            session.add(dbobj)
    except IntegrityError:
        raise HTTPException(409) from None

    return dbobj
示例#8
0
async def add_entity(session: AsyncSession, entity: Base) -> None:
    """
    Execute statement [add instance to session] and commit transaction.

    :param session: db connection
    :type session: AsyncSession
    :param entity: one of Base models instance
    :type entity: Base

    :return: None
    :rtype: None
    """

    async with session.begin():
        session.add(entity)
示例#9
0
async def delete_one_link(session: AsyncSession, link_id: int) -> None:
    """
    Delete link.

    :param session: db connection
    :type session: AsyncSession
    :param link_id: link id
    :type link_id: int

    :return: None
    :rtype: None
    """

    async with session.begin():
        stmt = sa.delete(Link).where(Link.id == link_id)
        await session.execute(stmt)
示例#10
0
async def delete_user(session: AsyncSession, user_id: int) -> None:
    """
    Delete user.

    :param session: db connection
    :type session: AsyncSession
    :param user_id: user id [as `User` keeps `id` column - means user tg id]
    :type user_id: int

    :return: None
    :rtype: None
    """

    async with session.begin():
        stmt = sa.delete(User).where(User.id == user_id)
        await session.execute(stmt)
示例#11
0
async def delete_entity_by_instance(session: AsyncSession,
                                    entity: Base) -> None:
    """
    Delete entity by model instance.

    :param session: db connection
    :type session: AsyncSession
    :param entity: model instance
    :type entity: Base

    :return: None
    :rtype: None
    """

    async with session.begin():
        await session.delete(entity)
示例#12
0
async def delete_all_links_by_rubric(session: AsyncSession,
                                     rubric_id: int) -> None:
    """
    Delete all links that related with rubric.

    :param session: db connection
    :type session: AsyncSession
    :param rubric_id: rubric id

    :type rubric_id: int
    :return: None
    :rtype: None
    """

    async with session.begin():
        stmt = sa.delete(Link).where(Link.rubric_id == rubric_id)
        await session.execute(stmt)
示例#13
0
async def delete_all_links_by_user(session: AsyncSession,
                                   user_id: int) -> None:
    """
    Delete all links that related with user.

    :param session: db connection
    :type session: AsyncSession
    :param user_id: user id
    :type user_id: int

    :return: None
    :rtype: None
    """

    async with session.begin():
        stmt = sa.delete(Link).where(Link.user_id == user_id)
        await session.execute(stmt)
示例#14
0
async def delete_one_rubric(
        session: AsyncSession,
        rubric_id: int,
        *,
        delete_links: bool = False,
        migrate_links_in_rubric_with_id: int = False) -> None:
    """
    Delete rubric. Optionally, it is possible to:
        * migrate related links in non-rubric category [by default]
        * delete related links [with `delete_links` flag]
        * migrate related links in another rubric [with `migrate_links_in_rubric_with_id` int id value]

    Note:
        It is impossible to pass few arguments that make different operating with related links.

    :param session: db connection
    :type session: AsyncSession
    :param rubric_id: rubric id
    :type rubric_id: int
    :keyword delete_links: to delete rubric and links that related with this rubric
    :type delete_links: bool
    :keyword migrate_links_in_rubric_with_id: to delete rubric and migrate related links in another rubric
    :type migrate_links_in_rubric_with_id: int

    :return: None
    :rtype: None

    :raises TypeError: raised if few arguments that make different operating with related links have passed
    """

    if delete_links and migrate_links_in_rubric_with_id:
        msg = (
            'It is impossible to pass few arguments that make different operating with related links! '
            'Instead got <delete_links> and <migrate_links_in_rubric_with_id> arguments together.'
        )
        raise TypeError(msg)
    elif delete_links:
        await delete_all_links_by_rubric(session, rubric_id=rubric_id)
    elif migrate_links_in_rubric_with_id:
        await migrate_links_in_another_rubric(session, rubric_id,
                                              migrate_links_in_rubric_with_id)

    async with session.begin():
        stmt = sa.delete(Rubric).where(Rubric.id == rubric_id)
        await session.execute(stmt)
示例#15
0
async def create(
    fields: PanelIn,
    db: AsyncSession = Depends(get_db),
):
    # Construct the model
    panel = Panel(
        title=fields.title, content=fields.content, channel_id=fields.channel_id
    )

    # Get the specified channel
    channel = await get_channel(panel.channel_id)
    if channel is None:
        raise HTTPException(
            status_code=400, detail="specified text channel does not exist"
        )

    # Check that the emojis exist
    for reaction in fields.reactions:
        # Ensure the emoji is valid
        emoji = await validate_emoji(channel, reaction.emoji)

        # Add the reaction to the panel
        panel.reactions.append(
            Reaction(category_id=reaction.category_id, emoji=str(emoji))
        )

    # Build and send the message
    message = await channel.send(
        embed=Embed(
            title=panel.title, description=panel.content, type="rich", color=EMBED_COLOR
        )
    )
    panel.message_id = message.id

    # Add the reactions
    for reaction in panel.reactions:
        await message.add_reaction(reaction.emoji)

    # Add it to the database
    async with db.begin():
        db.add(panel)

    return panel
示例#16
0
async def migrate_links_in_another_rubric(session: AsyncSession,
                                          old_rubric_id: int,
                                          new_rubric_id: int) -> None:
    """
    Move all links from ine rubric in another rubric.

    :param session: db connection
    :type session: AsyncSession
    :param old_rubric_id: move links !from! this rubric
    :type old_rubric_id: int
    :param new_rubric_id: move links !in! this rubric
    :type new_rubric_id: int

    :return: None
    :rtype: None
    """

    async with session.begin():
        stmt = (sa.update(Link).where(Link.rubric_id == old_rubric_id).values(
            rubric_id=new_rubric_id))
        await session.execute(stmt)