mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
format code
This commit is contained in:
parent
3291fa784e
commit
e75a4cce05
@ -16,7 +16,11 @@ engine = create_async_engine(
|
|||||||
|
|
||||||
# expire_on_commit=False will prevent attributes from being expired
|
# expire_on_commit=False will prevent attributes from being expired
|
||||||
# after commit.
|
# after commit.
|
||||||
AsyncSessionFactory = async_sessionmaker(engine, autoflush=False, expire_on_commit=False,)
|
AsyncSessionFactory = async_sessionmaker(
|
||||||
|
engine,
|
||||||
|
autoflush=False,
|
||||||
|
expire_on_commit=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Dependency
|
# Dependency
|
||||||
|
@ -17,7 +17,6 @@ class Nonsense(Base):
|
|||||||
description: Mapped[str | None]
|
description: Mapped[str | None]
|
||||||
# TODO: apply relation to other tables
|
# TODO: apply relation to other tables
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def find(cls, db_session: AsyncSession, name: str):
|
async def find(cls, db_session: AsyncSession, name: str):
|
||||||
"""
|
"""
|
||||||
@ -36,4 +35,3 @@ class Nonsense(Base):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
@ -1,20 +1,25 @@
|
|||||||
from typing import List, Optional
|
from sqlalchemy import (
|
||||||
|
Column,
|
||||||
from sqlalchemy import Column, ForeignKeyConstraint, Integer, PrimaryKeyConstraint, String, Table, Text, \
|
ForeignKeyConstraint,
|
||||||
UniqueConstraint, select
|
Integer,
|
||||||
|
PrimaryKeyConstraint,
|
||||||
|
String,
|
||||||
|
Table,
|
||||||
|
Text,
|
||||||
|
UniqueConstraint,
|
||||||
|
select,
|
||||||
|
)
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
|
||||||
class Base(DeclarativeBase):
|
class Base(DeclarativeBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Character(Base):
|
class Character(Base):
|
||||||
__tablename__ = 'character'
|
__tablename__ = "character"
|
||||||
__table_args__ = (
|
__table_args__ = (PrimaryKeyConstraint("id", name="character_pkey"), {"schema": "shakespeare"})
|
||||||
PrimaryKeyConstraint('id', name='character_pkey'),
|
|
||||||
{'schema': 'shakespeare'}
|
|
||||||
)
|
|
||||||
|
|
||||||
id: Mapped[str] = mapped_column(String(32), primary_key=True)
|
id: Mapped[str] = mapped_column(String(32), primary_key=True)
|
||||||
name: Mapped[str] = mapped_column(String(64))
|
name: Mapped[str] = mapped_column(String(64))
|
||||||
@ -22,16 +27,15 @@ class Character(Base):
|
|||||||
abbrev: Mapped[str | None] = mapped_column(String(32))
|
abbrev: Mapped[str | None] = mapped_column(String(32))
|
||||||
description: Mapped[str | None] = mapped_column(String(2056))
|
description: Mapped[str | None] = mapped_column(String(2056))
|
||||||
|
|
||||||
work: Mapped[list['Work']] = relationship('Work', secondary='shakespeare.character_work', back_populates='character')
|
work: Mapped[list["Work"]] = relationship(
|
||||||
paragraph: Mapped[list['Paragraph']] = relationship('Paragraph', back_populates='character')
|
"Work", secondary="shakespeare.character_work", back_populates="character"
|
||||||
|
)
|
||||||
|
paragraph: Mapped[list["Paragraph"]] = relationship("Paragraph", back_populates="character")
|
||||||
|
|
||||||
|
|
||||||
class Wordform(Base):
|
class Wordform(Base):
|
||||||
__tablename__ = 'wordform'
|
__tablename__ = "wordform"
|
||||||
__table_args__ = (
|
__table_args__ = (PrimaryKeyConstraint("id", name="wordform_pkey"), {"schema": "shakespeare"})
|
||||||
PrimaryKeyConstraint('id', name='wordform_pkey'),
|
|
||||||
{'schema': 'shakespeare'}
|
|
||||||
)
|
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||||
plain_text: Mapped[str] = mapped_column(String(64))
|
plain_text: Mapped[str] = mapped_column(String(64))
|
||||||
@ -41,11 +45,8 @@ class Wordform(Base):
|
|||||||
|
|
||||||
|
|
||||||
class Work(Base):
|
class Work(Base):
|
||||||
__tablename__ = 'work'
|
__tablename__ = "work"
|
||||||
__table_args__ = (
|
__table_args__ = (PrimaryKeyConstraint("id", name="work_pkey"), {"schema": "shakespeare"})
|
||||||
PrimaryKeyConstraint('id', name='work_pkey'),
|
|
||||||
{'schema': 'shakespeare'}
|
|
||||||
)
|
|
||||||
|
|
||||||
id: Mapped[str] = mapped_column(String(32), primary_key=True)
|
id: Mapped[str] = mapped_column(String(32), primary_key=True)
|
||||||
title: Mapped[str] = mapped_column(String(32))
|
title: Mapped[str] = mapped_column(String(32))
|
||||||
@ -57,18 +58,22 @@ class Work(Base):
|
|||||||
total_paragraphs: Mapped[int] = mapped_column(Integer)
|
total_paragraphs: Mapped[int] = mapped_column(Integer)
|
||||||
notes: Mapped[str | None] = mapped_column(Text)
|
notes: Mapped[str | None] = mapped_column(Text)
|
||||||
|
|
||||||
character: Mapped[list['Character']] = relationship('Character', secondary='shakespeare.character_work', back_populates='work')
|
character: Mapped[list["Character"]] = relationship(
|
||||||
chapter: Mapped[list['Chapter']] = relationship('Chapter', back_populates='work')
|
"Character", secondary="shakespeare.character_work", back_populates="work"
|
||||||
paragraph: Mapped[list['Paragraph']] = relationship('Paragraph', back_populates='work')
|
)
|
||||||
|
chapter: Mapped[list["Chapter"]] = relationship("Chapter", back_populates="work")
|
||||||
|
paragraph: Mapped[list["Paragraph"]] = relationship("Paragraph", back_populates="work")
|
||||||
|
|
||||||
|
|
||||||
class Chapter(Base):
|
class Chapter(Base):
|
||||||
__tablename__ = 'chapter'
|
__tablename__ = "chapter"
|
||||||
__table_args__ = (
|
__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'),
|
PrimaryKeyConstraint("id", name="chapter_pkey"),
|
||||||
UniqueConstraint('work_id', 'section_number', 'chapter_number', name='chapter_work_id_section_number_chapter_number_key'),
|
UniqueConstraint(
|
||||||
{'schema': 'shakespeare'}
|
"work_id", "section_number", "chapter_number", name="chapter_work_id_section_number_chapter_number_key"
|
||||||
|
),
|
||||||
|
{"schema": "shakespeare"},
|
||||||
)
|
)
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||||
@ -77,29 +82,34 @@ class Chapter(Base):
|
|||||||
chapter_number: Mapped[int] = mapped_column(Integer)
|
chapter_number: Mapped[int] = mapped_column(Integer)
|
||||||
description: Mapped[str] = mapped_column(String(256))
|
description: Mapped[str] = mapped_column(String(256))
|
||||||
|
|
||||||
work: Mapped['Work'] = relationship('Work', back_populates='chapter')
|
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(
|
t_character_work = Table(
|
||||||
'character_work', Base.metadata,
|
"character_work",
|
||||||
Column('character_id', String(32), primary_key=True, nullable=False),
|
Base.metadata,
|
||||||
Column('work_id', String(32), primary_key=True, nullable=False),
|
Column("character_id", String(32), primary_key=True, nullable=False),
|
||||||
ForeignKeyConstraint(['character_id'], ['shakespeare.character.id'], name='character_work_character_id_fkey'),
|
Column("work_id", String(32), primary_key=True, nullable=False),
|
||||||
ForeignKeyConstraint(['work_id'], ['shakespeare.work.id'], name='character_work_work_id_fkey'),
|
ForeignKeyConstraint(["character_id"], ["shakespeare.character.id"], name="character_work_character_id_fkey"),
|
||||||
PrimaryKeyConstraint('character_id', 'work_id', name='character_work_pkey'),
|
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="character_work_work_id_fkey"),
|
||||||
schema='shakespeare'
|
PrimaryKeyConstraint("character_id", "work_id", name="character_work_pkey"),
|
||||||
|
schema="shakespeare",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Paragraph(Base):
|
class Paragraph(Base):
|
||||||
__tablename__ = 'paragraph'
|
__tablename__ = "paragraph"
|
||||||
__table_args__ = (
|
__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'], name='paragraph_chapter_fkey'),
|
ForeignKeyConstraint(
|
||||||
ForeignKeyConstraint(['work_id'], ['shakespeare.work.id'], name='paragraph_work_id_fkey'),
|
["work_id", "section_number", "chapter_number"],
|
||||||
PrimaryKeyConstraint('id', name='paragraph_pkey'),
|
["shakespeare.chapter.work_id", "shakespeare.chapter.section_number", "shakespeare.chapter.chapter_number"],
|
||||||
{'schema': 'shakespeare'}
|
name="paragraph_chapter_fkey",
|
||||||
|
),
|
||||||
|
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="paragraph_work_id_fkey"),
|
||||||
|
PrimaryKeyConstraint("id", name="paragraph_pkey"),
|
||||||
|
{"schema": "shakespeare"},
|
||||||
)
|
)
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||||
@ -115,9 +125,9 @@ class Paragraph(Base):
|
|||||||
char_count: Mapped[int] = mapped_column(Integer)
|
char_count: Mapped[int] = mapped_column(Integer)
|
||||||
word_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')
|
chapter: Mapped["Chapter"] = relationship("Chapter", back_populates="paragraph")
|
||||||
work: Mapped['Work'] = relationship('Work', back_populates='paragraph')
|
work: Mapped["Work"] = relationship("Work", back_populates="paragraph")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def find(cls, db_session: AsyncSession, character: str):
|
async def find(cls, db_session: AsyncSession, character: str):
|
||||||
|
@ -26,7 +26,7 @@ class User(Base):
|
|||||||
|
|
||||||
@password.setter
|
@password.setter
|
||||||
def password(self, password: str):
|
def password(self, password: str):
|
||||||
self._password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
self._password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
|
||||||
|
|
||||||
def check_password(self, password: str):
|
def check_password(self, password: str):
|
||||||
return pwd_context.verify(password, self.password)
|
return pwd_context.verify(password, self.password)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user