1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
24 from sugarsoap_services import *
25 from sugarsoap_services_types import *
27 from tools.translate import _
29 from lxml import etree
31 import import_sugarcrm
36 class LoginError(Exception): pass
38 def login(username, password, url):
40 loc = sugarsoapLocator()
41 portType = loc.getsugarsoapPortType(url)
42 request = loginRequest()
43 uauth = ns0.user_auth_Def(request)
44 request._user_auth = uauth
46 uauth._user_name = username
47 uauth._password = hashlib.md5(password).hexdigest()
48 uauth._version = '1.1'
50 response = portType.login(request)
52 raise osv.except_osv(_('Error !'), _('Authentication error !\nBad Username or Password bad SugarSoap Api url !'))
53 if -1 == response._return._id:
54 raise LoginError(response._return._error._description)
56 return (portType, response._return._id)
58 def relation_search(portType, sessionid, module_name=None, module_id=None, related_module=None, query=None, deleted=None):
59 se_req = get_relationshipsRequest()
60 se_req._session = sessionid
61 se_req._module_name = module_name
62 se_req._module_id = module_id
63 se_req._related_module = related_module
64 se_resp = portType.get_relationships(se_req)
67 list = se_resp._return.get_element_ids()
69 ans_list.append(i.get_element_id())
72 def attachment_search(portType, sessionid, module_name, module_id=None):
73 se_req = get_note_attachmentRequest()
74 se_req._session = sessionid
75 se_req._id = module_id
76 se_req._module_name = module_name
77 se_resp = portType.get_note_attachment(se_req)
78 file = se_resp._return._note_attachment.File
79 filename = se_resp._return._note_attachment.Filename
82 def user_get_attendee_list(portType, sessionid, module_name=None, module_id=None):
83 se_req = get_attendee_listRequest()
84 se_req._session = sessionid
85 se_req._module_name = module_name
86 se_req._id = module_id
87 se_resp = portType.get_attendee_list(se_req)
88 list = se_resp.get_element_return()
89 arch = base64.decodestring(list.Result)
90 eview = etree.XML(arch)
94 for ch in child.getchildren():
95 attendee_dict[ch.tag] = tools.ustr(ch.text)
96 attendee_list.append(attendee_dict)
99 def get_contact_by_email(portType, username, password, email_address=None):
100 se_req = contact_by_emailRequest()
101 se_req._user_name = username
102 se_req._password = password
103 se_req._email_address = email_address
105 se_resp = portType.contact_by_email(se_req)
107 for list in se_resp.get_element_return():
108 if list.Email_address in email_list:
110 elif list.Email_address:
111 email_list.append(list.Email_address)
114 logging.getLogger('sugarcrm_soap').error('Exception: %s\n' % (tools.ustr(e)))
117 def get_document_revision_search(portType, sessionid, module_id=None):
118 se_req = get_document_revisionRequest()
119 se_req._session = sessionid
120 se_req._i = module_id
121 se_resp = portType.get_document_list(se_req)
122 file = se_resp._return.Document_revision.File
123 filename = se_resp._return.Document_revision.Filename
124 return file, filename
127 def email_search(portType, sessionid, module_name, module_id, select_fields=None):
128 se_req = get_entryRequest()
129 se_req._session = sessionid
130 se_req._module_name = module_name
131 se_req._id = module_id
132 se_req._select_fields = select_fields
134 se_resp = portType.get_entry(se_req)
137 list = se_resp._return._entry_list
141 for j in i._name_value_list:
142 ans_dir[tools.ustr(j._name)] = tools.ustr(j._value)
144 ans_list.append(ans_dir)
148 def search(portType, sessionid, module_name, offset, max_results, query=None, order_by=None, select_fields=None, deleted=None):
149 se_req = get_entry_listRequest()
150 se_req._session = sessionid
151 se_req._module_name = module_name
153 se_req._query = query
154 se_req._order_by = order_by
155 se_req._offset = offset
156 se_req._select_fields = select_fields
157 se_req._max_results = max_results
158 se_req._deleted = deleted
159 se_resp = portType.get_entry_list(se_req)
162 list = se_resp._return._entry_list
165 for j in i._name_value_list:
166 ans_dir[tools.ustr(j._name)] = import_sugarcrm.unescape_htmlentities(tools.ustr(j._value))
168 ans_list.append(ans_dir)