From a7ec5acd40ed873d562bcfd3f81d3ba489ab84aa Mon Sep 17 00:00:00 2001 From: Anton Plagemann <54081139+antonplagemann@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:22:07 +0200 Subject: [PATCH 1/2] Fix static contact field types #25 --- GMSync.py | 2 +- MonicaHelper.py | 39 +++++++++++++++++++++++++++++++++++++++ SyncHelper.py | 4 ++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/GMSync.py b/GMSync.py index 70478cc..75b9c00 100644 --- a/GMSync.py +++ b/GMSync.py @@ -17,7 +17,7 @@ from MonicaHelper import Monica from SyncHelper import Sync -VERSION = "v3.2.0" +VERSION = "v3.2.1" DATABASE_FILENAME = "syncState.db" LOG_FILENAME = 'Sync.log' # Google -> Monica contact syncing script diff --git a/MonicaHelper.py b/MonicaHelper.py index fad604d..1109b91 100644 --- a/MonicaHelper.py +++ b/MonicaHelper.py @@ -22,6 +22,7 @@ def __init__(self, log: Logger, databaseHandler: Database, token: str, base_url: self.dataAlreadyFetched = False self.contacts = [] self.genderMapping = {} + self.contactFieldTypeMapping = {} self.updatedContacts = {} self.createdContacts = {} self.deletedContacts = {} @@ -440,6 +441,44 @@ def getContactFields(self, monicaId: str, name: str) -> List[dict]: continue raise Exception(f"'{name}' ('{monicaId}'): Error fetching Monica contact fields: {error}") + def getContactFieldId(self, typeName: str) -> str: + '''Returns the id for a Monica contact field.''' + # Fetch if not present yet + if not self.contactFieldTypeMapping: + self.__getContactFieldTypes() + + # Get contact field id + fieldId = self.contactFieldTypeMapping.get(typeName, None) + + # No id is a serious issue + if not fieldId: + raise Exception(f"Could not find an id for contact field type '{typeName}'") + + return fieldId + + def __getContactFieldTypes(self) -> dict: + '''Fetches all contact field types from Monica and saves them to a dictionary.''' + + while True: + # Get genders + response = requests.get( + self.base_url + f"/contactfieldtypes", headers=self.header, params=self.parameters) + self.apiRequests += 1 + + # If successful + if response.status_code == 200: + contactFieldTypes = response.json()['data'] + contactFieldTypeMapping = {field['type']: field['id'] for field in contactFieldTypes} + self.contactFieldTypeMapping = contactFieldTypeMapping + return self.contactFieldTypeMapping + else: + error = response.json()['error']['message'] + if self.__isSlowDownError(response, error): + continue + self.log.error(f"Failed to fetch contact field types from Monica: {error}") + raise Exception("Error fetching contact field types from Monica!") + + def createContactField(self, monicaId: str, data: dict, name: str) -> None: '''Creates a contact field (phone number, email, etc.) for a given Monica contact id via api call.''' diff --git a/SyncHelper.py b/SyncHelper.py index 8859e7e..f185982 100644 --- a/SyncHelper.py +++ b/SyncHelper.py @@ -340,7 +340,7 @@ def __syncEmail(self, googleContact: dict, monicaContact: dict, monicaContactFie if googleContactEmails: googleEmails = [ { - "contact_field_type_id": 1, + "contact_field_type_id": self.monica.getContactFieldId('email'), "data": email["value"].strip(), "contact_id": monicaContact["id"] } @@ -385,7 +385,7 @@ def __syncPhone(self, googleContact: dict, monicaContact: dict, monicaContactFie if googleContactPhones: googlePhones = [ { - "contact_field_type_id": 2, + "contact_field_type_id": self.monica.getContactFieldId('phone'), "data": number["value"].strip(), "contact_id": monicaContact["id"] } From e646874af2089aa0363d908c99c4aac054eebf8c Mon Sep 17 00:00:00 2001 From: Anton Plagemann <54081139+antonplagemann@users.noreply.github.com> Date: Sun, 15 Aug 2021 22:35:00 +0200 Subject: [PATCH 2/2] Change default value and add logging #26 --- MonicaHelper.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MonicaHelper.py b/MonicaHelper.py index 1109b91..10504c3 100644 --- a/MonicaHelper.py +++ b/MonicaHelper.py @@ -105,6 +105,7 @@ def updateContact(self, monicaId: str, data: dict) -> None: if self.__isSlowDownError(response, error): continue self.log.error(f"'{name}' ('{monicaId}'): Error updating Monica contact: {error}. Does it exist?") + self.log.error(f"Monica form data: {data}") raise Exception("Error updating Monica contact!") def deleteContact(self, monicaId: str, name: str) -> None: @@ -538,7 +539,7 @@ class MonicaContactUploadForm(): def __init__(self, monica: Monica, firstName: str, lastName: str = None, nickName: str = None, middleName: str = None, genderType: str = 'O', birthdateDay: str = None, birthdateMonth: str = None, birthdateYear: str = None, - birthdateAgeBased: bool = None, isBirthdateKnown: bool = False, + birthdateAgeBased: bool = False, isBirthdateKnown: bool = False, isDeceased: bool = False, isDeceasedDateKnown: bool = False, deceasedDay: int = None, deceasedMonth: int = None, deceasedYear: int = None, deceasedAgeBased: bool = None,