mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-11-30 13:20:40 +03:00
code format
This commit is contained in:
@@ -26,7 +26,9 @@ class Base(DeclarativeBase):
|
||||
db_session.add(self)
|
||||
return await db_session.commit()
|
||||
except SQLAlchemyError as ex:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
|
||||
) from ex
|
||||
|
||||
async def delete(self, db_session: AsyncSession):
|
||||
"""
|
||||
@@ -39,7 +41,9 @@ class Base(DeclarativeBase):
|
||||
await db_session.commit()
|
||||
return True
|
||||
except SQLAlchemyError as ex:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
|
||||
) from ex
|
||||
|
||||
async def update(self, db: AsyncSession, **kwargs):
|
||||
"""
|
||||
@@ -53,7 +57,9 @@ class Base(DeclarativeBase):
|
||||
setattr(self, k, v)
|
||||
return await db.commit()
|
||||
except SQLAlchemyError as ex:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
|
||||
) from ex
|
||||
|
||||
async def save_or_update(self, db: AsyncSession):
|
||||
try:
|
||||
|
||||
@@ -12,7 +12,9 @@ from app.models.base import Base
|
||||
class Nonsense(Base):
|
||||
__tablename__ = "nonsense"
|
||||
__table_args__ = ({"schema": "happy_hog"},)
|
||||
id: Mapped[uuid:UUID] = mapped_column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
||||
id: Mapped[uuid:UUID] = mapped_column(
|
||||
UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True
|
||||
)
|
||||
name: Mapped[str] = mapped_column(String, primary_key=True, unique=True)
|
||||
description: Mapped[str | None]
|
||||
# TODO: apply relation to other tables
|
||||
@@ -31,7 +33,9 @@ class Nonsense(Base):
|
||||
if instance is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail={"Record not found": f"There is no record for requested name value : {name}"},
|
||||
detail={
|
||||
"Record not found": f"There is no record for requested name value : {name}"
|
||||
},
|
||||
)
|
||||
else:
|
||||
return instance
|
||||
|
||||
@@ -34,7 +34,10 @@ class Character(Base):
|
||||
"""
|
||||
|
||||
__tablename__ = "character"
|
||||
__table_args__ = (PrimaryKeyConstraint("id", name="character_pkey"), {"schema": "shakespeare"})
|
||||
__table_args__ = (
|
||||
PrimaryKeyConstraint("id", name="character_pkey"),
|
||||
{"schema": "shakespeare"},
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(32), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(64))
|
||||
@@ -45,7 +48,9 @@ class Character(Base):
|
||||
work: Mapped[list["Work"]] = relationship(
|
||||
"Work", secondary="shakespeare.character_work", back_populates="character"
|
||||
)
|
||||
paragraph: Mapped[list["Paragraph"]] = relationship("Paragraph", back_populates="character")
|
||||
paragraph: Mapped[list["Paragraph"]] = relationship(
|
||||
"Paragraph", back_populates="character"
|
||||
)
|
||||
|
||||
|
||||
class Wordform(Base):
|
||||
@@ -65,7 +70,10 @@ class Wordform(Base):
|
||||
"""
|
||||
|
||||
__tablename__ = "wordform"
|
||||
__table_args__ = (PrimaryKeyConstraint("id", name="wordform_pkey"), {"schema": "shakespeare"})
|
||||
__table_args__ = (
|
||||
PrimaryKeyConstraint("id", name="wordform_pkey"),
|
||||
{"schema": "shakespeare"},
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
plain_text: Mapped[str] = mapped_column(String(64))
|
||||
@@ -98,7 +106,10 @@ class Work(Base):
|
||||
"""
|
||||
|
||||
__tablename__ = "work"
|
||||
__table_args__ = (PrimaryKeyConstraint("id", name="work_pkey"), {"schema": "shakespeare"})
|
||||
__table_args__ = (
|
||||
PrimaryKeyConstraint("id", name="work_pkey"),
|
||||
{"schema": "shakespeare"},
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(32), primary_key=True)
|
||||
title: Mapped[str] = mapped_column(String(32))
|
||||
@@ -114,7 +125,9 @@ class Work(Base):
|
||||
"Character", secondary="shakespeare.character_work", back_populates="work"
|
||||
)
|
||||
chapter: Mapped[list["Chapter"]] = relationship("Chapter", back_populates="work")
|
||||
paragraph: Mapped[list["Paragraph"]] = relationship("Paragraph", back_populates="work")
|
||||
paragraph: Mapped[list["Paragraph"]] = relationship(
|
||||
"Paragraph", back_populates="work"
|
||||
)
|
||||
|
||||
|
||||
class Chapter(Base):
|
||||
@@ -137,10 +150,15 @@ class Chapter(Base):
|
||||
|
||||
__tablename__ = "chapter"
|
||||
__table_args__ = (
|
||||
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="chapter_work_id_fkey"),
|
||||
ForeignKeyConstraint(
|
||||
["work_id"], ["shakespeare.work.id"], name="chapter_work_id_fkey"
|
||||
),
|
||||
PrimaryKeyConstraint("id", name="chapter_pkey"),
|
||||
UniqueConstraint(
|
||||
"work_id", "section_number", "chapter_number", name="chapter_work_id_section_number_chapter_number_key"
|
||||
"work_id",
|
||||
"section_number",
|
||||
"chapter_number",
|
||||
name="chapter_work_id_section_number_chapter_number_key",
|
||||
),
|
||||
{"schema": "shakespeare"},
|
||||
)
|
||||
@@ -152,7 +170,9 @@ class Chapter(Base):
|
||||
description: Mapped[str] = mapped_column(String(256))
|
||||
|
||||
work: Mapped["Work"] = relationship("Work", back_populates="chapter")
|
||||
paragraph: Mapped[list["Paragraph"]] = relationship("Paragraph", back_populates="chapter")
|
||||
paragraph: Mapped[list["Paragraph"]] = relationship(
|
||||
"Paragraph", back_populates="chapter"
|
||||
)
|
||||
|
||||
|
||||
t_character_work = Table(
|
||||
@@ -160,8 +180,14 @@ t_character_work = Table(
|
||||
Base.metadata,
|
||||
Column("character_id", String(32), primary_key=True, nullable=False),
|
||||
Column("work_id", String(32), primary_key=True, nullable=False),
|
||||
ForeignKeyConstraint(["character_id"], ["shakespeare.character.id"], name="character_work_character_id_fkey"),
|
||||
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="character_work_work_id_fkey"),
|
||||
ForeignKeyConstraint(
|
||||
["character_id"],
|
||||
["shakespeare.character.id"],
|
||||
name="character_work_character_id_fkey",
|
||||
),
|
||||
ForeignKeyConstraint(
|
||||
["work_id"], ["shakespeare.work.id"], name="character_work_work_id_fkey"
|
||||
),
|
||||
PrimaryKeyConstraint("character_id", "work_id", name="character_work_pkey"),
|
||||
schema="shakespeare",
|
||||
)
|
||||
@@ -198,13 +224,23 @@ class Paragraph(Base):
|
||||
|
||||
__tablename__ = "paragraph"
|
||||
__table_args__ = (
|
||||
ForeignKeyConstraint(["character_id"], ["shakespeare.character.id"], name="paragraph_character_id_fkey"),
|
||||
ForeignKeyConstraint(
|
||||
["character_id"],
|
||||
["shakespeare.character.id"],
|
||||
name="paragraph_character_id_fkey",
|
||||
),
|
||||
ForeignKeyConstraint(
|
||||
["work_id", "section_number", "chapter_number"],
|
||||
["shakespeare.chapter.work_id", "shakespeare.chapter.section_number", "shakespeare.chapter.chapter_number"],
|
||||
[
|
||||
"shakespeare.chapter.work_id",
|
||||
"shakespeare.chapter.section_number",
|
||||
"shakespeare.chapter.chapter_number",
|
||||
],
|
||||
name="paragraph_chapter_fkey",
|
||||
),
|
||||
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="paragraph_work_id_fkey"),
|
||||
ForeignKeyConstraint(
|
||||
["work_id"], ["shakespeare.work.id"], name="paragraph_work_id_fkey"
|
||||
),
|
||||
PrimaryKeyConstraint("id", name="paragraph_pkey"),
|
||||
{"schema": "shakespeare"},
|
||||
)
|
||||
@@ -222,7 +258,9 @@ class Paragraph(Base):
|
||||
char_count: Mapped[int] = mapped_column(Integer)
|
||||
word_count: Mapped[int] = mapped_column(Integer)
|
||||
|
||||
character: Mapped["Character"] = relationship("Character", back_populates="paragraph")
|
||||
character: Mapped["Character"] = relationship(
|
||||
"Character", back_populates="paragraph"
|
||||
)
|
||||
chapter: Mapped["Chapter"] = relationship("Chapter", back_populates="paragraph")
|
||||
work: Mapped["Work"] = relationship("Work", back_populates="paragraph")
|
||||
|
||||
@@ -244,6 +282,12 @@ class Paragraph(Base):
|
||||
- List[Paragraph]: A list of `Paragraph` objects that are associated with the specified character.
|
||||
|
||||
"""
|
||||
stmt = select(cls).join(Character).join(Chapter).join(Work).where(Character.name == character)
|
||||
stmt = (
|
||||
select(cls)
|
||||
.join(Character)
|
||||
.join(Chapter)
|
||||
.join(Work)
|
||||
.where(Character.name == character)
|
||||
)
|
||||
result = await db_session.execute(stmt)
|
||||
return result.scalars().all()
|
||||
|
||||
@@ -13,11 +13,15 @@ from app.models.nonsense import Nonsense
|
||||
class Stuff(Base):
|
||||
__tablename__ = "stuff"
|
||||
__table_args__ = ({"schema": "happy_hog"},)
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True
|
||||
)
|
||||
name: Mapped[str] = mapped_column(String, primary_key=True, unique=True)
|
||||
description: Mapped[str | None]
|
||||
|
||||
nonsense: Mapped["Nonsense"] = relationship("Nonsense", secondary="happy_hog.stuff_full_of_nonsense")
|
||||
nonsense: Mapped["Nonsense"] = relationship(
|
||||
"Nonsense", secondary="happy_hog.stuff_full_of_nonsense"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def find(cls, db_session: AsyncSession, name: str):
|
||||
@@ -42,7 +46,11 @@ class Stuff(Base):
|
||||
class StuffFullOfNonsense(Base):
|
||||
__tablename__ = "stuff_full_of_nonsense"
|
||||
__table_args__ = ({"schema": "happy_hog"},)
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), default=uuid.uuid4, primary_key=True
|
||||
)
|
||||
stuff_id: Mapped[Stuff] = mapped_column(UUID, ForeignKey("happy_hog.stuff.id"))
|
||||
nonsense_id: Mapped["Nonsense"] = mapped_column(UUID, ForeignKey("happy_hog.nonsense.id"))
|
||||
nonsense_id: Mapped["Nonsense"] = mapped_column(
|
||||
UUID, ForeignKey("happy_hog.nonsense.id")
|
||||
)
|
||||
but_why: Mapped[str | None]
|
||||
|
||||
@@ -15,7 +15,9 @@ pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
|
||||
class User(Base):
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), default=uuid.uuid4, primary_key=True
|
||||
)
|
||||
email: Mapped[str] = mapped_column(String, nullable=False, unique=True)
|
||||
first_name: Mapped[str] = mapped_column(String, nullable=False)
|
||||
last_name: Mapped[str] = mapped_column(String, nullable=False)
|
||||
@@ -28,7 +30,9 @@ class User(Base):
|
||||
@password.setter
|
||||
def password(self, password: SecretStr):
|
||||
_password_string = password.get_secret_value()
|
||||
self._password = bcrypt.hashpw(_password_string.encode("utf-8"), bcrypt.gensalt())
|
||||
self._password = bcrypt.hashpw(
|
||||
_password_string.encode("utf-8"), bcrypt.gensalt()
|
||||
)
|
||||
|
||||
def check_password(self, password: SecretStr):
|
||||
return pwd_context.verify(password.get_secret_value(), self.password)
|
||||
|
||||
Reference in New Issue
Block a user