Skip to content

Commit

Permalink
Upgrade to poetry 2.0 (#580)
Browse files Browse the repository at this point in the history
ciur authored Jan 25, 2025
1 parent 99fa8a4 commit 60e8a01
Showing 30 changed files with 1,651 additions and 999 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ jobs:
- name: Install python dependencies
run: |
python -m pip install --upgrade pip
pip install poetry==1.8.2
pip install poetry==2.0.1
poetry install --with test && poetry run pytest papermerge/
env:
PAPERMERGE__DATABASE__URL: 'sqlite:///test.sqlite'
@@ -59,7 +59,7 @@ jobs:
- name: Install python dependencies
run: |
python -m pip install --upgrade pip
pip install poetry==1.8.2
pip install poetry==2.0.1
poetry install --with test -E pg && poetry run pytest papermerge/
env:
PAPERMERGE__DATABASE__URL: 'postgresql://pmguser:pmgpass@localhost:5432/test_pmgdb'
12 changes: 12 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,18 @@

<!-- towncrier release notes start -->

## [3.4] - Soon

### Changes

- pyproject.toml upgraded for poetry 2.0

### Adds

- Column sorting and filtering in Document Type list view
- Column sorting and filtering in Custom Field list view


## [3.3.1] - 2025-01-19

### Changes
2 changes: 1 addition & 1 deletion docker/standard/Dockerfile
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ RUN apk update && apk add linux-headers python3-dev \
libpq-dev \
poppler-utils

RUN pip install --upgrade poetry==1.8.2 roco==0.4.2
RUN pip install --upgrade poetry==2.0.1 roco==0.4.2
RUN curl -L -o /bin/env2js https://github.com/papermerge/env2js/releases/download/0.2/env2js.amd64
RUN chmod +x /bin/env2js

37 changes: 35 additions & 2 deletions papermerge/core/features/custom_fields/db/api.py
Original file line number Diff line number Diff line change
@@ -2,31 +2,64 @@
import logging
import uuid

from sqlalchemy import select, func
from sqlalchemy import select, func, or_
from sqlalchemy.orm import Session


from papermerge.core import schema, orm


logger = logging.getLogger(__name__)


ORDER_BY_MAP = {
"type": orm.CustomField.type.asc(),
"-type": orm.CustomField.type.desc(),
"name": orm.CustomField.name.asc(),
"-name": orm.CustomField.name.desc(),
}


def get_custom_fields(
db_session: Session, *, user_id: uuid.UUID, page_size: int, page_number: int
db_session: Session,
*,
user_id: uuid.UUID,
page_size: int,
page_number: int,
filter: str,
order_by: str = "name",
) -> schema.PaginatedResponse[schema.CustomField]:
stmt_total_cf = select(func.count(orm.CustomField.id)).where(
orm.CustomField.user_id == user_id
)
if filter:
stmt_total_cf = stmt_total_cf.where(
or_(
orm.CustomField.name.icontains(filter),
orm.CustomField.type.icontains(filter),
)
)

total_cf = db_session.execute(stmt_total_cf).scalar()
order_by_value = ORDER_BY_MAP.get(order_by, orm.CustomField.name.asc())

offset = page_size * (page_number - 1)
stmt = (
select(orm.CustomField)
.where(orm.CustomField.user_id == user_id)
.limit(page_size)
.offset(offset)
.order_by(order_by_value)
)

if filter:
stmt = stmt.where(
or_(
orm.CustomField.name.icontains(filter),
orm.CustomField.type.icontains(filter),
)
)

db_cfs = db_session.scalars(stmt).all()
items = [schema.CustomField.model_validate(db_cf) for db_cf in db_cfs]

9 changes: 6 additions & 3 deletions papermerge/core/features/custom_fields/router.py
Original file line number Diff line number Diff line change
@@ -12,9 +12,10 @@
from papermerge.core.features.custom_fields import schema as cf_schema
from papermerge.core.features.custom_fields.db import api as dbapi
from papermerge.core.routers.common import OPEN_API_GENERIC_JSON_DETAIL
from papermerge.core.routers.params import CommonQueryParams
from papermerge.core.features.users.schema import User

from .types import PaginatedQueryParams

router = APIRouter(
prefix="/custom-fields",
tags=["custom-fields"],
@@ -46,9 +47,9 @@ def get_custom_fields(
user: Annotated[
User, Security(get_current_user, scopes=[scopes.CUSTOM_FIELD_VIEW])
],
params: CommonQueryParams = Depends(),
params: PaginatedQueryParams = Depends(),
):
"""Get all (paginated) custom fields
"""Get paginated list of custom fields
Required scope: `{scope}`
"""
@@ -58,6 +59,8 @@ def get_custom_fields(
user_id=user.id,
page_size=params.page_size,
page_number=params.page_number,
order_by=params.order_by,
filter=params.filter,
)

return result
14 changes: 14 additions & 0 deletions papermerge/core/features/custom_fields/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from enum import Enum

from papermerge.core.types import PaginatedQueryParams as BaseParams


class OrderBy(str, Enum):
name_asc = "name"
name_desc = "-name"
type_asc = "type"
type_desc = "-type"


class PaginatedQueryParams(BaseParams):
order_by: OrderBy | None = None
23 changes: 22 additions & 1 deletion papermerge/core/features/document_types/db/api.py
Original file line number Diff line number Diff line change
@@ -30,22 +30,43 @@ def get_document_types_without_pagination(
return items


ORDER_BY_MAP = {
"name": orm.DocumentType.name.asc(),
"-name": orm.DocumentType.name.desc(),
}


def get_document_types(
session: Session, user_id: uuid.UUID, page_size: int, page_number: int
session: Session,
user_id: uuid.UUID,
page_size: int,
page_number: int,
filter: str | None = None,
order_by: str = "name",
) -> schema.PaginatedResponse[schema.DocumentType]:

stmt_total_doc_types = select(func.count(DocumentType.id)).where(
DocumentType.user_id == user_id
)
if filter:
stmt_total_doc_types = stmt_total_doc_types.where(
orm.DocumentType.name.icontains(filter)
)
total_doc_types = session.execute(stmt_total_doc_types).scalar()
order_by_value = ORDER_BY_MAP.get(order_by, orm.DocumentType.name.asc())

offset = page_size * (page_number - 1)

stmt = (
select(DocumentType)
.where(DocumentType.user_id == user_id)
.limit(page_size)
.offset(offset)
.order_by(order_by_value)
)
if filter:
stmt = stmt.where(orm.DocumentType.name.icontains(filter))

db_items = session.scalars(stmt).all()
items = [schema.DocumentType.model_validate(db_item) for db_item in db_items]

6 changes: 4 additions & 2 deletions papermerge/core/features/document_types/router.py
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@
from papermerge.core.features.auth import get_current_user
from papermerge.core.features.auth import scopes
from papermerge.core.routers.common import OPEN_API_GENERIC_JSON_DETAIL
from papermerge.core.routers.params import CommonQueryParams
from papermerge.core.features.users import schema as users_schema
from .types import PaginatedQueryParams

router = APIRouter(
prefix="/document-types",
@@ -46,7 +46,7 @@ def get_document_types(
user: Annotated[
users_schema.User, Security(get_current_user, scopes=[scopes.CUSTOM_FIELD_VIEW])
],
params: CommonQueryParams = Depends(),
params: PaginatedQueryParams = Depends(),
) -> schema.PaginatedResponse[schema.DocumentType]:
"""Get all (paginated) document types
@@ -58,6 +58,8 @@ def get_document_types(
user_id=user.id,
page_size=params.page_size,
page_number=params.page_number,
order_by=params.order_by,
filter=params.filter,
)

return paginated_response
12 changes: 12 additions & 0 deletions papermerge/core/features/document_types/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from enum import Enum

from papermerge.core.types import PaginatedQueryParams as BaseParams


class OrderBy(str, Enum):
name_asc = "name"
name_desc = "-name"


class PaginatedQueryParams(BaseParams):
order_by: OrderBy | None = None
9 changes: 9 additions & 0 deletions papermerge/core/types.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
from datetime import date
from enum import Enum
from typing import Generic, Literal, TypeAlias, TypeVar
from fastapi import Query

from pydantic import BaseModel, ConfigDict

@@ -53,3 +54,11 @@ class CFVValueColumn(str, Enum):
MONETARY = 'value_monetary'
BOOLEAN = 'value_boolean'
YEARMONTH = 'value_yearmonth'


class PaginatedQueryParams(BaseModel):
page_size: int = Query(5, ge=1, description="Number of items per page")
page_number: int = Query(
1, ge=1, description="Page number. It is first, second etc. page?"
)
filter: str | None = None
2 changes: 1 addition & 1 deletion papermerge/core/version.py
Original file line number Diff line number Diff line change
@@ -2,4 +2,4 @@

# In order for this to work, you need to run first:
# $ poetry install
__version__ = importlib.metadata.version("papermerge-core")
__version__ = importlib.metadata.version("papermerge")
Loading

0 comments on commit 60e8a01

Please sign in to comment.