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 ##############################################################################
20 from datetime import datetime
21 from tools import DEFAULT_SERVER_DATETIME_FORMAT
22 from osv import osv, fields
23 from tools.translate import _
25 import gdata.docs.data
26 import gdata.docs.client
27 from gdata.client import RequestError
28 from gdata.docs.service import DOCUMENT_LABEL
30 from gdata.docs.data import Resource
33 _logger = logging.getLogger(__name__)
34 _logger.warning("Please install latest gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list")
36 class google_docs_ir_attachment(osv.osv):
37 _inherit = 'ir.attachment'
39 def _auth(self, cr, uid, context=None):
41 Connexion with google base account
42 @return client object for connexion
44 #pool the google.login in google_base_account
45 google_pool = self.pool.get('google.login')
46 #get gmail password and login. We use default_get() instead of a create() followed by a read() on the
47 # google.login object, because it is easier. The keys 'user' and 'password' ahve to be passed in the dict
48 # but the values will be replaced by the user gmail password and login.
49 user_config = google_pool.default_get( cr, uid, {'user' : '' , 'password' : ''}, context=context)
51 client = google_pool.google_login( user_config['user'], user_config['password'], type='docs_client', context=context)
53 raise osv.except_osv( _('Google Docs Error!'), _("Check your google configuration in Users/Users/Synchronization tab."))
56 def create_empty_google_doc(self, cr, uid, res_model, res_id, context=None):
57 '''Create a new google document, empty and with a default type (txt)
58 :param res_model: the object for which the google doc is created
59 :param res_id: the Id of the object for which the google doc is created
60 :return: the ID of the google document object created
62 #login with the base account google module
63 client = self._auth(cr, uid, context=context)
64 # create the document in google docs
65 title = "%s %s" % (context.get("name","Untitled Document."), datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT))
66 local_resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL,title=title)
67 #create a new doc in Google Docs
68 gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/')
69 # create an ir.attachment into the db
70 self.create(cr, uid, {
71 'res_model': res_model,
75 'url': gdocs_resource.get_alternate_link().href,
77 return {'resource_id': gdocs_resource.resource_id.text,
79 'url': gdocs_resource.get_alternate_link().href}
81 def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None):
83 copy an existing document in google docs
84 :param res_model: the object for which the google doc is created
85 :param res_id: the Id of the object for which the google doc is created
86 :param name_gdocs: the name of the future ir.attachment that will be created. Based on the google doc template foun.
87 :param gdoc_template_id: the id of the google doc document to copy
88 :return: the ID of the google document object created
90 #login with the base account google module
91 client = self._auth(cr, uid)
92 # fetch and copy the original document
94 original_resource = client.get_resource_by_id(gdoc_template_id)
95 #copy the document you choose in the configuration
96 copy_resource = client.copy_resource(original_resource, name_gdocs)
98 raise osv.except_osv(_('Google Docs Error!'), _("Your resource id is not correct. You can find the id in the google docs URL."))
99 # create an ir.attachment
100 self.create(cr, uid, {
101 'res_model': res_model,
105 'url': copy_resource.get_alternate_link().href
107 return copy_resource.resource_id.text
109 def google_doc_get(self, cr, uid, res_model, ids, context=None):
111 Function called by the js, when no google doc are yet associated with a record, with the aim to create one. It
112 will first seek for a google.docs.config associated with the model `res_model` to find out what's the template
113 of google doc to copy (this is usefull if you want to start with a non-empty document, a type or a name
114 different than the default values). If no config is associated with the `res_model`, then a blank text document
115 with a default name is created.
116 :param res_model: the object for which the google doc is created
117 :param ids: the list of ids of the objects for which the google doc is created. This list is supposed to have
118 a length of 1 element only (batch processing is not supported in the code, though nothing really prevent it)
119 :return: the google document object created
122 raise osv.except_osv(_('Google Docs Error!'), _("Creating google docs may only be done by one at a time."))
124 pool_ir_attachment = self.pool.get('ir.attachment')
125 pool_gdoc_config = self.pool.get('google.docs.config')
127 model_fields_dic = self.pool.get(res_model).read(cr, uid, res_id, [], context=context)
129 # check if a model is configured with a template
130 google_docs_config = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model)], context=context)
131 if google_docs_config:
132 name_gdocs = pool_gdoc_config.browse(cr, uid, google_docs_config, context=context)[0].name_template
134 name_gdocs = name_gdocs % model_fields_dic
136 raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object."))
137 google_template_id = pool_gdoc_config.browse(cr, uid, google_docs_config[0], context=context).gdocs_resource_id
138 google_document = pool_ir_attachment.copy_gdoc(cr, uid, res_model, res_id, name_gdocs, google_template_id, context=context)
140 google_document = pool_ir_attachment.create_empty_google_doc(cr, uid, res_model, res_id, context=context)
141 return google_document
143 class config(osv.osv):
144 _name = 'google.docs.config'
145 _description = "Google Docs templates config"
148 'model_id': fields.many2one('ir.model', 'Model', required=True),
149 'gdocs_resource_id': fields.char('Google Resource ID to Use as Template', size=64, help='''
150 This is the id of the template document, on google side. You can find it thanks to its URL:
151 *for a text document with url like `https://docs.google.com/a/openerp.com/document/d/123456789/edit`, the ID is `document:123456789`
152 *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`
153 *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`
154 *for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789`
157 'name_template': fields.char('Google Doc Name Pattern', size=64, help='Choose how the new google docs will be named, on google side. Eg. gdoc_%(field_name)s', required=True),
161 'name_template': 'gdoc_%(name)s',