Skip to content

Commit

Permalink
refactor properties with UserListProperty
Browse files Browse the repository at this point in the history
Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Mar 24, 2024
1 parent fc0b577 commit 2578257
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
12 changes: 3 additions & 9 deletions zammadoo/groups.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, Optional

from .resource import NamedResource
from .resource import NamedResource, UserListProperty
from .resources import CreatableT, IterableT

if TYPE_CHECKING:
from .client import Client
from .users import User


class Group(NamedResource):
Expand All @@ -17,19 +16,14 @@ class Group(NamedResource):
parent: "Groups" # type: ignore[assignment]

shared_drafts: bool #:
user_ids: List[int] #:
users = UserListProperty() #:

@property
def parent_group(self) -> Optional["Group"]:
"""available since Zammad version 6.2"""
pid: int = self["parent_id"]
return None if pid is None else self.parent(pid)

@property
def users(self) -> List["User"]:
users = self.parent.client.users
return [users(uid) for uid in self.user_ids]


class Groups(IterableT[Group], CreatableT[Group]):
"""Groups(...)"""
Expand Down
19 changes: 5 additions & 14 deletions zammadoo/organizations.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from typing import TYPE_CHECKING, List
from typing import TYPE_CHECKING

from .resource import NamedResource
from .resource import NamedResource, UserListProperty
from .resources import CreatableT, SearchableT

if TYPE_CHECKING:
from .client import Client
from .users import User


class Organization(NamedResource):
"""Organization(...)"""

shared: bool #:
domain: str #:
domain_assignment: bool #:

@property
def members(self) -> List["User"]:
users = self.parent.client.users
return [users(mid) for mid in self["member_ids"]]

@property
def secondary_members(self) -> List["User"]:
users = self.parent.client.users
return [users(sec_mid) for sec_mid in self["secondary_member_ids"]]
members = UserListProperty() #:
secondary_members = UserListProperty() #:
shared: bool #:

@property
def weburl(self) -> str:
Expand Down
10 changes: 8 additions & 2 deletions zammadoo/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
# -*- coding: UTF-8 -*-

from datetime import datetime
from typing import TYPE_CHECKING, Any, Optional, Tuple
from typing import TYPE_CHECKING, Any, List, Optional, Tuple

from .resources import ResourcesT, _T_co
from .utils import AttributeT, DateTime, FrozenInfo, _AttributeBase

if TYPE_CHECKING:
# noinspection PyUnresolvedReferences
from .users import User
from .utils import JsonDict

Expand Down Expand Up @@ -71,6 +70,13 @@ def __get__(self, instance: Resource, owner=None) -> "User":
return instance.parent.client.users(instance[f"{self.name}_id"])


class UserListProperty(_AttributeBase):
def __get__(self, instance: Resource, owner=None) -> List["User"]:
user = instance.parent.client.users
uids = instance[f"{self.name[:-1]}_ids"]
return [user(uid) for uid in uids]


class OptionalUserProperty(_AttributeBase):
def __get__(self, instance: Resource, owner=None) -> Optional["User"]:
value = instance[f"{self.name}_id"]
Expand Down

0 comments on commit 2578257

Please sign in to comment.