From 62664706a2550b86eea7458875604f00bc38501e Mon Sep 17 00:00:00 2001 From: flashdagger Date: Mon, 13 Nov 2023 23:21:51 +0100 Subject: [PATCH] add User.longname attribute Signed-off-by: flashdagger --- tests/test_users.py | 21 +++++++++++++++++++++ zammadoo/users.py | 15 ++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/tests/test_users.py b/tests/test_users.py index 31ed1ca..2d6e6d1 100644 --- a/tests/test_users.py +++ b/tests/test_users.py @@ -27,6 +27,27 @@ def test_user_fullname_attribute_given_firstname_and_lastname(client): assert user.fullname == "Max Headroom" +def test_user_fullname_attribute_given_email_only(client): + user = client.users( + 123, info={"id": 123, "firstname": "", "lastname": "", "email": "max@head.room"} + ) + assert user.fullname == "max@head.room" + + +def test_user_longname_attribute(client): + user = client.users( + 123, + info={ + "id": 123, + "firstname": "Max", + "lastname": "Headroom", + "organization_id": 123, + }, + ) + client.organizations(123, info={"id": 123, "name": "Network 23"}) + assert user.longname == "Max Headroom (Network 23)" + + def test_user_groups_attribute(client): user = client.users( 123, diff --git a/zammadoo/users.py b/zammadoo/users.py index e8a32bf..6d74831 100644 --- a/zammadoo/users.py +++ b/zammadoo/users.py @@ -37,11 +37,16 @@ class User(NamedResource): @property def fullname(self) -> str: - """users firstname and lastname combined""" - firstname, lastname = self["firstname"], self["lastname"] - return ( - f"{firstname or ''}{' ' if firstname and lastname else ''}{lastname or ''}" - ) + """users firstname and lastname combined, or email""" + fullname = " ".join(filter(bool, (self["firstname"], self["lastname"]))) + return fullname or self["email"] + + @property + def longname(self) -> str: + """users fullname with organization""" + fullname = self.fullname + organization = self.organization + return f"{fullname} ({organization.name})" if organization else fullname @property def name(self) -> str: