Skip to content

Commit

Permalink
provide Article.create_or_update_time_accounting()
Browse files Browse the repository at this point in the history
Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Mar 17, 2024
1 parent 8af7c45 commit a57aed3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
4 changes: 3 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ History
* **added features**

* added ``raise_if_exists`` parameter to :meth:`articles.Attachment.download` method
* support ``time_accountings`` endpoint by :class:`time_accountings.TimeAccountings`
* added support for the ``/time_accountings`` endpoint (for details see :doc:`api/time_accountings`)
* added :meth:`tickets.Ticket.time_accountings` and :meth:`tickets.Ticket.create_time_accounting`
* added :meth:`articles.Article.create_or_update_time_accounting`

* **fixes**

Expand Down
11 changes: 9 additions & 2 deletions tests/test_time_accountings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ def test_create_article_time_accounting(single_ticket):
single_ticket.reload()
assert single_ticket.time_unit is None

accounting = article.create_time_accounting("1.23")
accounting = article.create_or_update_time_accounting("1.23")
assert accounting.ticket_article == article
article.reload()
assert article.time_unit == "1.23"

new_accounting = article.create_or_update_time_accounting("2.34")
assert new_accounting == accounting
article.reload()
assert article.time_unit == "2.34"

single_ticket.reload()
assert single_ticket.time_unit == "1.23"
assert single_ticket.time_unit == "2.34"


