Skip to content

Commit fd92a25

Browse files
authored
added files.get_tags function (#260)
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>
1 parent f6a3c3d commit fd92a25

File tree

7 files changed

+569
-489
lines changed

7 files changed

+569
-489
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

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

5-
## [0.14.0 - 2024-05-xx]
5+
## [0.14.0 - 2024-05-31]
66

77
### Added
88

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

1113
## [0.13.0 - 2024-04-28]
1214

nc_py_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
NextcloudMissingCapabilities,
88
)
99
from ._version import __version__
10-
from .files import FilePermissions, FsNode, LockType
10+
from .files import FilePermissions, FsNode, LockType, SystemTag
1111
from .files.sharing import ShareType
1212
from .nextcloud import AsyncNextcloud, AsyncNextcloudApp, Nextcloud, NextcloudApp

nc_py_api/files/_files.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ def build_list_tags_response(response: Response) -> list[SystemTag]:
168168
return result
169169

170170

171+
def build_tags_ids_for_object(url_to_fetch: str, response: Response) -> list[int]:
172+
result = []
173+
records = _webdav_response_to_records(response, "list_tags_ids")
174+
for record in records:
175+
prop_stat = record["d:propstat"]
176+
if str(prop_stat.get("d:status", "")).find("200 OK") != -1:
177+
href_suffix = str(record["d:href"]).removeprefix(url_to_fetch).strip("/")
178+
if href_suffix:
179+
result.append(int(href_suffix))
180+
return result
181+
182+
171183
def build_update_tag_req(
172184
name: str | None, user_visible: bool | None, user_assignable: bool | None
173185
) -> ElementTree.Element:

nc_py_api/files/files.py

Lines changed: 14 additions & 482 deletions
Large diffs are not rendered by default.

nc_py_api/files/files_async.py

Lines changed: 523 additions & 0 deletions
Large diffs are not rendered by default.

nc_py_api/nextcloud.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
from .ex_app.occ_commands import AsyncOccCommandsAPI, OccCommandsAPI
3535
from .ex_app.providers.providers import AsyncProvidersApi, ProvidersApi
3636
from .ex_app.ui.ui import AsyncUiApi, UiApi
37-
from .files.files import AsyncFilesAPI, FilesAPI
37+
from .files.files import FilesAPI
38+
from .files.files_async import AsyncFilesAPI
3839
from .loginflow_v2 import _AsyncLoginFlowV2API, _LoginFlowV2API
3940
from .notes import _AsyncNotesAPI, _NotesAPI
4041
from .notifications import _AsyncNotificationsAPI, _NotificationsAPI

tests/actual_tests/files_test.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212

1313
import pytest
1414

15-
from nc_py_api import FsNode, LockType, NextcloudException, NextcloudExceptionNotFound
15+
from nc_py_api import (
16+
FsNode,
17+
LockType,
18+
NextcloudException,
19+
NextcloudExceptionNotFound,
20+
SystemTag,
21+
)
1622

1723

1824
class MyBytesIO(BytesIO):
@@ -1152,7 +1158,7 @@ async def test_create_update_delete_tag_async(anc_any):
11521158
await anc_any.files.update_tag(tag)
11531159

11541160

1155-
def test_assign_unassign_tag(nc_any):
1161+
def test_get_assign_unassign_tag(nc_any):
11561162
with contextlib.suppress(NextcloudExceptionNotFound):
11571163
nc_any.files.delete_tag(nc_any.files.tag_by_name("test_nc_py_api"))
11581164
with contextlib.suppress(NextcloudExceptionNotFound):
@@ -1167,8 +1173,10 @@ def test_assign_unassign_tag(nc_any):
11671173
assert tag2.user_assignable is False
11681174
new_file = nc_any.files.upload("/test_dir_tmp/tag_test.txt", content=b"")
11691175
new_file = nc_any.files.by_id(new_file)
1176+
assert nc_any.files.get_tags(new_file) == []
11701177
assert len(nc_any.files.list_by_criteria(tags=[tag1])) == 0
11711178
nc_any.files.assign_tag(new_file, tag1)
1179+
assert isinstance(nc_any.files.get_tags(new_file)[0], SystemTag)
11721180
assert len(nc_any.files.list_by_criteria(tags=[tag1])) == 1
11731181
assert len(nc_any.files.list_by_criteria(["favorite"], tags=[tag1])) == 0
11741182
assert len(nc_any.files.list_by_criteria(tags=[tag1, tag2.tag_id])) == 0
@@ -1182,7 +1190,7 @@ def test_assign_unassign_tag(nc_any):
11821190

11831191

11841192
@pytest.mark.asyncio(scope="session")
1185-
async def test_assign_unassign_tag_async(anc_any):
1193+
async def test_get_assign_unassign_tag_async(anc_any):
11861194
with contextlib.suppress(NextcloudExceptionNotFound):
11871195
await anc_any.files.delete_tag(await anc_any.files.tag_by_name("test_nc_py_api"))
11881196
with contextlib.suppress(NextcloudExceptionNotFound):
@@ -1197,8 +1205,10 @@ async def test_assign_unassign_tag_async(anc_any):
11971205
assert tag2.user_assignable is False
11981206
new_file = await anc_any.files.upload("/test_dir_tmp/tag_test.txt", content=b"")
11991207
new_file = await anc_any.files.by_id(new_file)
1208+
assert await anc_any.files.get_tags(new_file) == []
12001209
assert len(await anc_any.files.list_by_criteria(tags=[tag1])) == 0
12011210
await anc_any.files.assign_tag(new_file, tag1)
1211+
assert isinstance((await anc_any.files.get_tags(new_file))[0], SystemTag)
12021212
assert len(await anc_any.files.list_by_criteria(tags=[tag1])) == 1
12031213
assert len(await anc_any.files.list_by_criteria(["favorite"], tags=[tag1])) == 0
12041214
assert len(await anc_any.files.list_by_criteria(tags=[tag1, tag2.tag_id])) == 0

0 commit comments

Comments
 (0)