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
41 class LoginError(Exception): pass
43 def login(username, password, url):
45 loc = sugarsoapLocator()
47 portType = loc.getsugarsoapPortType(url, tracefile=sys.stdout)
49 portType = loc.getsugarsoapPortType(url)
50 request = loginRequest()
51 uauth = ns0.user_auth_Def(request)
52 request._user_auth = uauth
54 uauth._user_name = username
55 uauth._password = hashlib.md5(password).hexdigest()
56 uauth._version = '1.1'
58 response = portType.login(request)
60 raise osv.except_osv(_('Error !'), _('Authentication error !\nBad Username or Password bad SugarSoap Api url !'))
61 if -1 == response._return._id:
62 raise LoginError(response._return._error._description)
64 return (portType, response._return._id)
66 def relation_search(portType, sessionid, module_name=None, module_id=None, related_module=None, query=None, deleted=None):
67 se_req = get_relationshipsRequest()
68 se_req._session = sessionid
69 se_req._module_name = module_name
70 se_req._module_id = module_id
71 se_req._related_module = related_module
72 se_resp = portType.get_relationships(se_req)
75 list = se_resp._return.get_element_ids()
77 ans_list.append(i.get_element_id())
80 def attachment_search(portType, sessionid, module_name, module_id=None):
81 se_req = get_note_attachmentRequest()
82 se_req._session = sessionid
83 se_req._id = module_id
84 se_req._module_name = module_name
85 se_resp = portType.get_note_attachment(se_req)
86 file = se_resp._return._note_attachment.File
87 filename = se_resp._return._note_attachment.Filename
90 def user_get_attendee_list(portType, sessionid, module_name=None, module_id=None):
91 se_req = get_attendee_listRequest()
92 se_req._session = sessionid
93 se_req._module_name = module_name
94 se_req._id = module_id
95 se_resp = portType.get_attendee_list(se_req)
96 list = se_resp.get_element_return()
97 arch = base64.decodestring(list.Result)
98 eview = etree.XML(arch)
102 for ch in child.getchildren():
103 attendee_dict[ch.tag] = tools.ustr(ch.text)
104 attendee_list.append(attendee_dict)
107 def get_contact_by_email(portType, username, password, email_address=None):
108 se_req = contact_by_emailRequest()
109 se_req._user_name = username
110 se_req._password = password
111 se_req._email_address = email_address
113 se_resp = portType.contact_by_email(se_req)
115 for list in se_resp.get_element_return():
116 if list.Email_address in email_list:
118 elif list.Email_address:
119 email_list.append(list.Email_address)
122 logging.getLogger('sugarcrm_soap').error('Exception: %s\n' % (tools.ustr(e)))
125 def get_document_revision_search(portType, sessionid, module_id=None):
126 se_req = get_document_revisionRequest()
127 se_req._session = sessionid
128 se_req._i = module_id
129 se_resp = portType.get_document_list(se_req)
130 file = se_resp._return.Document_revision.File
131 filename = se_resp._return.Document_revision.Filename
132 return file, filename
135 def email_search(portType, sessionid, module_name, module_id, select_fields=None):
136 se_req = get_entryRequest()
137 se_req._session = sessionid
138 se_req._module_name = module_name
139 se_req._id = module_id
140 se_req._select_fields = select_fields
142 se_resp = portType.get_entry(se_req)
145 list = se_resp._return._entry_list
149 for j in i._name_value_list:
150 ans_dir[tools.ustr(j._name)] = tools.ustr(j._value)
152 ans_list.append(ans_dir)
156 def search(portType, sessionid, module_name, offset, max_results, query=None, order_by=None, select_fields=None, deleted=None):
157 se_req = get_entry_listRequest()
158 se_req._session = sessionid
159 se_req._module_name = module_name
161 se_req._query = query
162 se_req._order_by = order_by
163 se_req._offset = offset
164 se_req._select_fields = select_fields
165 se_req._max_results = max_results
166 se_req._deleted = deleted
167 se_resp = portType.get_entry_list(se_req)
170 list = se_resp._return._entry_list
173 for j in i._name_value_list:
174 ans_dir[tools.ustr(j._name)] = import_sugarcrm.unescape_htmlentities(tools.ustr(j._value))
176 ans_list.append(ans_dir)
180 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: