Skip to content

Commit

Permalink
alost works!
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Jan 7, 2024
1 parent 729d0cb commit 91aeccc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
7 changes: 4 additions & 3 deletions papermerge/core/db/doc_ver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ def get_last_doc_ver(
"""
with Session(engine) as session: # noqa
stmt = select(DocumentVersion).where(
document_id=doc_id,
user_id=user_id
)
DocumentVersion.document_id == doc_id
).order_by(
DocumentVersion.number.desc()
).limit(1)
db_doc_ver = session.scalars(stmt).one()
model_doc_ver = schemas.DocumentVersion.model_validate(db_doc_ver)

Expand Down
26 changes: 19 additions & 7 deletions papermerge/core/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@


class Base(DeclarativeBase):
created_at: Mapped[datetime] = mapped_column(
insert_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
insert_default=func.now(),
onupdate=func.now()
)
pass


class User(Base):
Expand All @@ -30,6 +24,13 @@ class User(Base):
)
home_folder_id: Mapped[UUID] = mapped_column(ForeignKey("Node.id"))
inbox_folder_id: Mapped[UUID] = mapped_column(ForeignKey("Node.id"))
created_at: Mapped[datetime] = mapped_column(
insert_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
insert_default=func.now(),
onupdate=func.now()
)


CType = Literal["document", "folder"]
Expand All @@ -49,6 +50,13 @@ class Node(Base):
)
user_id: Mapped[UUID] = mapped_column(ForeignKey("core_user.id"))
parent_id: Mapped[UUID] = mapped_column(ForeignKey("node.id"))
created_at: Mapped[datetime] = mapped_column(
insert_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
insert_default=func.now(),
onupdate=func.now()
)

__mapper_args__ = {
"polymorphic_identity": "node",
Expand Down Expand Up @@ -97,12 +105,16 @@ class DocumentVersion(Base):
ForeignKey("core_document.basetreenode_ptr_id")
)

lang: Mapped[str]
short_description: Mapped[str]


class Page(Base):
__tablename__ = "core_page"

id: Mapped[UUID] = mapped_column(primary_key=True)
number: Mapped[int]
lang: Mapped[str]
document_version_id: Mapped[UUID] = mapped_column(
ForeignKey("core_documentversion.id")
)
7 changes: 4 additions & 3 deletions papermerge/core/db/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ def get_first_page(
"""
with Session(engine) as session: # noqa
stmt = select(Page).where(
document_version_id=doc_ver_id,
user_id=user_id
)
Page.document_version_id == doc_ver_id
).order_by(
Page.number.asc()
).limit(1)
db_page = session.scalars(stmt).one()
db_model = schemas.Page.model_validate(db_page)

Expand Down
3 changes: 2 additions & 1 deletion papermerge/core/routers/thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def retrieve_document_thumbnail(
)

last_version = doc.versions.last()

first_page = last_version.pages.first()

if first_page is None:
Expand Down Expand Up @@ -105,7 +106,7 @@ def retrieve_document_thumbnail2(
document_id: uuid.UUID,
size: int = DEFAULT_THUMBNAIL_SIZE,
user: schemas.User = Depends(get_current_user),
engine: db.Engine = db.get_engine()
engine: db.Engine = Depends(db.get_engine)
):
"""Retrieves thumbnail of the document last version's first page"""
try:
Expand Down

0 comments on commit 91aeccc

Please sign in to comment.