[imp] corrected integer handling in search view
[odoo/odoo.git] / addons / google_base_account / wizard / google_login.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 from osv import fields,osv
23 from tools.translate import _
24 try:
25     import gdata.contacts.service
26     import gdata.contacts.client
27     import gdata.calendar.service
28 except ImportError:
29     raise osv.except_osv(_('Google Contacts Import Error!'), _('Please install gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list'))
30
31 class google_login(osv.osv_memory):
32     _description ='Google Contact'
33     _name = 'google.login'
34     _columns = {
35         'user': fields.char('Google Username', size=64, required=True),
36         'password': fields.char('Google Password', size=64),
37     }
38
39     def google_login(self, user, password, type='', context=None):
40         if type == 'group': 
41             gd_client = gdata.contacts.client.ContactsClient(source='OpenERP')
42         if type == 'contact' :   
43             gd_client = gdata.contacts.service.ContactsService()
44         if type == 'calendar':
45             gd_client = gdata.calendar.service.CalendarService()
46         else:
47             gd_client = gdata.contacts.service.ContactsService()     
48         try:    
49             gd_client.ClientLogin(user, password,gd_client.source)
50         except Exception:
51             return False
52         return gd_client
53
54
55     def default_get(self, cr, uid, fields, context=None):
56         res = super(google_login, self).default_get(cr, uid, fields, context=context)
57         user_obj = self.pool.get('res.users').browse(cr, uid, uid)
58         if 'user' in fields:
59             res.update({'user': user_obj.gmail_user})
60         if 'password' in fields:
61             res.update({'password': user_obj.gmail_password})
62         return res
63     
64     def login(self, cr, uid, ids, context=None):
65         data = self.read(cr, uid, ids)[0]
66         user = data['user']
67         password = data['password']
68         if self.google_login(user, password):
69             res = {
70                    'gmail_user': user,
71                    'gmail_password': password
72             }
73             self.pool.get('res.users').write(cr, uid, uid, res, context=context)
74         else:
75             raise osv.except_osv(_('Error'), _("Authentication fail check the user and password !"))
76
77         return self._get_next_action(cr, uid, context=context)
78
79     def _get_next_action(self, cr, uid, context=None):
80         return {'type': 'ir.actions.act_window_close'}
81
82 google_login()
83
84 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: