Skip to content

Commit

Permalink
refactor Ticket.links()
Browse files Browse the repository at this point in the history
* update tests and docstring

Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Apr 11, 2024
1 parent e162b2b commit 739f980
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions tests/test_tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_lazy_attribute_caching(caplog, rclient):
with caplog.at_level(logging.INFO, logger="zammadoo"):
_ = ticket["number"]
with pytest.raises(KeyError):
ticket["doesnotexist"]
_ = ticket["doesnotexist"]

# expect only one request without expand
assert caplog.record_tuples == [
Expand All @@ -206,7 +206,7 @@ def test_lazy_attribute_caching(caplog, rclient):
with caplog.at_level(logging.INFO, logger="zammadoo"):
_ = ticket["owner"]
with pytest.raises(KeyError):
ticket["stilldoesnotexist"]
_ = ticket["stilldoesnotexist"]

# expect one more request with expand
assert caplog.record_tuples == [
Expand Down
29 changes: 21 additions & 8 deletions zammadoo/tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,34 @@ def links(self) -> Dict[str, List["Ticket"]]:
"""
returns all linked tickets grouped by link type
To loop over all linked tickets at once use `itertools.chain()`:
::
for linked_ticked in itertools.chain(*ticket.links().values()):
print(linked_ticket)
:return: ``{"normal": [Ticket, ...], "parent": [...], "child": [...]}``
"""

parent = self.parent
client = parent.client
params = {"link_object": "Ticket", "link_object_value": self.id}
link_map = dict((key, []) for key in LINK_TYPES)

items: _TypedJson = client.get("links", params=params, _erase_return_type=True)
items: _TypedJson = client.get(
"links",
params={"link_object": "Ticket", "link_object_value": self.id},
_erase_return_type=True,
)
cache_assets(client, items["assets"])

links = items["links"]
for item in links:
link_map: Dict[str, List["Ticket"]] = {}
for item in items["links"]:
assert item["link_object"] == "Ticket"
link_type = item["link_type"]
link_map.setdefault(link_type, []).append(parent(item["link_object_value"]))
link_map.setdefault(item["link_type"], []).append(
parent(item["link_object_value"])
)
for link_type in LINK_TYPES:
if link_type not in link_map:
link_map[link_type] = []

return link_map

Expand Down

0 comments on commit 739f980

Please sign in to comment.