def test_create_ticket_time_accounting_with_type(single_ticket):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
{"method": "POST", "url": "https://localhost/api/v1/tickets#fixture", "status_code": 201, "reason": "Created", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "905"}, "encoding": "utf-8", "content_size": 905}
{"id":343,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67328","title":"__pytest__","owner_id":1,"customer_id":26,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":1,"escalation_at":null,"pending_time":null,"type":null,"time_unit":null,"preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2024-03-17T19:28:10.896Z","updated_at":"2024-03-17T19:28:11.176Z","article_ids":[560],"ticket_time_accounting_ids":[]}
{"id":344,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67329","title":"__pytest__","owner_id":1,"customer_id":26,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":1,"escalation_at":null,"pending_time":null,"type":null,"time_unit":null,"preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2024-03-17T20:45:00.796Z","updated_at":"2024-03-17T20:45:01.419Z","article_ids":[562],"ticket_time_accounting_ids":[]}
{"method": "POST", "url": "https://localhost/api/v1/ticket_articles", "status_code": 201, "reason": "Created", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "562"}, "encoding": "utf-8", "content_size": 562}
{"id":561,"ticket_id":343,"type_id":10,"sender_id":1,"from":"zammadoo admin","to":null,"cc":null,"subject":null,"reply_to":null,"message_id":null,"message_id_md5":null,"in_reply_to":null,"content_type":"text/plain","references":null,"body":"...","internal":true,"preferences":{},"updated_by_id":3,"created_by_id":3,"origin_by_id":null,"created_at":"2024-03-17T19:28:11.359Z","updated_at":"2024-03-17T19:28:11.359Z","attachments":[],"created_by":"zammadoo@hephaestus.local","updated_by":"zammadoo@hephaestus.local","type":"note","sender":"Agent","time_unit":null}
{"method": "GET", "url": "https://localhost/api/v1/tickets/343", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "853"}, "encoding": "utf-8", "content_size": 853}
{"id":343,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67328","title":"__pytest__","owner_id":1,"customer_id":26,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":2,"escalation_at":null,"pending_time":null,"type":null,"time_unit":null,"preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2024-03-17T19:28:10.896Z","updated_at":"2024-03-17T19:28:11.403Z"}
{"id":563,"ticket_id":344,"type_id":10,"sender_id":1,"from":"zammadoo admin","to":null,"cc":null,"subject":null,"reply_to":null,"message_id":null,"message_id_md5":null,"in_reply_to":null,"content_type":"text/plain","references":null,"body":"...","internal":true,"preferences":{},"updated_by_id":3,"created_by_id":3,"origin_by_id":null,"created_at":"2024-03-17T20:45:01.604Z","updated_at":"2024-03-17T20:45:01.604Z","attachments":[],"created_by":"zammadoo@hephaestus.local","updated_by":"zammadoo@hephaestus.local","type":"note","sender":"Agent","time_unit":null}
{"method": "GET", "url": "https://localhost/api/v1/tickets/344", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "853"}, "encoding": "utf-8", "content_size": 853}
{"id":344,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67329","title":"__pytest__","owner_id":1,"customer_id":26,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":2,"escalation_at":null,"pending_time":null,"type":null,"time_unit":null,"preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2024-03-17T20:45:00.796Z","updated_at":"2024-03-17T20:45:01.650Z"}
{"method": "GET", "url": "https://localhost/api/v1/tickets/344/time_accountings", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "2"}, "encoding": "utf-8", "content_size": 2}
[]
{"method": "POST", "url": "https://localhost/api/v1/time_accountings", "status_code": 201, "reason": "Created", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "181"}, "encoding": "utf-8", "content_size": 181}
{"id":70,"ticket_id":343,"ticket_article_id":561,"time_unit":"1.23","type_id":null,"created_by_id":3,"created_at":"2024-03-17T19:28:11.924Z","updated_at":"2024-03-17T19:28:11.924Z"}
{"method": "GET", "url": "https://localhost/api/v1/tickets/343", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "855"}, "encoding": "utf-8", "content_size": 855}
{"id":343,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67328","title":"__pytest__","owner_id":1,"customer_id":26,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":2,"escalation_at":null,"pending_time":null,"type":null,"time_unit":"1.23","preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2024-03-17T19:28:10.896Z","updated_at":"2024-03-17T19:28:11.931Z"}
{"method": "DELETE", "url": "https://localhost/api/v1/tickets/343#fixture", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "text/html", "Content-Length": "0"}, "encoding": "ISO-8859-1", "content_size": 0}
{"id":72,"ticket_id":344,"ticket_article_id":563,"time_unit":"1.23","type_id":null,"created_by_id":3,"created_at":"2024-03-17T20:45:01.946Z","updated_at":"2024-03-17T20:45:01.946Z"}
{"method": "GET", "url": "https://localhost/api/v1/ticket_articles/563", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "564"}, "encoding": "utf-8", "content_size": 564}
{"id":563,"ticket_id":344,"type_id":10,"sender_id":1,"from":"zammadoo admin","to":null,"cc":null,"subject":null,"reply_to":null,"message_id":null,"message_id_md5":null,"in_reply_to":null,"content_type":"text/plain","references":null,"body":"...","internal":true,"preferences":{},"updated_by_id":3,"created_by_id":3,"origin_by_id":null,"created_at":"2024-03-17T20:45:01.604Z","updated_at":"2024-03-17T20:45:01.604Z","attachments":[],"created_by":"zammadoo@hephaestus.local","updated_by":"zammadoo@hephaestus.local","type":"note","sender":"Agent","time_unit":"1.23"}
{"method": "GET", "url": "https://localhost/api/v1/tickets/344/time_accountings", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "183"}, "encoding": "utf-8", "content_size": 183}
[{"id":72,"ticket_id":344,"ticket_article_id":563,"time_unit":"1.23","type_id":null,"created_by_id":3,"created_at":"2024-03-17T20:45:01.946Z","updated_at":"2024-03-17T20:45:01.946Z"}]
{"method": "PUT", "url": "https://localhost/api/v1/time_accountings/72", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "181"}, "encoding": "utf-8", "content_size": 181}
{"ticket_article_id":563,"time_unit":"2.34","id":72,"ticket_id":344,"type_id":null,"created_by_id":3,"created_at":"2024-03-17T20:45:01.946Z","updated_at":"2024-03-17T20:45:02.265Z"}
{"method": "GET", "url": "https://localhost/api/v1/ticket_articles/563", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "564"}, "encoding": "utf-8", "content_size": 564}
{"id":563,"ticket_id":344,"type_id":10,"sender_id":1,"from":"zammadoo admin","to":null,"cc":null,"subject":null,"reply_to":null,"message_id":null,"message_id_md5":null,"in_reply_to":null,"content_type":"text/plain","references":null,"body":"...","internal":true,"preferences":{},"updated_by_id":3,"created_by_id":3,"origin_by_id":null,"created_at":"2024-03-17T20:45:01.604Z","updated_at":"2024-03-17T20:45:01.604Z","attachments":[],"created_by":"zammadoo@hephaestus.local","updated_by":"zammadoo@hephaestus.local","type":"note","sender":"Agent","time_unit":"2.34"}
{"method": "GET", "url": "https://localhost/api/v1/tickets/344", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "application/json; charset=utf-8", "Content-Length": "855"}, "encoding": "utf-8", "content_size": 855}
{"id":344,"group_id":1,"priority_id":2,"state_id":1,"organization_id":null,"number":"67329","title":"__pytest__","owner_id":1,"customer_id":26,"note":null,"first_response_at":null,"first_response_escalation_at":null,"first_response_in_min":null,"first_response_diff_in_min":null,"close_at":null,"close_escalation_at":null,"close_in_min":null,"close_diff_in_min":null,"update_escalation_at":null,"update_in_min":null,"update_diff_in_min":null,"last_close_at":null,"last_contact_at":null,"last_contact_agent_at":null,"last_contact_customer_at":null,"last_owner_update_at":null,"create_article_type_id":10,"create_article_sender_id":1,"article_count":2,"escalation_at":null,"pending_time":null,"type":null,"time_unit":"2.34","preferences":{},"updated_by_id":3,"created_by_id":3,"created_at":"2024-03-17T20:45:00.796Z","updated_at":"2024-03-17T20:45:02.270Z"}
{"method": "DELETE", "url": "https://localhost/api/v1/tickets/344#fixture", "status_code": 200, "reason": "OK", "headers": {"Content-Type": "text/html", "Content-Length": "0"}, "encoding": "ISO-8859-1", "content_size": 0}

