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