Skip to content

Commit 267f58f

Browse files
committed
Add content_items to child / descendant tags
1 parent 6f8fb46 commit 267f58f

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

src/posit/connect/tags.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ def __init__(self, ctx: Context, path: str, /, *, parent_tag: Tag) -> None:
146146
self._path = path
147147
self._parent_tag = parent_tag
148148

149+
def content_items(self) -> ChildTagContentItems:
150+
"""
151+
Find all content items from the child tags.
152+
153+
Returns
154+
-------
155+
ChildTagContentItems
156+
Helper class that can `.find()` all content items that are tagged with a child tag.
157+
158+
Examples
159+
--------
160+
```python
161+
import posit
162+
163+
client = posit.connect.Client(...)
164+
165+
mytag = client.tags.find(id="TAG_ID_HERE")
166+
tagged_content_items = mytag.child_tags.content_items.find()
167+
```
168+
"""
169+
return ChildTagContentItems(self._ctx, self._path, parent_tag=self._parent_tag)
170+
149171
def find(self) -> list[Tag]:
150172
"""
151173
Find all child tags that are direct children of a single tag.
@@ -166,13 +188,56 @@ def find(self) -> list[Tag]:
166188
return child_tags
167189

168190

191+
class ChildTagContentItems(ContextManager):
192+
def __init__(self, ctx: Context, path: str, /, *, parent_tag: Tag) -> None:
193+
super().__init__()
194+
self._ctx = ctx
195+
self._path = path
196+
self._parent_tag = parent_tag
197+
198+
def find(self) -> list[ContentItem]:
199+
"""
200+
Find all content items that are tagged with a child tag.
201+
202+
Returns
203+
-------
204+
list[ContentItem]
205+
List of content items that are tagged with a child tag.
206+
"""
207+
child_tags = self._parent_tag.child_tags.find()
208+
content_items = DescendantTagContentItems._unique_content_items(child_tags)
209+
return content_items
210+
211+
169212
class DescendantTags(ContextManager):
170213
def __init__(self, ctx: Context, /, *, parent_tag: Tag) -> None:
171214
super().__init__()
172215
self._ctx = ctx
173216
self._path = "v1/tags"
174217
self._parent_tag = parent_tag
175218

219+
def content_items(self) -> DescendantTagContentItems:
220+
"""
221+
Find all content items from the descendant tags.
222+
223+
Returns
224+
-------
225+
DescendantTagContentItems
226+
Helper class that can `.find()` all content items that are tagged with a descendant tag.
227+
228+
Examples
229+
--------
230+
```python
231+
import posit
232+
233+
client = posit.connect.Client(...)
234+
235+
mytag = client.tags.find(id="TAG_ID_HERE")
236+
tagged_content_items = mytag.descendant_tags.content_items.find()
237+
```
238+
"""
239+
return DescendantTagContentItems(self._ctx, self._path, parent_tag=self._parent_tag)
240+
176241
def find(self) -> list[Tag]:
177242
"""
178243
Find all child tags that descend from a single tag.
@@ -218,6 +283,44 @@ def find(self) -> list[Tag]:
218283
return child_tags
219284

220285

286+
class DescendantTagContentItems(ContextManager):
287+
def __init__(self, ctx: Context, path: str, /, *, parent_tag: Tag) -> None:
288+
super().__init__()
289+
self._ctx = ctx
290+
self._path = path
291+
self._parent_tag = parent_tag
292+
293+
@staticmethod
294+
def _unique_content_items(tags: list[Tag]) -> list[ContentItem]:
295+
content_items: list[ContentItem] = []
296+
content_items_seen: set[str] = set()
297+
298+
for tag in tags:
299+
tag_content_items = tag.content_items.find()
300+
301+
for content_item in tag_content_items:
302+
content_item_guid = content_item["guid"]
303+
304+
if content_item_guid not in content_items_seen:
305+
content_items.append(content_item)
306+
content_items_seen.add(content_item_guid)
307+
308+
return content_items
309+
310+
def find(self) -> list[ContentItem]:
311+
"""
312+
Find all content items that are tagged with a descendant tag.
313+
314+
Returns
315+
-------
316+
list[ContentItem]
317+
List of content items that are tagged with a descendant tag.
318+
"""
319+
descendant_tags = self._parent_tag.descendant_tags.find()
320+
content_items = self._unique_content_items(descendant_tags)
321+
return content_items
322+
323+
221324
class Tags(ContextManager):
222325
"""Content item tags resource."""
223326

0 commit comments

Comments
 (0)