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
|
|
|
|
import webbrowser
|
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-02-29 18:45:20 +00:00
|
|
|
# check google_base_account
|
|
|
|
users_obj = self.pool.get('res.users')
|
|
|
|
user = users_obj.browse(cr, uid, [uid])[0]
|
2012-03-19 10:31:32 +00:00
|
|
|
if not user.gmail_user or not user.gmail_password:
|
2012-03-02 10:35:05 +00:00
|
|
|
return False
|
2012-02-27 13:54:25 +00:00
|
|
|
|
2012-02-29 18:45:20 +00:00
|
|
|
# login
|
|
|
|
client = gdata.docs.client.DocsClient(source='openerp.com')
|
|
|
|
client.ssl = True
|
|
|
|
client.http_client.debug = False
|
2012-03-19 10:31:32 +00:00
|
|
|
client.ClientLogin(user.gmail_user, user.gmail_password, client.source, service='writely') #authentification in a gmail account
|
2012-02-29 18:45:20 +00:00
|
|
|
|
|
|
|
return client
|
2012-02-24 10:52:15 +00:00
|
|
|
|
2012-03-19 10:31:32 +00:00
|
|
|
def create_empty_google_doc(self, cr, uid, 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-01 14:45:07 +00:00
|
|
|
# authenticate
|
2012-03-20 16:03:20 +00:00
|
|
|
|
|
|
|
'''
|
2012-03-01 14:45:07 +00:00
|
|
|
client = self._auth(cr, uid)
|
2012-03-02 10:35:05 +00:00
|
|
|
if client == False:
|
2012-03-20 16:03:20 +00:00
|
|
|
return False
|
|
|
|
'''
|
2012-03-23 10:29:04 +00:00
|
|
|
client = self.pool.get('google.oauth').login(cr,uid,ids)
|
|
|
|
|
2012-03-20 16:03:20 +00:00
|
|
|
|
2012-03-01 14:45:07 +00:00
|
|
|
# create the document in google docs
|
2012-03-01 15:25:30 +00:00
|
|
|
if type_doc=='slide':
|
2012-03-01 14:45:07 +00:00
|
|
|
local_resource = gdata.docs.data.Resource(gdata.docs.data.PRESENTATION_LABEL)
|
2012-03-01 15:25:30 +00:00
|
|
|
elif type_doc=='spreadsheet':
|
2012-03-01 14:45:07 +00:00
|
|
|
local_resource = gdata.docs.data.Resource(gdata.docs.data.SPREADSHEET_LABEL)
|
|
|
|
else:
|
|
|
|
local_resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL)
|
2012-03-20 16:03:20 +00:00
|
|
|
|
|
|
|
#create a new doc in Google Docs
|
2012-03-23 10:29:04 +00:00
|
|
|
#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-02 08:40:06 +00:00
|
|
|
'name': 'new_foo %s' % (type_doc,) , # TODO pending from the working config
|
2012-03-23 10:29:04 +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-19 10:31:32 +00:00
|
|
|
def copy_gdoc(self, cr, uid, model, ids,context=None):
|
2012-03-23 10:29:04 +00:00
|
|
|
#client = self._auth(cr, uid)
|
|
|
|
#with oauth already connect check for the correct token
|
|
|
|
#if client == False:
|
|
|
|
# return False
|
2012-02-29 18:45:20 +00:00
|
|
|
# fetch and copy the original document
|
|
|
|
original_resource = client.get_resource_by_id(gdocs_resource_id)
|
2012-03-01 14:45:07 +00:00
|
|
|
copy_resource = client.copy_resource(entry=original_resource)
|
2012-03-19 10:31:32 +00:00
|
|
|
|
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-19 10:31:32 +00:00
|
|
|
'name': 'file_name',
|
|
|
|
'name': 'copy_foo %s' (type_doc,) , #TODO pending from the working config
|
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-16 09:00:54 +00:00
|
|
|
def doc_get(self, cr, uid, model, id, type_doc):
|
2012-03-05 16:11:15 +00:00
|
|
|
ir_attachment_ref = self.pool.get('ir.attachment')
|
2012-03-19 10:31:32 +00:00
|
|
|
google_docs_config = self.pool.get('google.docs.config').search(cr, uid, [('context_model_id', '=', model)])
|
2012-03-05 16:11:15 +00:00
|
|
|
|
2012-03-19 10:31:32 +00:00
|
|
|
if google_docs_config:
|
2012-03-05 16:11:15 +00:00
|
|
|
google_document = ir_attachment_ref.copy_gdoc(cr, uid, model, id)
|
2012-03-19 10:31:32 +00:00
|
|
|
else:
|
|
|
|
google_document = ir_attachment_ref.create_empty_google_doc(cr, uid, model, id, type_doc)
|
|
|
|
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-06 12:58:58 +00:00
|
|
|
'context_model_id': fields.many2one('ir.model', 'Model'),
|
2012-03-20 16:03:20 +00:00
|
|
|
'context_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'),
|
2012-03-06 12:58:58 +00:00
|
|
|
'context_name_template': fields.char('GDoc name template ', size=64, help='This is the name which appears on google side'),
|
2012-03-12 14:41:31 +00:00
|
|
|
'context_name': fields.char('Name', size=64, help='This is the attachment\'s name. As well, it appears on the panel.'),
|
|
|
|
'context_multiple': fields.boolean('Multiple documents')
|
2012-02-29 18:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_defaults = {
|
2012-03-12 14:41:31 +00:00
|
|
|
'context_name_template': 'Google Document',
|
|
|
|
'context_name': 'pr_%(name)',
|
|
|
|
'context_multiple': False,
|
2012-02-29 18:45:20 +00:00
|
|
|
}
|
2012-03-16 09:00:54 +00:00
|
|
|
def get_config(self, cr, uid, model):
|
2012-03-14 15:29:29 +00:00
|
|
|
domain = [('context_model_id', '=', model)]
|
2012-03-16 09:00:54 +00:00
|
|
|
if self.search_count(cr, uid, domain) != 0:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return self.search(cr, uid, domain)
|
2012-03-12 14:41:31 +00:00
|
|
|
config()
|
2012-03-20 16:03:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class oauth (osv.osv):
|
|
|
|
_name = 'google.oauth'
|
2012-03-23 10:29:04 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
def open_url(self,uid,ids,url,context=None):
|
|
|
|
return {'type' : 'ir.actions.act_url',
|
|
|
|
'url' : url,
|
|
|
|
'target': 'new',
|
|
|
|
}
|
|
|
|
'''
|
2012-03-20 16:03:20 +00:00
|
|
|
def login(self,cr,uid,ids,context=None):
|
2012-03-23 10:29:04 +00:00
|
|
|
|
|
|
|
# subscribe the google API
|
|
|
|
CONSUMER_KEY = '751376579939.apps.googleusercontent.com'
|
|
|
|
CONSUMER_SECRET = '_KGpgyO8DZIseyG3N-j-h8gN'
|
|
|
|
|
|
|
|
|
|
|
|
local_resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL)
|
|
|
|
SCOPES = ['https://docs.google.com/feeds/'] #select the google service
|
|
|
|
|
|
|
|
client = gdata.docs.client.DocsClient(source='openerp.com')
|
|
|
|
|
|
|
|
#the callback url
|
|
|
|
oauth_callback_url = 'http://127.0.0.1:8069/'#TODO give a correct dynamic url
|
|
|
|
|
|
|
|
#create a temporary token need to create a google authorization
|
|
|
|
request_token = client.GetOAuthToken(SCOPES, oauth_callback_url, CONSUMER_KEY, CONSUMER_SECRET)
|
|
|
|
#create an autorization google link
|
|
|
|
auth_url = request_token.generate_authorization_url()
|
|
|
|
#openthe link in your browser
|
|
|
|
webbrowser.open(str(auth_url))
|
|
|
|
|
|
|
|
#when you accept the autorization you are linked in your callback url
|
|
|
|
#you need to catch this url for the moment I copy past it in a raw_input
|
|
|
|
url_after=raw_input()
|
|
|
|
|
|
|
|
#create an autorization token
|
|
|
|
request_token_authorized = gdata.gauth.AuthorizeRequestToken(request_token, url_after)
|
|
|
|
#upgrade the token
|
|
|
|
access_token = client.GetAccessToken(request_token_authorized)
|
|
|
|
|
|
|
|
|
|
|
|
#when your access token you can connect your google services
|
|
|
|
client.auth_token = gdata.gauth.OAuthHmacToken(CONSUMER_KEY,
|
|
|
|
CONSUMER_SECRET,
|
|
|
|
access_token.token,
|
|
|
|
access_token.token_secret,
|
|
|
|
gdata.gauth.ACCESS_TOKEN)
|
|
|
|
|
|
|
|
return client
|