format code

This commit is contained in:
Jakub Miazek 2023-10-21 15:37:12 +02:00
parent 3291fa784e
commit e75a4cce05
4 changed files with 63 additions and 51 deletions

View File

@ -16,7 +16,11 @@ engine = create_async_engine(
# expire_on_commit=False will prevent attributes from being expired
# after commit.
AsyncSessionFactory = async_sessionmaker(engine, autoflush=False, expire_on_commit=False,)
AsyncSessionFactory = async_sessionmaker(
engine,
autoflush=False,
expire_on_commit=False,
)
# Dependency

View File

@ -17,7 +17,6 @@ class Nonsense(Base):
description: Mapped[str | None]
# TODO: apply relation to other tables
@classmethod
async def find(cls, db_session: AsyncSession, name: str):
"""
@ -36,4 +35,3 @@ class Nonsense(Base):
)
else:
return instance

View File

@ -1,20 +1,25 @@
from typing import List, Optional
from sqlalchemy import Column, ForeignKeyConstraint, Integer, PrimaryKeyConstraint, String, Table, Text, \
UniqueConstraint, select
from sqlalchemy import (
Column,
ForeignKeyConstraint,
Integer,
PrimaryKeyConstraint,
String,
Table,
Text,
UniqueConstraint,
select,
)
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(DeclarativeBase):
pass
class Character(Base):
__tablename__ = 'character'
__table_args__ = (
PrimaryKeyConstraint('id', name='character_pkey'),
{'schema': 'shakespeare'}
)
__tablename__ = "character"
__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))
@ -22,16 +27,15 @@ class Character(Base):
abbrev: Mapped[str | None] = mapped_column(String(32))
description: Mapped[str | None] = mapped_column(String(2056))
work: Mapped[list['Work']] = relationship('Work', secondary='shakespeare.character_work', back_populates='character')
paragraph: Mapped[list['Paragraph']] = relationship('Paragraph', back_populates='character')
work: Mapped[list["Work"]] = relationship(
"Work", secondary="shakespeare.character_work", back_populates="character"
)
paragraph: Mapped[list["Paragraph"]] = relationship("Paragraph", back_populates="character")
class Wordform(Base):
__tablename__ = 'wordform'
__table_args__ = (
PrimaryKeyConstraint('id', name='wordform_pkey'),
{'schema': 'shakespeare'}
)
__tablename__ = "wordform"
__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))
@ -41,11 +45,8 @@ class Wordform(Base):
class Work(Base):
__tablename__ = 'work'
__table_args__ = (
PrimaryKeyConstraint('id', name='work_pkey'),
{'schema': 'shakespeare'}
)
__tablename__ = "work"
__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))
@ -57,18 +58,22 @@ class Work(Base):
total_paragraphs: Mapped[int] = mapped_column(Integer)
notes: Mapped[str | None] = mapped_column(Text)
character: Mapped[list['Character']] = relationship('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')
character: Mapped[list["Character"]] = relationship(
"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")
class Chapter(Base):
__tablename__ = 'chapter'
__tablename__ = "chapter"
__table_args__ = (
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'),
{'schema': 'shakespeare'}
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"
),
{"schema": "shakespeare"},
)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
@ -77,29 +82,34 @@ class Chapter(Base):
chapter_number: Mapped[int] = mapped_column(Integer)
description: Mapped[str] = mapped_column(String(256))
work: Mapped['Work'] = relationship('Work', back_populates='chapter')
paragraph: Mapped[list['Paragraph']] = relationship('Paragraph', back_populates='chapter')
work: Mapped["Work"] = relationship("Work", back_populates="chapter")
paragraph: Mapped[list["Paragraph"]] = relationship("Paragraph", back_populates="chapter")
t_character_work = Table(
'character_work', 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'),
PrimaryKeyConstraint('character_id', 'work_id', name='character_work_pkey'),
schema='shakespeare'
"character_work",
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"),
PrimaryKeyConstraint("character_id", "work_id", name="character_work_pkey"),
schema="shakespeare",
)
class Paragraph(Base):
__tablename__ = 'paragraph'
__tablename__ = "paragraph"
__table_args__ = (
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(['work_id'], ['shakespeare.work.id'], name='paragraph_work_id_fkey'),
PrimaryKeyConstraint('id', name='paragraph_pkey'),
{'schema': 'shakespeare'}
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(["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)
@ -115,9 +125,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')
chapter: Mapped['Chapter'] = relationship('Chapter', back_populates='paragraph')
work: Mapped['Work'] = relationship('Work', 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")
@classmethod
async def find(cls, db_session: AsyncSession, character: str):

View File

@ -26,7 +26,7 @@ class User(Base):
@password.setter
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):
return pwd_context.verify(password, self.password)