Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements set_entry method #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions sugarcrm/sugarcrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, url, username, password, is_ldap_member=False):
for method in ['get_user_id', 'get_user_team_id',
'get_available_modules', 'get_module_fields',
'get_entries_count', 'get_entry', 'get_entries',
'get_entry_list', 'set_entry', 'set_entries',
'get_entry_list', 'set_entries',
'set_relationship', 'set_relationships',
'get_relationships', 'get_server_info',
'set_note_attachment', 'get_note_attachment',
Expand All @@ -55,19 +55,7 @@ def __init__(self, url, username, password, is_ldap_member=False):
# Use this to be able to evaluate "method".
def gen(method_name):
def f(*args):
try:
result = self._sendRequest(method_name,
[self._session] + list(args))
except SugarError, error:
if error.is_invalid_session():
# Try to recover if session ID was lost
self._login(self._username, self._password)
result = self._sendRequest(method_name,
[self._session] + list(args))
else:
raise SugarUnhandledException

return result
return self._callMethod(method_name, *args)

return f
self.__dict__[method] = gen(method)
Expand All @@ -77,6 +65,49 @@ def f(*args):
self.get_available_modules())

self.name_re = re.compile(r'([A-Z][^A-Z]*)')


def _callMethod(self, method_name, *args):
"""
Call the SugarCRM REST API method
"""
try:
result = self._sendRequest(method_name,
[self._session] + list(args))
except SugarError, error:
if error.is_invalid_session():
# Try to recover if session ID was lost
self._login(self._username, self._password)
result = self._sendRequest(method_name,
[self._session] + list(args))
else:
raise SugarUnhandledException

return result


def set_entry(self, module, data):
"""
Create an entry in the specified module and return entry object

Wrapper for SugarCRM REST API method set_entry

Keyword arguments:
module -- Module object where entry will be created
data -- Entry properties data
"""

# Call method
entry_data = self._callMethod('set_entry', *(module._name, data))

# Create new entry
new_entry = SugarEntry(module)

# Parametrize entry
for attribute in entry_data:
new_entry._fields[attribute] = entry_data[attribute]

return new_entry


def _sendRequest(self, method, data):
Expand Down