[ADD]: Add Attendee in Meeting For sugarcrm
[odoo/odoo.git] / addons / import_sugarcrm / 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 osv
27 from tools.translate import _
28 import base64
29 from lxml import etree
30 class LoginError(Exception): pass
31
32 def login(username, password, url):
33     loc = sugarsoapLocator()
34
35     portType = loc.getsugarsoapPortType(url)
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 relation_search(portType, sessionid, module_name=None, module_id=None, related_module=None, query=None, deleted=None):
52   se_req = get_relationshipsRequest()
53   se_req._session = sessionid
54   se_req._module_name = module_name
55   se_req._module_id = module_id
56   se_req._related_module =  related_module 
57   se_resp = portType.get_relationships(se_req)
58   ans_list = []
59   if se_resp:
60       list = se_resp._return.get_element_ids()
61       for i in list:
62           ans_list.append(i.get_element_id())
63   return ans_list
64
65 def user_get_attendee_list(portType, sessionid, module_name=None, module_id=None):
66   se_req = get_attendee_listRequest()
67   se_req._session = sessionid
68   se_req._module_name = module_name
69   se_req._id = module_id
70   se_resp = portType.get_attendee_list(se_req)
71   list = se_resp.get_element_return()
72   arch = base64.decodestring(list.Result)
73   eview = False
74   attende_id = False
75   attende_email_id = False
76   eview = etree.XML(arch)
77   for child in eview:
78       for ch in child.getchildren():
79            if ch.tag == 'id':
80                attende_id = ch.text
81            if ch.tag == 'email1':
82                 attende_email_id = ch.text   
83   return attende_id, attende_email_id            
84           
85
86 def search(portType, sessionid, module_name=None):
87   se_req = get_entry_listRequest()
88   se_req._session = sessionid
89   se_req._module_name = module_name
90   se_resp = portType.get_entry_list(se_req)
91   ans_list = []
92   if se_resp:
93       list = se_resp._return._entry_list
94       for i in list:
95           ans_dir = {}
96           for j in i._name_value_list:
97               ans_dir[j._name.encode('utf-8')] = j._value.encode('utf-8')
98             #end for
99           ans_list.append(ans_dir)
100     #end for
101   return ans_list
102