From d705d92135ea55cfebdb5deacd3ee8e2a289c4c1 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Fri, 6 Dec 2024 15:17:59 -0500 Subject: [PATCH] Remove `.content_items` property from `ChildTags` and `DescendantTags` helper classes --- integration/tests/posit/connect/test_tags.py | 21 +-- src/posit/connect/tags.py | 141 +------------------ 2 files changed, 6 insertions(+), 156 deletions(-) diff --git a/integration/tests/posit/connect/test_tags.py b/integration/tests/posit/connect/test_tags.py index 27a0aeb5..37aae32b 100644 --- a/integration/tests/posit/connect/test_tags.py +++ b/integration/tests/posit/connect/test_tags.py @@ -140,23 +140,11 @@ def test_tag_content_items(self): assert len(tagC.content_items.find()) == 3 # Make sure unique content items are found - assert len(tagB.child_tags.content_items.find()) == 0 - assert len(tagD.child_tags.content_items.find()) == 0 - assert len(tagB.descendant_tags.content_items.find()) == 0 - assert len(tagD.descendant_tags.content_items.find()) == 0 + content_items = tagA.content_items.find() + assert len(content_items) == 3 - child_content_items = tagC.child_tags.content_items.find() - assert len(child_content_items) == 1 - child_content_item_guids = {content_item["guid"] for content_item in child_content_items} - assert child_content_item_guids == {self.contentA["guid"]} - - descendant_content_items = tagA.descendant_tags.content_items.find() - assert len(descendant_content_items) == 3 - - descendant_content_item_guids = { - content_item["guid"] for content_item in descendant_content_items - } - assert descendant_content_item_guids == { + content_item_guids = {content_item["guid"] for content_item in content_items} + assert content_item_guids == { self.contentA["guid"], self.contentB["guid"], self.contentC["guid"], @@ -169,6 +157,7 @@ def test_tag_content_items(self): assert len(tagA.content_items.find()) == 0 assert len(tagB.content_items.find()) == 0 assert len(tagC.content_items.find()) == 0 + assert len(tagD.content_items.find()) == 0 # cleanup tagRoot.destroy() diff --git a/src/posit/connect/tags.py b/src/posit/connect/tags.py index a26e7c12..659088a2 100644 --- a/src/posit/connect/tags.py +++ b/src/posit/connect/tags.py @@ -13,38 +13,11 @@ class _RelatedTagsBase(ContextManager, ABC): - @property - @abstractmethod - def content_items(self) -> _TagContentItemsBase: - pass - @abstractmethod def find(self) -> list[Tag]: pass -class _TagContentItemsBase(ContextManager, ABC): - @staticmethod - def _unique_content_items(tags: list[Tag]) -> list[ContentItem]: - content_items: list[ContentItem] = [] - content_items_seen: set[str] = set() - - for tag in tags: - tag_content_items = tag.content_items.find() - - for content_item in tag_content_items: - content_item_guid = content_item["guid"] - - if content_item_guid not in content_items_seen: - content_items.append(content_item) - content_items_seen.add(content_item_guid) - - return content_items - - @abstractmethod - def find(self) -> list[ContentItem]: ... - - class Tag(Active): """Tag resource.""" @@ -163,7 +136,7 @@ def destroy(self) -> None: self._ctx.session.delete(url) -class TagContentItems(_TagContentItemsBase): +class TagContentItems(ContextManager): def __init__(self, ctx: Context, path: str) -> None: super().__init__() self._ctx = ctx @@ -205,29 +178,6 @@ def __init__(self, ctx: Context, path: str, /, *, parent_tag: Tag) -> None: self._parent_tag = parent_tag - @property - def content_items(self) -> ChildTagContentItems: - """ - Find all content items from the child tags. - - Returns - ------- - ChildTagContentItems - Helper class that can `.find()` all content items that are tagged with a child tag. - - Examples - -------- - ```python - import posit - - client = posit.connect.Client() - mytag = client.tags.get("TAG_ID_HERE") - - tagged_content_items = mytag.child_tags.content_items.find() - ``` - """ - return ChildTagContentItems(self._ctx, self._path, parent_tag=self._parent_tag) - def find(self) -> list[Tag]: """ Find all child tags that are direct children of a single tag. @@ -252,101 +202,12 @@ def find(self) -> list[Tag]: return child_tags -class ChildTagContentItems(_TagContentItemsBase): - def __init__(self, ctx: Context, path: str, /, *, parent_tag: Tag) -> None: - super().__init__() - self._ctx = ctx - self._path = path - self._parent_tag = parent_tag - - def find(self) -> list[ContentItem]: - """ - Find all content items that are tagged with a child tag. - - Returns - ------- - list[ContentItem] - List of content items that are tagged with a child tag. - - Examples - -------- - ```python - import posit - - client = posit.connect.Client() - mytag = client.tags.get("TAG_ID_HERE") - - tagged_content_items = mytag.child_tags.content_items.find() - ``` - """ - child_tags = self._parent_tag.child_tags.find() - content_items = self._unique_content_items(child_tags) - return content_items - - -class DescendantTagContentItems(_TagContentItemsBase): - def __init__(self, ctx: Context, /, *, parent_tag: Tag) -> None: - super().__init__() - self._ctx = ctx - self._parent_tag = parent_tag - - def find(self) -> list[ContentItem]: - """ - Find all content items that are tagged with a descendant tag. - - Returns - ------- - list[ContentItem] - List of content items that are tagged with a descendant tag. - - Examples - -------- - ```python - import posit - - client = posit.connect.Client() - mytag = client.tags.get("TAG_ID_HERE") - - tagged_content_items = mytag.descendant_tags.content_items.find() - ``` - """ - descendant_tags = self._parent_tag.descendant_tags.find() - content_items = self._unique_content_items(descendant_tags) - return content_items - - class DescendantTags(_RelatedTagsBase): def __init__(self, ctx: Context, /, *, parent_tag: Tag) -> None: super().__init__() self._ctx = ctx self._parent_tag = parent_tag - @property - def content_items(self) -> DescendantTagContentItems: - """ - Find all content items from the descendant tags. - - Returns - ------- - DescendantTagContentItems - Helper class that can `.find()` all content items that are tagged with a descendant tag. - - Examples - -------- - ```python - import posit - - client = posit.connect.Client() - mytag = client.tags.find(id="TAG_ID_HERE") - - tagged_content_items = mytag.descendant_tags.content_items.find() - ``` - """ - return DescendantTagContentItems( - self._ctx, - parent_tag=self._parent_tag, - ) - def find(self) -> list[Tag]: """ Find all child tags that descend from a single tag.