2012-03-02 10:35:05 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2012 OpenERP SA (<http://www.openerp.com>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2012-02-24 10:52:15 +00:00
|
|
|
from osv import osv, fields
|
2012-03-02 10:35:05 +00:00
|
|
|
try:
|
|
|
|
import gdata.docs.data
|
|
|
|
import gdata.docs.client
|
|
|
|
from gdata.client import RequestError
|
|
|
|
from gdata.docs.service import DOCUMENT_LABEL
|
2012-03-20 16:03:20 +00:00
|
|
|
import gdata.auth
|
2012-03-02 10:35:05 +00:00
|
|
|
except ImportError:
|
|
|
|
raise osv.except_osv(_('Google Docs Error!'), _('Please install gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list'))
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-02-29 18:45:20 +00:00
|
|
|
class google_docs_ir_attachment(osv.osv):
|
2012-02-29 12:29:40 +00:00
|
|
|
_inherit = 'ir.attachment'
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-03-19 10:31:32 +00:00
|
|
|
def _auth(self, cr, uid,context=None):
|
2012-03-28 08:29:48 +00:00
|
|
|
'''
|
|
|
|
Connexion with google base account
|
|
|
|
@return client object for connexion
|
|
|
|
'''
|
|
|
|
#pool the google.login in google_base_account
|
|
|
|
google_pool = self.pool.get('google.login')
|
|
|
|
|
|
|
|
#get gmail password and login
|
|
|
|
user_config = google_pool.default_get( cr, uid, {'user' : '' , 'password' : ''},context=context)
|
|
|
|
#login gmail account
|
|
|
|
client = google_pool.google_login( user_config['user'], user_config['password'], type='docs_client', context=context)
|
2012-03-28 09:47:18 +00:00
|
|
|
if not client:
|
|
|
|
raise osv.except_osv(('Google Docs Error!'),("Check your google configuration in users/synchronization") )
|
2012-02-29 18:45:20 +00:00
|
|
|
return client
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-03-30 14:51:49 +00:00
|
|
|
def create_empty_google_doc(self, cr, uid, name_gdocs, model, ids, type_doc,context=None):
|
2012-02-24 10:52:15 +00:00
|
|
|
'''Associate a copy of the gdoc identified by 'gdocs_res_id' to the current entity.
|
|
|
|
@param cr: the current row from the database cursor.
|
|
|
|
@param uid: the current user ID, for security checks.
|
|
|
|
@param model: the current model name.
|
2012-03-01 15:25:30 +00:00
|
|
|
@param type_doc: text, spreadsheet or slide.
|
2012-03-01 14:45:07 +00:00
|
|
|
@return the document object.
|
2012-03-02 10:35:05 +00:00
|
|
|
@return False if the google_base_account hasn't been configured yet.
|
2012-02-24 14:01:19 +00:00
|
|
|
'''
|
2012-03-28 08:29:48 +00:00
|
|
|
#login with the base account google module
|
|
|
|
client = self._auth(cr, uid,context=context)
|
2012-03-01 14:45:07 +00:00
|
|
|
# create the document in google docs
|
2012-03-28 08:29:48 +00:00
|
|
|
local_resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL)
|
|
|
|
#create a new doc in Google Docs
|
|
|
|
gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/')
|
2012-03-01 14:45:07 +00:00
|
|
|
# register into the db
|
|
|
|
self.create(cr, uid, {
|
2012-03-02 08:30:13 +00:00
|
|
|
'res_model': model,
|
2012-03-01 15:25:30 +00:00
|
|
|
'res_id': ids[0],
|
2012-03-01 14:45:07 +00:00
|
|
|
'type': 'url',
|
2012-03-30 14:51:49 +00:00
|
|
|
'name': 'Google Doc',
|
2012-03-28 08:29:48 +00:00
|
|
|
'url': gdocs_resource.get_alternate_link().href,
|
2012-03-19 10:31:32 +00:00
|
|
|
},context=context)
|
|
|
|
|
|
|
|
|
2012-03-23 10:29:04 +00:00
|
|
|
return 1
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-03-30 14:51:49 +00:00
|
|
|
def copy_gdoc(self, cr, uid, name_gdocs, model, google_res_id, ids,context=None):
|
2012-03-28 11:41:13 +00:00
|
|
|
'''
|
|
|
|
copy an existing document in google docs
|
|
|
|
'''
|
2012-03-28 08:29:48 +00:00
|
|
|
client = self._auth(cr, uid)
|
2012-02-29 18:45:20 +00:00
|
|
|
# fetch and copy the original document
|
2012-03-28 11:41:13 +00:00
|
|
|
try:
|
|
|
|
original_resource = client.get_resource_by_id(google_res_id)
|
|
|
|
#copy the document you choose in the configuration
|
|
|
|
copy_resource = client.copy_resource(original_resource,'copy_%s' % original_resource.title.text)
|
|
|
|
except:
|
|
|
|
raise osv.except_osv(('Google Docs Error!'),("Your ressource id is not correct. You can find the id in the google docs URL") )
|
2012-03-01 14:45:07 +00:00
|
|
|
# register into the db
|
|
|
|
self.create(cr, uid, {
|
2012-03-02 08:30:13 +00:00
|
|
|
'res_model': model,
|
|
|
|
'res_id': ids[0],
|
2012-03-01 14:45:07 +00:00
|
|
|
'type': 'url',
|
2012-03-30 14:51:49 +00:00
|
|
|
'name': name_gdocs,
|
2012-03-01 14:45:07 +00:00
|
|
|
'url': copy_resource.get_alternate_link().href
|
2012-03-19 10:31:32 +00:00
|
|
|
},context=context)
|
2012-03-01 14:45:07 +00:00
|
|
|
|
|
|
|
return copy_resource
|
2012-02-29 18:45:20 +00:00
|
|
|
|
2012-03-05 16:11:15 +00:00
|
|
|
class google_docs(osv.osv):
|
|
|
|
_name = 'google.docs'
|
|
|
|
|
2012-03-28 08:29:48 +00:00
|
|
|
def doc_get(self, cr, uid, model, id, type_doc,context=None):
|
2012-03-05 16:11:15 +00:00
|
|
|
ir_attachment_ref = self.pool.get('ir.attachment')
|
2012-03-30 14:51:49 +00:00
|
|
|
pool_gdoc_config = self.pool.get('google.docs.config')
|
|
|
|
google_docs_config = pool_gdoc_config.search(cr, uid, [('model_id', '=', model)])
|
|
|
|
name_gdocs=''
|
2012-04-02 13:18:17 +00:00
|
|
|
model_fields_dic = self.pool.get(model).read(cr,uid,id,[])
|
|
|
|
|
2012-03-30 14:51:49 +00:00
|
|
|
if google_docs_config:
|
|
|
|
name_gdocs = pool_gdoc_config.browse(cr,uid,google_docs_config,context=context)[0].name_template
|
2012-04-02 13:18:17 +00:00
|
|
|
name_gdocs = name_gdocs % model_fields_dic[0]
|
2012-03-30 14:51:49 +00:00
|
|
|
|
2012-03-28 08:29:48 +00:00
|
|
|
# check if a model is configurate with a template
|
2012-03-19 10:31:32 +00:00
|
|
|
if google_docs_config:
|
2012-03-28 08:29:48 +00:00
|
|
|
for google_config in self.pool.get('google.docs.config').browse(cr,uid,google_docs_config,context=context):
|
2012-03-30 14:51:49 +00:00
|
|
|
google_res_id = google_config.gdocs_resource_id
|
|
|
|
google_document = ir_attachment_ref.copy_gdoc(cr, uid,name_gdocs ,model,google_res_id, id)
|
2012-03-19 10:31:32 +00:00
|
|
|
else:
|
2012-03-30 14:51:49 +00:00
|
|
|
google_document = ir_attachment_ref.create_empty_google_doc(cr, uid,name_gdocs, model, id, type_doc)
|
2012-03-19 10:31:32 +00:00
|
|
|
return -1
|
2012-03-05 16:11:15 +00:00
|
|
|
|
|
|
|
|
2012-03-12 14:41:31 +00:00
|
|
|
class config(osv.osv):
|
|
|
|
_name = 'google.docs.config'
|
|
|
|
_description = "Google Docs templates config"
|
2012-02-29 18:45:20 +00:00
|
|
|
|
|
|
|
_columns = {
|
2012-03-30 14:51:49 +00:00
|
|
|
'model_id': fields.many2one('ir.model', 'Model'),
|
|
|
|
'gdocs_resource_id': fields.char('Google resource ID', size=64,help='This is the id of the template document you kind find it in the URL'),
|
|
|
|
'name_template': fields.char('GDoc name template ', size=64, help='This is the name which appears on google side'),
|
2012-02-29 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_defaults = {
|
2012-04-02 13:18:17 +00:00
|
|
|
'name_template': 'pr_%(name)s',
|
2012-02-29 18:45:20 +00:00
|
|
|
}
|
2012-04-02 13:18:17 +00:00
|
|
|
def get_config(self, cr, uid, ids, model,context=None):
|
2012-03-28 11:41:13 +00:00
|
|
|
'''
|
|
|
|
Method use with the js to hidde or show the add google doc button
|
|
|
|
@return : list of configuration ids or false
|
|
|
|
'''
|
2012-04-03 07:07:52 +00:00
|
|
|
if self.pool.get('ir.attachment').search_count(cr,uid,[('url','like','https://docs.google.com/%'),('res_model','=',model),('res_id','=',ids[0])]) !=0:
|
2012-04-02 13:18:17 +00:00
|
|
|
return False
|
2012-03-16 09:00:54 +00:00
|
|
|
else:
|
2012-04-02 13:18:17 +00:00
|
|
|
return True
|
2012-03-28 08:29:48 +00:00
|
|
|
config()
|
2012-03-23 10:29:04 +00:00
|
|
|
|
|
|
|
|