Skip to content

Commit

Permalink
add tags operations on the node (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Jul 5, 2023
1 parent 5ef5788 commit 5487878
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 13 deletions.
38 changes: 34 additions & 4 deletions papermerge_cli/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,48 @@ def post(
self,
url,
json,
response_model
response_model=None
):
response = requests.post(
f"{self.host}{url}",
headers=self.headers,
data=json
json=json
)

if response.status_code != 201:
if response.status_code not in (200, 201):
raise ValueError(response.text)

return response_model(**response.json())
if response_model:
return response_model(**response.json())

def patch(
self,
url,
json,
response_model=None
):
response = requests.patch(
f"{self.host}{url}",
headers=self.headers,
json=json
)

if response.status_code not in (200, 201):
raise ValueError(response.text)

if response_model:
return response_model(**response.json())

def delete(
self,
url,
json,
):
requests.delete(
f"{self.host}{url}",
headers=self.headers,
json=json
)

def upload(
self,
Expand Down
4 changes: 3 additions & 1 deletion papermerge_cli/format/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ def list_nodes(data: Paginator[Node]) -> Table:
table.add_column("Type")
table.add_column("Title")
table.add_column("UUID", no_wrap=True)
table.add_column("Tags")

for node in data.items:
table.add_row(
node.ctype,
node.title,
str(node.id)
str(node.id),
','.join(sorted(tag.name for tag in node.tags))
)

return table
37 changes: 36 additions & 1 deletion papermerge_cli/lib/nodes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import uuid
from typing import List

from papermerge_cli.rest import get_me, get_nodes
from papermerge_cli.rest import (get_me, get_nodes, node_add_tags,
node_assign_tags, node_remove_tags)
from papermerge_cli.schema import Node, Paginator, User
from papermerge_cli.types import NodeActionEnum


def list_nodes(
Expand Down Expand Up @@ -43,3 +46,35 @@ def list_nodes(
)

return data


def perform_node_command(
host: str,
token: str,
node_id: uuid.UUID,
action: NodeActionEnum,
tags: List[str]
):
if action in (NodeActionEnum.assign_tags, NodeActionEnum.replace_tags):
node_assign_tags(
host=host,
token=token,
node_id=node_id,
tags=tags
)
elif action in (NodeActionEnum.add_tags, NodeActionEnum.append_tags):
node_add_tags(
host=host,
token=token,
node_id=node_id,
tags=tags
)
elif action in (NodeActionEnum.remove_tags, NodeActionEnum.delete_tags):
node_remove_tags(
host=host,
token=token,
node_id=node_id,
tags=tags
)
else:
raise ValueError("Invalid node action")
32 changes: 31 additions & 1 deletion papermerge_cli/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import importlib.metadata
import uuid
from pathlib import Path
from typing import List

import click
import typer
Expand All @@ -11,9 +12,10 @@
import papermerge_cli.format.nodes as format_nodes
import papermerge_cli.format.users as format_users
from papermerge_cli.lib.importer import upload_file_or_folder
from papermerge_cli.lib.nodes import list_nodes
from papermerge_cli.lib.nodes import list_nodes, perform_node_command
from papermerge_cli.lib.users import me as perform_me
from papermerge_cli.schema import Node, Paginator, User
from papermerge_cli.types import NodeActionEnum

from .utils import sanitize_host

Expand All @@ -22,6 +24,7 @@
console = Console()
app = typer.Typer()


HostEnvVar = Annotated[
str,
typer.Option(
Expand All @@ -37,6 +40,16 @@
help='JWT authorization token'
),
]
NodeAction = Annotated[
NodeActionEnum,
typer.Argument(
help='add/removes/assign tags to the node'
)
]
NodeID = Annotated[
uuid.UUID,
typer.Option(help='Node UUID')
]
ParentFolderID = Annotated[
uuid.UUID,
typer.Option(help='Parent folder UUID')
Expand Down Expand Up @@ -187,6 +200,23 @@ def current_user_command(ctx: typer.Context):
console.print(ex)


@app.command(name="node")
def node_command(
ctx: typer.Context,
node_id: NodeID,
action: NodeAction,
tags: List[str]
):
"""Perform actions on specific node"""
perform_node_command(
host=ctx.obj['HOST'],
token=ctx.obj['TOKEN'],
node_id=node_id,
action=action,
tags=tags
)


"""
@click.command
@click.option(
Expand Down
6 changes: 5 additions & 1 deletion papermerge_cli/rest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from .documents import upload as upload_document
from .nodes import create_folder, get_nodes
from .nodes import (create_folder, get_nodes, node_add_tags, node_assign_tags,
node_remove_tags)
from .users import get_me

__all__ = [
get_me,
get_nodes,
node_add_tags,
node_remove_tags,
node_assign_tags,
upload_document,
create_folder
]
50 changes: 50 additions & 0 deletions papermerge_cli/rest/nodes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import List
from uuid import UUID

from papermerge_cli.api_client import ApiClient
Expand Down Expand Up @@ -25,6 +26,55 @@ def get_nodes(
return paginator


def node_assign_tags(
node_id: UUID,
host: str,
token: str,
tags: List[str]
):
"""Assigns list of tags to the node
Assignment operation will replace all current tags with the
new ones.
"""
api_client = ApiClient(token=token, host=host)
api_client.post(
f'/api/nodes/{node_id}/tags',
json=tags
)


def node_add_tags(
node_id: UUID,
host: str,
token: str,
tags: List[str]
):
"""Add list of tags to the node
Add operation will append new tags to the current one.
"""
api_client = ApiClient(token=token, host=host)
api_client.patch(
f'/api/nodes/{node_id}/tags',
json=tags
)


def node_remove_tags(
node_id: UUID,
host: str,
token: str,
tags: List[str]
):
"""Remove list of tags from the node"""
api_client = ApiClient(token=token, host=host)
api_client.delete(
f'/api/nodes/{node_id}/tags',
json=tags
)


def create_folder(
host: str,
token: str,
Expand Down
7 changes: 7 additions & 0 deletions papermerge_cli/schema/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ class DocumentNode(BaseModel):
ocr_status: OCRStatusEnum = OCRStatusEnum.unknown


class Tag(BaseModel):
name: str
bg_color: str
fg_color: str


class Node(BaseModel):
id: UUID
title: str
ctype: NodeType
tags: List[Tag]
created_at: datetime
updated_at: datetime
parent_id: UUID | None
Expand Down
9 changes: 9 additions & 0 deletions papermerge_cli/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ class OCRStatusEnum(str, Enum):
started = 'STARTED'
success = 'SUCCESS'
failed = 'FAILED'


class NodeActionEnum(str, Enum):
add_tags = "add-tags"
append_tags = "append-tags" # same as "add-tags"
assign_tags = "assign-tags"
replace_tags = "replace-tags" # same as "assign-tags"
remove_tags = "remove-tags"
delete_tags = "delete-tags" # same as "remove-tags"
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ backoff = "^2.2.1"
rich = "^13.4.2"
pydantic = {extras = ["email"], version = "^1.10.9"}
requests = "^2.31.0"
laconiq = "^0.1.0"
laconiq = "^0.2.0"
typer = {extras = ["all"], version = "^0.9.0"}

[tool.poetry.scripts]
Expand Down

0 comments on commit 5487878

Please sign in to comment.