Skip to content

Commit

Permalink
Remove .content_items property from ChildTags and `DescendantTags…
Browse files Browse the repository at this point in the history
…` helper classes
  • Loading branch information
schloerke committed Dec 6, 2024
1 parent ef4bc00 commit d705d92
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 156 deletions.
21 changes: 5 additions & 16 deletions integration/tests/posit/connect/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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()
Expand Down
141 changes: 1 addition & 140 deletions src/posit/connect/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit d705d92

Please sign in to comment.