Skip to content

Commit

Permalink
minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Jan 9, 2024
1 parent 337cd87 commit d6a872e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions papermerge/core/db/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class UserNotFound(Exception):
pass


class PageNotFound(Exception):
pass
2 changes: 1 addition & 1 deletion papermerge/core/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class DocumentVersion(Base):

id: Mapped[UUID] = mapped_column(primary_key=True)
number: Mapped[int]
file_name: Mapped[str]
document_id: Mapped[UUID] = mapped_column(
ForeignKey("core_document.basetreenode_ptr_id")
)

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

Expand Down
13 changes: 11 additions & 2 deletions papermerge/core/db/pages.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from uuid import UUID

from sqlalchemy import Engine, select
from sqlalchemy import Engine, exc, select
from sqlalchemy.orm import Session

from papermerge.core import schemas
from papermerge.core.db.models import Page

from .exceptions import PageNotFound


def get_first_page(
engine: Engine,
Expand All @@ -21,7 +23,14 @@ def get_first_page(
).order_by(
Page.number.asc()
).limit(1)
db_page = session.scalars(stmt).one()
try:
db_page = session.scalars(stmt).one()
except exc.NoResultFound:
session.close()
raise PageNotFound(
f"DocVerID={doc_ver_id} does not have pages."
" Maybe it does not have associated file yet?"
)
model = schemas.Page.model_validate(db_page)

return model
5 changes: 3 additions & 2 deletions papermerge/core/routers/thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from papermerge.core import schemas
from papermerge.core.auth import get_current_user
from papermerge.core.constants import DEFAULT_THUMBNAIL_SIZE
from papermerge.core.db import exceptions as db_exc
from papermerge.core.models import Document
from papermerge.core.pathlib import rel2abs, thumbnail_path
from papermerge.core.utils import image
Expand Down Expand Up @@ -116,10 +117,10 @@ def retrieve_document_thumbnail(
doc_id=document_id
)
page = db.get_first_page(engine, doc_ver_id=doc_ver.id)
except Document.DoesNotExist:
except db_exc.PageNotFound as e:
raise HTTPException(
status_code=404,
detail="Page does not exist"
detail=f"DocID={document_id}: {e}"
)

if page is None:
Expand Down

0 comments on commit d6a872e

Please sign in to comment.