Skip to content

Commit

Permalink
Merge pull request #330 from flablog/new_department
Browse files Browse the repository at this point in the history
[person/department] new func to add department or get one by name
  • Loading branch information
EvanBldy authored Aug 7, 2024
2 parents 675ddd1 + d824787 commit 02f618d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
49 changes: 49 additions & 0 deletions gazu/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ def get_all_month_time_spents(id, date, client=default):
)


@cache
def get_department_by_name(name, client=default):
"""
Args:
name (str): Department name.
Returns:
dict: Department corresponding to given name.
"""
return raw.fetch_first(
"departments",
{"name": name},
client=client,
)


@cache
def get_department(department_id, client=default):
"""
Args:
department_id (str): An uuid identifying a department.
Returns:
dict: Department corresponding to given department_id.
"""
return raw.fetch_one("departments", department_id, client=client)


@cache
def get_person(id, relations=False, client=default):
"""
Expand Down Expand Up @@ -182,6 +210,27 @@ def get_organisation(client=default):
return raw.get("auth/authenticated", client=client)["organisation"]


def new_department(name, color="", archived=False, client=default):
"""
Create a new departement based on given parameters.
Args:
name (str): the name of the departement.
color (str): the color of the departement.
archived (bool): Whether the departement is archived or not.
Returns:
dict: Created departement.
"""
department = get_department_by_name(name, client=client)
if department is None:
department = raw.post(
"data/departments",
{"name": name, "color": color, "archived": archived},
client=client,
)
return department


def new_person(
first_name,
last_name,
Expand Down
33 changes: 33 additions & 0 deletions tests/test_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,39 @@ def test_get_person_by_full_name(self):
person = gazu.person.get_person_by_full_name("", "John", "Doe")
self.assertEqual(person["id"], "person-1")

def test_get_department_by_name(self):
with requests_mock.mock() as mock:
mock_route(
mock,
"GET",
"data/departments?name=department-1",
text=[{"name": "department-1"}],
)
department = gazu.person.get_department_by_name("department-1")
self.assertEqual(department["name"], "department-1")

def test_get_department(self):
with requests_mock.mock() as mock:
mock_route(
mock,
"GET",
"data/departments/%s" % fakeid("department-1"),
text={"name": "department-1"},
)
department = gazu.person.get_department(fakeid("department-1"))
self.assertEqual(department["name"], "department-1")

def test_new_department(self):
with requests_mock.mock() as mock:
result = {"name": "department-1"}
mock_route(
mock, "GET", "data/departments?name=department-1", text=[]
)
mock_route(mock, "POST", "data/departments", text=result)
self.assertEqual(
gazu.person.new_department("department-1"), result
)

def test_get_person_by_desktop_login(self):
with requests_mock.mock() as mock:
mock.get(
Expand Down

0 comments on commit 02f618d

Please sign in to comment.