16 changes: 12 additions & 4 deletions zammadoo/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,25 @@ def attachments(self) -> List[Attachment]:
for info in self["attachments"]
]

def create_time_accounting(
def create_or_update_time_accounting(
self, time_unit: Union[str, float], **kwargs
) -> "TimeAccounting":
"""
Add accounted time to ticket article.
Create accounted time for ticket article.
If time accounting already exists, it will be updated.
:param time_unit: accounted time units
:param kwargs: type(`str` | :class:`TimeAccountingType`)
:param kwargs: type(`str`) or type_id(`int`)
:rtype: :class:`TimeAccounting`
"""
kwargs["ticket_article_id"] = self.id
aid = self.id
kwargs["ticket_article_id"] = aid

ticket = self.ticket
for accounting in ticket.time_accountings():
if accounting["ticket_article_id"] == aid:
return accounting.update(time_unit=str(time_unit), **kwargs)

return self.ticket.create_time_accounting(time_unit, **kwargs)


Expand Down
5 changes: 4 additions & 1 deletion zammadoo/tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def articles(self) -> List["Article"]:
return [articles(aid) for aid in sorted(article_ids)]

def time_accountings(self) -> List[TimeAccounting]:
"""
:return: all time accounting objects for the ticket.
"""
parent = self.parent
client = parent.client
time_accountings = client.time_accountings
Expand Down Expand Up @@ -183,7 +186,7 @@ def links(self) -> Dict[str, List["Ticket"]]:
"""
returns all linked tickets grouped by link type
:returns: ``{"normal": [Ticket, ...], "parent": [...], "child": [...]}``
:return: ``{"normal": [Ticket, ...], "parent": [...], "child": [...]}``
"""
parent = self.parent
client = parent.client
Expand Down

0 comments on commit a57aed3

Please sign in to comment.