From 81e51eefd34346e07a231da5d72ebecc22ea77c8 Mon Sep 17 00:00:00 2001 From: bensteUEM Date: Fri, 4 Oct 2024 20:03:54 +0200 Subject: [PATCH] Implement /person/masterdata #110 --- churchtools_api/persons.py | 30 ++++++++++++++++++++++++++++++ tests/test_churchtools_api.py | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/churchtools_api/persons.py b/churchtools_api/persons.py index ab1abe6..51e20a6 100644 --- a/churchtools_api/persons.py +++ b/churchtools_api/persons.py @@ -5,6 +5,7 @@ logger = logging.getLogger(__name__) + class ChurchToolsApiPersons(ChurchToolsApiAbstract): """ Part definition of ChurchToolsApi which focuses on persons @@ -64,3 +65,32 @@ def get_persons(self, **kwargs): "Persons requested failed: {}".format( response.status_code)) return None + + def get_persons_masterdata( + self, resultClass: str = None, returnAsDict: bool = False, **kwargs + ) -> dict[list[dict]]: + """ + Function to get the Masterdata of the persons module + This information is required to map some IDs to specific items + + Returns: + dict of lists of masterdata items each with list of dict items used as configuration + """ + url = self.domain + "/api/person/masterdata" + + headers = {"accept": "application/json"} + response = self.session.get(url=url, headers=headers) + + if response.status_code == 200: + response_content = json.loads(response.content) + response_data = response_content["data"].copy() + logger.debug("Person Masterdata load successful {}".format(response_data)) + + return response_data + else: + logger.warning( + "%s Something went wrong fetching person metadata: %s", + response.status_code, + response.content, + ) + return None diff --git a/tests/test_churchtools_api.py b/tests/test_churchtools_api.py index 296307e..8e6534f 100644 --- a/tests/test_churchtools_api.py +++ b/tests/test_churchtools_api.py @@ -5,7 +5,6 @@ import os from pathlib import Path import unittest -from datetime import datetime, timedelta from churchtools_api.churchtools_api import ChurchToolsApi @@ -19,6 +18,7 @@ log_directory.mkdir(parents=True) logging.config.dictConfig(config=logging_config) + class TestsChurchToolsApi(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestsChurchToolsApi, self).__init__(*args, **kwargs) @@ -130,6 +130,38 @@ def test_get_persons(self): result4 = self.api.get_persons(returnAsDict=False) self.assertIsInstance(result4, list) + def test_get_persons_masterdata(self) -> None: + """ + Tries to retrieve metadata for persons module + Expected sections equal those that were available on ELKW1610.krz.tools on 4.Oct.2024 + """ + EXPECTED_SECTIONS = { + "roles", + "ageGroups", + "targetGroups", + "groupTypes", + "groupCategories", + "groupStatuses", + "departments", + "statuses", + "campuses", + "contactLabels", + "growPaths", + "followUps", + "followUpIntervals", + "groupMeetingTemplates", + "relationshipTypes", + } + + result = self.api.get_persons_masterdata() + self.assertIsInstance(result, dict) + self.assertEqual(EXPECTED_SECTIONS, set(result.keys())) + + for section in result.values(): + self.assertIsInstance(section, list) + for item in section: + self.assertIsInstance(item, dict) + def test_get_songs(self) -> None: """ 1. Test requests all songs and checks that result has more than 10 elements (hence default pagination works)