1 ##############################################################################
3 # OpenERP, Open Source Management Solution
4 # Copyright (C) 2004-2012 OpenERP SA (<http://www.openerp.com>).
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.
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.
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/>.
19 ##############################################################################
21 from osv import osv, fields
22 from tools.translate import _
24 import gdata.docs.data
25 import gdata.docs.client
26 from gdata.client import RequestError
27 from gdata.docs.service import DOCUMENT_LABEL
30 raise osv.except_osv(_('Google Docs Error!'), _('Please install gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list'))
32 class google_docs_ir_attachment(osv.osv):
33 _inherit = 'ir.attachment'
35 def _auth(self, cr, uid, context=None):
37 Connexion with google base account
38 @return client object for connexion
40 #pool the google.login in google_base_account
41 google_pool = self.pool.get('google.login')
42 #get gmail password and login. We use default_get() instead of a create() followed by a read() on the
43 # google.login object, because it is easier. The keys 'user' and 'password' ahve to be passed in the dict
44 # but the values will be replaced by the user gmail password and login.
45 user_config = google_pool.default_get( cr, uid, {'user' : '' , 'password' : ''}, context=context)
47 client = google_pool.google_login( user_config['user'], user_config['password'], type='docs_client', context=context)
49 raise osv.except_osv( _('Google Docs Error!'), _("Check your google configuration in users/synchronization"))
52 def create_empty_google_doc(self, cr, uid, res_model, res_id, context=None):
53 '''Create a new google document, empty and with a default type (txt)
54 :param res_model: the object for which the google doc is created
55 :param res_id: the Id of the object for which the google doc is created
56 :return: the ID of the google document object created
58 #login with the base account google module
59 client = self._auth(cr, uid, context=context)
60 # create the document in google docs
61 local_resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL)
62 #create a new doc in Google Docs
63 gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/')
64 # create an ir.attachment into the db
65 self.create(cr, uid, {
66 'res_model': res_model,
69 'name': _('Google Doc'),
70 'url': gdocs_resource.get_alternate_link().href,
72 return gdocs_resource.resource_id.text
74 def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None):
76 copy an existing document in google docs
77 :param res_model: the object for which the google doc is created
78 :param res_id: the Id of the object for which the google doc is created
79 :param name_gdocs: the name of the future ir.attachment that will be created. Based on the google doc template foun.
80 :param gdoc_template_id: the id of the google doc document to copy
81 :return: the ID of the google document object created
83 #login with the base account google module
84 client = self._auth(cr, uid)
85 # fetch and copy the original document
87 original_resource = client.get_resource_by_id(gdoc_template_id)
88 #copy the document you choose in the configuration
89 copy_resource = client.copy_resource(original_resource, 'copy_%s' % original_resource.title.text)
91 raise osv.except_osv(_('Google Docs Error!'), _("Your resource id is not correct. You can find the id in the google docs URL"))
92 # create an ir.attachment
93 self.create(cr, uid, {
94 'res_model': res_model,
98 'url': copy_resource.get_alternate_link().href
100 return copy_resource.resource_id.text
102 def google_doc_get(self, cr, uid, res_model, ids, context=None):
104 Function called by the js, when no google doc are yet associated with a record, with the aim to create one. It
105 will first seek for a google.docs.config associated with the model `res_model` to find out what's the template
106 of google doc to copy (this is usefull if you want to start with a non-empty document, a type or a name
107 different than the default values). If no config is associated with the `res_model`, then a blank text document
108 with a default name is created.
109 :param res_model: the object for which the google doc is created
110 :param ids: the list of ids of the objects for which the google doc is created. This list is supposed to have
111 a length of 1 element only (batch processing is not supported in the code, though nothing really prevent it)
112 :return: the google document object created
114 assert len(ids) == 1, 'Creating google docs may only be done by one at a time'
116 pool_ir_attachment = self.pool.get('ir.attachment')
117 pool_gdoc_config = self.pool.get('google.docs.config')
119 model_fields_dic = self.pool.get(res_model).read(cr, uid, res_id, [], context=context)
121 # check if a model is configured with a template
122 google_docs_config = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model)], context=context)
123 if google_docs_config:
124 name_gdocs = pool_gdoc_config.browse(cr, uid, google_docs_config, context=context)[0].name_template
125 name_gdocs = name_gdocs % model_fields_dic
126 google_template_id = pool_gdoc_config.browse(cr, uid, google_docs_config[0], context=context).gdocs_resource_id
127 google_document = pool_ir_attachment.copy_gdoc(cr, uid, res_model, res_id, name_gdocs, google_template_id, context=context)
129 google_document = pool_ir_attachment.create_empty_google_doc(cr, uid, res_model, res_id, context=context)
130 return google_document
132 class config(osv.osv):
133 _name = 'google.docs.config'
134 _description = "Google Docs templates config"
137 'model_id': fields.many2one('ir.model', 'Model'),
138 'gdocs_resource_id': fields.char('Google resource ID', size=64,help='''
139 This is the id of the template document, on google side. You can find it thanks to its URL:
140 *for a text document with url like `https://docs.google.com/a/openerp.com/document/d/123456789/edit`, the ID is `document:123456789`
141 *for a spreadsheet document with url like `https://docs.google.com/a/openerp.com/spreadsheet/ccc?key=123456789#gid=0`, the ID is `spreadsheet:123456789`
142 *for a presentation (slide show) document with url like `https://docs.google.com/a/openerp.com/presentation/d/123456789/edit#slide=id.p`, the ID is `presentation:123456789`
143 *for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789`
146 'name_template': fields.char('GDoc name template ', size=64, help='This is the name which appears on google side'),
150 'name_template': 'gdoc_%(name)s',