mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 23:55:46 +02:00
Creating bookmarks + message command
This commit is contained in:
parent
8308b4ad9a
commit
12d2017cbe
8 changed files with 234 additions and 11 deletions
46
database/crud/bookmarks.py
Normal file
46
database/crud/bookmarks.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
from typing import Optional
|
||||
|
||||
import sqlalchemy.exc
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud.users import get_or_add_user
|
||||
from database.exceptions import DuplicateInsertException, ForbiddenNameException
|
||||
from database.schemas import Bookmark
|
||||
|
||||
__all__ = ["create_bookmark", "get_bookmarks", "get_bookmark_by_name"]
|
||||
|
||||
|
||||
async def create_bookmark(session: AsyncSession, user_id: int, label: str, jump_url: str) -> Bookmark:
|
||||
"""Create a new bookmark to a message"""
|
||||
# Don't allow bookmarks with names of subcommands
|
||||
if label.lower() in ["create", "ls", "list", "search"]:
|
||||
raise ForbiddenNameException
|
||||
|
||||
await get_or_add_user(session, user_id)
|
||||
|
||||
try:
|
||||
bookmark = Bookmark(label=label, jump_url=jump_url, user_id=user_id)
|
||||
session.add(bookmark)
|
||||
await session.commit()
|
||||
await session.refresh(bookmark)
|
||||
except sqlalchemy.exc.IntegrityError as e:
|
||||
raise DuplicateInsertException from e
|
||||
|
||||
return bookmark
|
||||
|
||||
|
||||
async def get_bookmarks(session: AsyncSession, user_id: int, *, query: Optional[str] = None) -> list[Bookmark]:
|
||||
"""Get all a user's bookmarks"""
|
||||
statement = select(Bookmark).where(Bookmark.user_id == user_id)
|
||||
|
||||
if query is not None:
|
||||
statement = statement.where(Bookmark.label.ilike(f"%{query.lower()}%"))
|
||||
|
||||
return (await session.execute(statement)).scalars().all()
|
||||
|
||||
|
||||
async def get_bookmark_by_name(session: AsyncSession, user_id: int, query: str) -> Optional[Bookmark]:
|
||||
"""Try to find a bookmark by its name"""
|
||||
statement = select(Bookmark).where(Bookmark.user_id == user_id).where(func.lower(Bookmark.label) == query.lower())
|
||||
return (await session.execute(statement)).scalar_one_or_none()
|
||||
|
|
@ -1,5 +1,11 @@
|
|||
from .constraints import DuplicateInsertException
|
||||
from .constraints import DuplicateInsertException, ForbiddenNameException
|
||||
from .currency import DoubleNightly, NotEnoughDinks
|
||||
from .not_found import NoResultFoundException
|
||||
|
||||
__all__ = ["DuplicateInsertException", "DoubleNightly", "NotEnoughDinks", "NoResultFoundException"]
|
||||
__all__ = [
|
||||
"DuplicateInsertException",
|
||||
"ForbiddenNameException",
|
||||
"DoubleNightly",
|
||||
"NotEnoughDinks",
|
||||
"NoResultFoundException",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
__all__ = ["DuplicateInsertException"]
|
||||
__all__ = ["DuplicateInsertException", "ForbiddenNameException"]
|
||||
|
||||
|
||||
class DuplicateInsertException(Exception):
|
||||
"""Exception raised when a value already exists"""
|
||||
|
||||
|
||||
class ForbiddenNameException(Exception):
|
||||
"""Exception raised when trying to insert something with a name that isn't allowed"""
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ from sqlalchemy import (
|
|||
ForeignKey,
|
||||
Integer,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.orm import declarative_base, relationship
|
||||
|
||||
|
|
@ -25,6 +26,7 @@ __all__ = [
|
|||
"Base",
|
||||
"Bank",
|
||||
"Birthday",
|
||||
"Bookmark",
|
||||
"CustomCommand",
|
||||
"CustomCommandAlias",
|
||||
"DadJoke",
|
||||
|
|
@ -78,6 +80,20 @@ class Birthday(Base):
|
|||
user: User = relationship("User", uselist=False, back_populates="birthday", lazy="selectin")
|
||||
|
||||
|
||||
class Bookmark(Base):
|
||||
"""A bookmark to a given message"""
|
||||
|
||||
__tablename__ = "bookmarks"
|
||||
__table_args__ = (UniqueConstraint("user_id", "label"),)
|
||||
|
||||
bookmark_id: int = Column(Integer, primary_key=True)
|
||||
label: str = Column(Text, nullable=False)
|
||||
jump_url: str = Column(Text, nullable=False)
|
||||
user_id: int = Column(BigInteger, ForeignKey("users.user_id"))
|
||||
|
||||
user: User = relationship("User", back_populates="bookmarks", uselist=False, lazy="selectin")
|
||||
|
||||
|
||||
class CustomCommand(Base):
|
||||
"""Custom commands to fill the hole Dyno couldn't"""
|
||||
|
||||
|
|
@ -231,6 +247,9 @@ class User(Base):
|
|||
birthday: Optional[Birthday] = relationship(
|
||||
"Birthday", back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
bookmarks: list[Bookmark] = relationship(
|
||||
"Bookmark", back_populates="user", uselist=True, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
nightly_data: NightlyData = relationship(
|
||||
"NightlyData", back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue