From ca288618f193dae8cc93d39b308c576788fcd9f7 Mon Sep 17 00:00:00 2001 From: Pavel Levin Date: Thu, 16 Jan 2014 02:40:23 -0800 Subject: [PATCH 1/2] Implements set_entry method I have a task - create an entry in the module and relate it with the other entry. The problem is that the dynamic method set_enty returned dictionary, but method relate occure SugarEntry object. I changed the code, for convenience redefined the dynamic creation methods, api for easy overriding them. Created method set_entry, which works as before, only returns SugarEntry object instead dictionary, which can be used further. Sorry for bad english! --- sugarcrm/sugarcrm.py | 58 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/sugarcrm/sugarcrm.py b/sugarcrm/sugarcrm.py index c2c4b92..dce0edb 100644 --- a/sugarcrm/sugarcrm.py +++ b/sugarcrm/sugarcrm.py @@ -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) @@ -77,6 +65,50 @@ 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 + """ + print(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 + + + 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): From d468e6855e19f307f59ae2d56525d685edf96357 Mon Sep 17 00:00:00 2001 From: Pavel Levin Date: Thu, 16 Jan 2014 04:07:12 -0800 Subject: [PATCH 2/2] some fixes --- sugarcrm/sugarcrm.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sugarcrm/sugarcrm.py b/sugarcrm/sugarcrm.py index dce0edb..105eba0 100644 --- a/sugarcrm/sugarcrm.py +++ b/sugarcrm/sugarcrm.py @@ -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', @@ -71,7 +71,6 @@ def _callMethod(self, method_name, *args): """ Call the SugarCRM REST API method """ - print(args) try: result = self._sendRequest(method_name, [self._session] + list(args))