Skip to content

Commit

Permalink
added files.get_tags function (#260)
Browse files Browse the repository at this point in the history
Fixes #259

```python3

    nc = nc_py_api.Nextcloud(nc_auth_user="admin", nc_auth_pass="admin", nextcloud_url="http://stable29.local")
    some_file = nc.files.by_path("New folderz/1.txt")
    some_file_tags = nc.files.get_tags(some_file)
    print(some_file_tags)
```

Output:

`[<SystemTag id=1, name=first>, <SystemTag id=2, name=second>]`

---------

Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
bigcat88 authored May 30, 2024
1 parent f6a3c3d commit fd92a25
Show file tree
Hide file tree
Showing 7 changed files with 569 additions and 489 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

All notable changes to this project will be documented in this file.

## [0.14.0 - 2024-05-xx]
## [0.14.0 - 2024-05-31]

### Added

- NextcloudApp: `nc.ui.files_dropdown_menu.register_ex` to register new version of FileActions(AppAPI 2.6.0+)
- `LoginFlowV2` implementation by @blvdek #255
- NextcloudApp: `nc.ui.files_dropdown_menu.register_ex` to register new version of FileActions(AppAPI 2.6.0+) #252
- `files.get_tags` function to get all tags assigned to the file or directory. #260

## [0.13.0 - 2024-04-28]

Expand Down
2 changes: 1 addition & 1 deletion nc_py_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
NextcloudMissingCapabilities,
)
from ._version import __version__
from .files import FilePermissions, FsNode, LockType
from .files import FilePermissions, FsNode, LockType, SystemTag
from .files.sharing import ShareType
from .nextcloud import AsyncNextcloud, AsyncNextcloudApp, Nextcloud, NextcloudApp
12 changes: 12 additions & 0 deletions nc_py_api/files/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ def build_list_tags_response(response: Response) -> list[SystemTag]:
return result


def build_tags_ids_for_object(url_to_fetch: str, response: Response) -> list[int]:
result = []
records = _webdav_response_to_records(response, "list_tags_ids")
for record in records:
prop_stat = record["d:propstat"]
if str(prop_stat.get("d:status", "")).find("200 OK") != -1:
href_suffix = str(record["d:href"]).removeprefix(url_to_fetch).strip("/")
if href_suffix:
result.append(int(href_suffix))
return result


def build_update_tag_req(
name: str | None, user_visible: bool | None, user_assignable: bool | None
) -> ElementTree.Element:
Expand Down
496 changes: 14 additions & 482 deletions nc_py_api/files/files.py

Large diffs are not rendered by default.

523 changes: 523 additions & 0 deletions nc_py_api/files/files_async.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion nc_py_api/nextcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
from .ex_app.occ_commands import AsyncOccCommandsAPI, OccCommandsAPI
from .ex_app.providers.providers import AsyncProvidersApi, ProvidersApi
from .ex_app.ui.ui import AsyncUiApi, UiApi
from .files.files import AsyncFilesAPI, FilesAPI
from .files.files import FilesAPI
from .files.files_async import AsyncFilesAPI
from .loginflow_v2 import _AsyncLoginFlowV2API, _LoginFlowV2API
from .notes import _AsyncNotesAPI, _NotesAPI
from .notifications import _AsyncNotificationsAPI, _NotificationsAPI
Expand Down
16 changes: 13 additions & 3 deletions tests/actual_tests/files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

import pytest

from nc_py_api import FsNode, LockType, NextcloudException, NextcloudExceptionNotFound
from nc_py_api import (
FsNode,
LockType,
NextcloudException,
NextcloudExceptionNotFound,
SystemTag,
)


class MyBytesIO(BytesIO):
Expand Down Expand Up @@ -1152,7 +1158,7 @@ async def test_create_update_delete_tag_async(anc_any):
await anc_any.files.update_tag(tag)


def test_assign_unassign_tag(nc_any):
def test_get_assign_unassign_tag(nc_any):
with contextlib.suppress(NextcloudExceptionNotFound):
nc_any.files.delete_tag(nc_any.files.tag_by_name("test_nc_py_api"))
with contextlib.suppress(NextcloudExceptionNotFound):
Expand All @@ -1167,8 +1173,10 @@ def test_assign_unassign_tag(nc_any):
assert tag2.user_assignable is False
new_file = nc_any.files.upload("/test_dir_tmp/tag_test.txt", content=b"")
new_file = nc_any.files.by_id(new_file)
assert nc_any.files.get_tags(new_file) == []
assert len(nc_any.files.list_by_criteria(tags=[tag1])) == 0
nc_any.files.assign_tag(new_file, tag1)
assert isinstance(nc_any.files.get_tags(new_file)[0], SystemTag)
assert len(nc_any.files.list_by_criteria(tags=[tag1])) == 1
assert len(nc_any.files.list_by_criteria(["favorite"], tags=[tag1])) == 0
assert len(nc_any.files.list_by_criteria(tags=[tag1, tag2.tag_id])) == 0
Expand All @@ -1182,7 +1190,7 @@ def test_assign_unassign_tag(nc_any):


@pytest.mark.asyncio(scope="session")
async def test_assign_unassign_tag_async(anc_any):
async def test_get_assign_unassign_tag_async(anc_any):
with contextlib.suppress(NextcloudExceptionNotFound):
await anc_any.files.delete_tag(await anc_any.files.tag_by_name("test_nc_py_api"))
with contextlib.suppress(NextcloudExceptionNotFound):
Expand All @@ -1197,8 +1205,10 @@ async def test_assign_unassign_tag_async(anc_any):
assert tag2.user_assignable is False
new_file = await anc_any.files.upload("/test_dir_tmp/tag_test.txt", content=b"")
new_file = await anc_any.files.by_id(new_file)
assert await anc_any.files.get_tags(new_file) == []
assert len(await anc_any.files.list_by_criteria(tags=[tag1])) == 0
await anc_any.files.assign_tag(new_file, tag1)
assert isinstance((await anc_any.files.get_tags(new_file))[0], SystemTag)
assert len(await anc_any.files.list_by_criteria(tags=[tag1])) == 1
assert len(await anc_any.files.list_by_criteria(["favorite"], tags=[tag1])) == 0
assert len(await anc_any.files.list_by_criteria(tags=[tag1, tag2.tag_id])) == 0
Expand Down

0 comments on commit fd92a25

Please sign in to comment.