Skip to content

Commit

Permalink
refactor time_accountings type objects
Browse files Browse the repository at this point in the history
Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Mar 16, 2024
1 parent 5b9b0cc commit 156c9fc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
12 changes: 6 additions & 6 deletions tests/test_time_accountings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def test_ticket_time_accounting(ticket_pair):
ticket_a, ticket_b = ticket_pair
accountings = ticket_a.time_accountings
accountings = ticket_a.time_accountings()
assert not accountings

client = ticket_a.parent.client
Expand All @@ -25,18 +25,18 @@ def test_ticket_time_accounting(ticket_pair):
new_article = ticket_a.create_article(
"article with added accounting", time_unit="4.5"
)
accountings = ticket_a.time_accountings
accountings = ticket_a.time_accountings()
assert len(accountings) == 2
assert accountings[1].ticket_article == new_article

accountings[1].delete()
accountings = ticket_a.time_accountings
accountings = ticket_a.time_accountings()
assert len(accountings) == 1

accounting_types = client.time_accountings.types()
accounting_types = list(client.time_accountings.types.values())
assert accounting_types
type_name = accounting_types[0]["name"]
type_id = accounting_types[0]["id"]
type_name = accounting_types[0].name
type_id = accounting_types[0].id

new_accounting = accountings[0].update(type=type_name)
assert new_accounting.type == type_name
Expand Down
5 changes: 3 additions & 2 deletions tests/test_types_tickets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@
reveal_type(article.attachments) # N: Revealed type is "builtins.list[zammadoo.articles.Attachment]"
reveal_type(ticket.tags()) # N: Revealed type is "builtins.list[builtins.str]"
reveal_type(ticket.time_accountings) # N: Revealed type is "builtins.list[zammadoo.time_accountings.TimeAccounting]"
t_acc = ticket.time_accountings[0]
taccs = ticket.time_accountings()
reveal_type(taccs) # N: Revealed type is "builtins.list[zammadoo.time_accountings.TimeAccounting]"
t_acc = taccs[0]
reveal_type(t_acc.ticket) # N: Revealed type is "zammadoo.tickets.Ticket"
reveal_type(t_acc.time_unit) # N: Revealed type is "builtins.str"
reveal_type(t_acc.created_by) # N: Revealed type is "zammadoo.users.User"
Expand Down
3 changes: 2 additions & 1 deletion zammadoo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ def delete(self, *args, json: Optional["StringKeyMapping"] = None):
@cached_property
def server_version(self) -> str:
"""the Zammad server version"""
return str(self.get("version")["version"])
version: str = self.get("version")["version"]
return version

@cached_property
def weburl(self) -> str:
Expand Down
1 change: 0 additions & 1 deletion zammadoo/tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def articles(self) -> List["Article"]:
article_ids: List[int] = self["article_ids"]
return [articles(aid) for aid in sorted(article_ids)]

@property
def time_accountings(self) -> List[TimeAccounting]:
parent = self.parent
client = parent.client
Expand Down
31 changes: 24 additions & 7 deletions zammadoo/time_accountings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# -*- coding: UTF-8 -*-

from datetime import datetime
from typing import TYPE_CHECKING, Optional, Union
from typing import TYPE_CHECKING, Dict, Optional, Union

from .resource import MutableResource
from .resources import CreatableT, IterableT, _T_co
from .utils import FrozenInfo

if TYPE_CHECKING:
from .articles import Article
Expand All @@ -21,7 +22,6 @@ class TimeAccounting(MutableResource):
EXPANDED_ATTRIBUTES = ("type",)

id: int #:
type_id: Optional[int] #:
ticket_id: int #:
ticket_article_id: Optional[int] #:
created_at: datetime #:
Expand All @@ -45,9 +45,10 @@ def ticket_article(self) -> Optional["Article"]:

@property
def type(self) -> Optional[str]:
if self["type_id"] is None:
type_id: int = self["type_id"]
if type_id is None:
return None
return str(self["type"])
return self.parent.client.time_accountings.types[type_id].name

def update(self: _T_co, **kwargs) -> _T_co:
"""
Expand All @@ -73,12 +74,22 @@ def update(self: _T_co, **kwargs) -> _T_co:
return super().update(**kwargs)


class TimeAccountingsType(FrozenInfo):
id: int
name: str
note: str
active: bool
created_at: datetime
updated_at: datetime


class TimeAccountings(IterableT[TimeAccounting], CreatableT[TimeAccounting]):
"""TimeAccountings(...)"""

_RESOURCE_TYPE = TimeAccounting

def __init__(self, client: "Client"):
self._types: Optional[Dict[int, TimeAccountingsType]] = None
super().__init__(client, "time_accountings")

def create(
Expand All @@ -97,6 +108,12 @@ def create(

return self._create(json=json)

def types(self):
"""returns a list of properties for each defined accounting type"""
return self.client.get("time_accounting/types")
@property
def types(self) -> Dict[int, TimeAccountingsType]:
"""returns a mapping of properties for each defined accounting type"""
if self._types is None:
self._types = {
info["id"]: TimeAccountingsType(info)
for info in self.client.get("time_accounting/types")
}
return self._types

0 comments on commit 156c9fc

Please sign in to comment.