Skip to content

Commit fbd96b5

Browse files
committedMar 23, 2025·
be changes/group ownership for tags
1 parent abb2b6f commit fbd96b5

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed
 

‎papermerge/core/features/tags/db/api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ def get_tag(
102102

103103

104104
def create_tag(
105-
db_session, attrs: schema.CreateTag, user_id: uuid.UUID
105+
db_session, attrs: schema.CreateTag
106106
) -> Tuple[schema.Tag | None, schema.Error | None]:
107107

108-
db_tag = orm.Tag(user_id=user_id, **attrs.model_dump())
108+
db_tag = orm.Tag(**attrs.model_dump())
109109
db_session.add(db_tag)
110110

111111
try:

‎papermerge/core/features/tags/router.py

+35-6
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
from typing import Annotated
33
from uuid import UUID
44

5-
from fastapi import APIRouter, Depends, HTTPException, Security
5+
from fastapi import APIRouter, Depends, HTTPException, Security, status
66

77
from papermerge.core.db.engine import Session
88
from papermerge.core import utils
99
from papermerge.core.features.users import schema as usr_schema
1010
from papermerge.core.features.auth import get_current_user
1111
from papermerge.core.features.auth import scopes
1212

13-
from papermerge.core.routers.params import CommonQueryParams
14-
13+
from papermerge.core.features.users.db import api as users_dbapi
1514
from papermerge.core.features.tags.db import api as tags_dbapi
1615
from papermerge.core.features.tags import schema as tags_schema
1716
from papermerge.core.exceptions import EntityNotFound
17+
from papermerge.core.routers.common import OPEN_API_GENERIC_JSON_DETAIL
1818
from .types import PaginatedQueryParams
1919

20+
2021
router = APIRouter(
2122
prefix="/tags",
2223
tags=["tags"],
@@ -88,20 +89,48 @@ def get_tag_details(
8889
return tag
8990

9091

91-
@router.post("/", status_code=201)
92+
@router.post(
93+
"/",
94+
status_code=201,
95+
responses={
96+
status.HTTP_403_FORBIDDEN: {
97+
"description": """User does not belong to group""",
98+
"content": OPEN_API_GENERIC_JSON_DETAIL,
99+
}
100+
},
101+
)
92102
@utils.docstring_parameter(scope=scopes.TAG_CREATE)
93103
def create_tag(
94104
attrs: tags_schema.CreateTag,
95105
user: Annotated[
96106
usr_schema.User, Security(get_current_user, scopes=[scopes.TAG_CREATE])
97107
],
98108
) -> tags_schema.Tag:
99-
"""Creates user tag
109+
"""Creates tag
110+
111+
If attribute `group_id` is present, tag will be owned
112+
by respective group, otherwise ownership is set to current user.
113+
If attribute `group_id` is present then current user should
114+
belong to that group, otherwise http status 403 (Forbidden) will
115+
be raised.
100116
101117
Required scope: `{scope}`
102118
"""
119+
if not attrs.group_id:
120+
attrs.user_id = user.id
121+
103122
with Session() as db_session:
104-
tag, error = tags_dbapi.create_tag(db_session, attrs=attrs, user_id=user.id)
123+
if attrs.group_id:
124+
group_id = attrs.group_id
125+
ok = users_dbapi.user_belongs_to(
126+
db_session, user_id=user.id, group_id=group_id
127+
)
128+
if not ok:
129+
detail = f"User {user.id=} does not belong to group {group_id=}"
130+
raise HTTPException(
131+
status_code=status.HTTP_403_FORBIDDEN, detail=detail
132+
)
133+
tag, error = tags_dbapi.create_tag(db_session, attrs=attrs)
105134

106135
if error:
107136
raise HTTPException(status_code=400, detail=error.model_dump())

‎papermerge/core/features/tags/schema.py

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class CreateTag(BaseModel):
2424
fg_color: str = DEFAULT_TAG_FG_COLOR
2525
description: str | None = None
2626
pinned: bool = False
27+
group_id: UUID | None = None
28+
user_id: UUID | None = None
2729

2830
# Config
2931
model_config = ConfigDict(from_attributes=True)
@@ -35,6 +37,8 @@ class UpdateTag(BaseModel):
3537
fg_color: Optional[str] = None
3638
description: Optional[str] = None
3739
pinned: Optional[bool] = False
40+
group_id: UUID | None = None
41+
user_id: UUID | None = None
3842

3943
# Config
4044
model_config = ConfigDict(from_attributes=True)

0 commit comments

Comments
 (0)
Please sign in to comment.