[FIX]: Remove print statement and Fix Import user problem.
[odoo/odoo.git] / addons / sugarcrm_syncro / sugar.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22
23 import hashlib
24 from sugarsoap_services import *
25 from sugarsoap_services_types import *
26 from osv import fields, osv
27 from tools.translate import _
28 import sys;
29
30 class LoginError(Exception): pass
31
32 def login(username, password):
33     loc = sugarsoapLocator()
34
35     portType = loc.getsugarsoapPortType()
36     request = loginRequest()
37     uauth = ns0.user_auth_Def(request)
38     request._user_auth = uauth
39
40     uauth._user_name = username
41     uauth._password = hashlib.md5(password).hexdigest()
42     uauth._version = '1.1'
43     try:
44         response = portType.login(request)
45     except:
46         raise osv.except_osv(_('Error !'), _('Authentication error !\nBad Username or Password !'))
47     if -1 == response._return._id:
48         raise LoginError(response._return._error._description)
49     return (portType, response._return._id)
50
51 def search(portType, sessionid, module_name=None):
52   se_req = get_entry_listRequest()
53   se_req._session = sessionid
54   se_req._module_name = module_name
55   se_resp = portType.get_entry_list(se_req)
56   ans_list = []
57   if se_resp:
58       list = se_resp._return._entry_list
59       for i in list:
60           ans_dir = {}
61           for j in i._name_value_list:
62               ans_dir[j._name.encode('utf-8')] = j._value.encode('utf-8')
63             #end for
64           ans_list.append(ans_dir)
65     #end for
66   return ans_list
67