[IMP]Putting a domain of filter to see only filters of the choosen model.
[odoo/odoo.git] / addons / google_docs / google_docs.py
1 ##############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2012 OpenERP SA (<http://www.openerp.com>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ##############################################################################
20 import logging
21 from datetime import datetime
22
23 from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
24 from openerp.osv import fields, osv
25 from openerp.tools.translate import _
26
27 _logger = logging.getLogger(__name__)
28
29 try:
30     import gdata.docs.data
31     import gdata.docs.client
32
33     # API breakage madness in the gdata API - those guys are insane. 
34     try:
35         # gdata 2.0.15+
36         gdata.docs.client.DocsClient.copy_resource
37     except AttributeError:
38         # gdata 2.0.14- : copy_resource() was copy()
39         gdata.docs.client.DocsClient.copy_resource = gdata.docs.client.DocsClient.copy
40
41     try:
42         # gdata 2.0.16+
43         gdata.docs.client.DocsClient.get_resource_by_id
44     except AttributeError:
45         try:
46             # gdata 2.0.15+
47             gdata.docs.client.DocsClient.get_resource_by_self_link
48             def get_resource_by_id_2_0_16(self, resource_id, **kwargs):
49                 return self.GetResourceBySelfLink(
50                     gdata.docs.client.RESOURCE_FEED_URI + ('/%s' % resource_id), **kwargs)
51             gdata.docs.client.DocsClient.get_resource_by_id = get_resource_by_id_2_0_16
52         except AttributeError:
53             # gdata 2.0.14- : alias get_resource_by_id()
54             gdata.docs.client.DocsClient.get_resource_by_id = gdata.docs.client.DocsClient.get_doc
55
56     try:
57         import atom.http_interface
58         _logger.info('GData lib version `%s` detected' % atom.http_interface.USER_AGENT)
59     except (ImportError, AttributeError):
60         _logger.debug('GData lib version could not be detected', exc_info=True)
61
62 except ImportError:
63     _logger.warning("Please install latest gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list")
64
65
66 class google_docs_ir_attachment(osv.osv):
67     _inherit = 'ir.attachment'
68
69     def _auth(self, cr, uid, context=None):
70         '''
71         Connexion with google base account
72         @return client object for connexion
73         '''
74         #pool the google.login in google_base_account
75         google_pool = self.pool.get('google.login')
76         #get gmail password and login. We use default_get() instead of a create() followed by a read() on the 
77         # google.login object, because it is easier. The keys 'user' and 'password' ahve to be passed in the dict
78         # but the values will be replaced by the user gmail password and login.
79         user_config = google_pool.default_get(cr, uid, {'user' : '' , 'password' : ''}, context=context)
80         #login gmail account
81         client = google_pool.google_login(user_config['user'], user_config['password'], type='docs_client', context=context)
82         if not client:
83             raise osv.except_osv(_('Google Docs Error!'), _("Check your google configuration in Users/Users/Synchronization tab."))
84         _logger.info('Logged into google docs as %s', user_config['user'])
85         return client
86
87     def create_empty_google_doc(self, cr, uid, res_model, res_id, context=None):
88         '''Create a new google document, empty and with a default type (txt)
89            :param res_model: the object for which the google doc is created
90            :param res_id: the Id of the object for which the google doc is created
91            :return: the ID of the google document object created
92         '''
93         #login with the base account google module
94         client = self._auth(cr, uid, context=context)
95         # create the document in google docs
96         title = "%s %s" % (context.get("name","Untitled Document."), datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT))
97         local_resource = gdata.docs.data.Resource(gdata.docs.data.SPREADSHEET_LABEL,title=title)
98         #create a new doc in Google Docs 
99         gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/')
100         # create an ir.attachment into the db
101         self.create(cr, uid, {
102             'res_model': res_model,
103             'res_id': res_id,
104             'type': 'url',
105             'name': title,
106             'url': gdocs_resource.get_alternate_link().href,
107         }, context=context)
108         return {'resource_id': gdocs_resource.resource_id.text,
109                 'title': title,
110                 'url': gdocs_resource.get_alternate_link().href}
111
112     def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None):
113         '''
114         copy an existing document in google docs
115            :param res_model: the object for which the google doc is created
116            :param res_id: the Id of the object for which the google doc is created
117            :param name_gdocs: the name of the future ir.attachment that will be created. Based on the google doc template foun.
118            :param gdoc_template_id: the id of the google doc document to copy
119            :return: the ID of the google document object created
120         '''
121         #login with the base account google module
122         client = self._auth(cr, uid)
123         # fetch and copy the original document
124         try:
125             doc = client.get_resource_by_id(gdoc_template_id)
126             #copy the document you choose in the configuration
127             copy_resource = client.copy_resource(doc, name_gdocs)
128         except:
129             raise osv.except_osv(_('Google Drive Error!'), _("Your resource id is not correct. You can find the id in the google docs URL."))
130         # create an ir.attachment
131         self.create(cr, uid, {
132             'res_model': res_model,
133             'res_id': res_id,
134             'type': 'url',
135             'name': name_gdocs,
136             'url': copy_resource.get_alternate_link().href
137         }, context=context)
138         return copy_resource.resource_id.text
139
140     def google_doc_get(self, cr, uid, res_model, ids, context=None):
141         '''
142         Function called by the js, when no google doc are yet associated with a record, with the aim to create one. It
143         will first seek for a google.docs.config associated with the model `res_model` to find out what's the template
144         of google doc to copy (this is usefull if you want to start with a non-empty document, a type or a name 
145         different than the default values). If no config is associated with the `res_model`, then a blank text document
146         with a default name is created.
147           :param res_model: the object for which the google doc is created
148           :param ids: the list of ids of the objects for which the google doc is created. This list is supposed to have
149             a length of 1 element only (batch processing is not supported in the code, though nothing really prevent it)
150           :return: the google document object created
151         '''
152         if len(ids) != 1:
153             raise osv.except_osv(_('Google Drive Error!'), _("Creating google drive may only be done by one at a time."))
154         res_id = ids[0]
155         pool_ir_attachment = self.pool.get('ir.attachment')
156         pool_gdoc_config = self.pool.get('google.docs.config')
157         name_gdocs = ''
158         model_fields_dic = self.pool.get(res_model).read(cr, uid, res_id, [], context=context)
159
160         # check if a model is configured with a template
161         google_docs_config = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model)], context=context)
162         if google_docs_config:
163             name_gdocs = pool_gdoc_config.browse(cr, uid, google_docs_config, context=context)[0].name_template
164             try:
165                 name_gdocs = name_gdocs % model_fields_dic
166             except:
167                 raise osv.except_osv(_('Key Error!'), _("Your Google Drive Name Pattern's key does not found in object."))
168             google_template_id = pool_gdoc_config.browse(cr, uid, google_docs_config[0], context=context).gdocs_resource_id
169             google_document = pool_ir_attachment.copy_gdoc(cr, uid, res_model, res_id, name_gdocs, google_template_id, context=context)
170         else:
171             google_document = pool_ir_attachment.create_empty_google_doc(cr, uid, res_model, res_id, context=context)
172         return google_document
173
174 class config(osv.osv):
175     _name = 'google.docs.config'
176     _description = "Google Drive templates config"
177
178     _columns = {
179         'model_id': fields.many2one('ir.model', 'Model', required=True),
180         'filter_id' : fields.many2one('ir.filters', 'Filter'),
181         'template_url': fields.char('Template Url', required=True, size=1024),
182         'name_template': fields.char('Google Drive Name Pattern', size=64, help='Choose how the new google drive will be named, on google side. Eg. gdoc_%(field_name)s', required=True),
183     }
184
185     def onchange_model_id(self, cr, uid, ids, model_id):
186         res = {'domain':{'filter_id':[]}}
187         if model_id:
188             model_name = self.pool.get('ir.model').read(cr, uid, model_id, ['model'])
189             if model_name:
190                 mod_name = model_name['model']
191                 res['domain'] = {'filter_id': [('model_id', '=', mod_name)]}
192         else:
193             res['value'] = {'filter_id': False}
194         return res
195
196     _defaults = {
197         'name_template': 'gdoc_%(name)s',
198     }
199 config()