Skip to content

Commit

Permalink
Merge pull request #27 from antonplagemann/development
Browse files Browse the repository at this point in the history
v3.2.1 Fix static contact field types
  • Loading branch information
antonplagemann authored Aug 16, 2021
2 parents 058a6f9 + e646874 commit 7b6bfaa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion GMSync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 41 additions & 1 deletion MonicaHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -104,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:
Expand Down Expand Up @@ -440,6 +442,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.'''
Expand Down Expand Up @@ -499,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,
Expand Down
4 changes: 2 additions & 2 deletions SyncHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
Expand Down Expand Up @@ -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"]
}
Expand Down

0 comments on commit 7b6bfaa

Please sign in to comment.