Skip to content

Commit

Permalink
Add support for names in contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrergeru committed May 24, 2024
1 parent dd6fead commit 1af5216
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions moira_client/models/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ def __init__(self, value='', type='', **kwargs):
self.type = type
self.value = value
self.user = kwargs.get('user', None)
self.name = kwargs.get('name', None)
self._id = kwargs.get('id', None)


class ContactManager:
def __init__(self, client):
self._client = client

def add(self, value, contact_type):
def add(self, value, contact_type, name=None):
"""
Add new contact
Expand All @@ -38,16 +39,23 @@ def add(self, value, contact_type):
:raises: ResponseStructureError
"""
data = {
'value': value,
'type': contact_type
}

contacts = self.fetch_by_current_user()
for contact in contacts:
if contact.value == value and contact.type == contact_type:
if name and contact.name != name:
contact.name = name
self.update(contact)
return contact
return contact

data = {
'value': value,
'type': contact_type
}
if name:
data['name'] = name

result = self._client.put(self._full_path(), json=data)
if 'id' not in result:
raise ResponseStructureError('No id in response', result)
Expand Down Expand Up @@ -135,6 +143,8 @@ def update(self, contact):
'type': contact.type,
'value': contact.value,
}
if contact.name:
data['name'] = contact.name

try:
self._client.put(self._full_path(contact.id), json=data)
Expand Down

0 comments on commit 1af5216

Please sign in to comment.