[IMP] add gdata docs library in google base account remove oauth authentification and replace with google_login method in google_base_account remove unuse button in sidebar and correct the copy_resource method

bzr revid: mva@openerp.com-20120328082948-7my72vqn0gcbsf2l
This commit is contained in:
MVA 2012-03-28 10:29:48 +02:00
parent dda93ab1ec
commit 3590c952ec
5 changed files with 57 additions and 110 deletions

View File

@ -43,6 +43,8 @@ class google_login(osv.osv_memory):
gd_client = gdata.contacts.service.ContactsService() gd_client = gdata.contacts.service.ContactsService()
if type == 'calendar': if type == 'calendar':
gd_client = gdata.calendar.service.CalendarService() gd_client = gdata.calendar.service.CalendarService()
if type =='docs_client':
gd_client = gdata.docs.client.DocsClient()
else: else:
gd_client = gdata.contacts.service.ContactsService() gd_client = gdata.contacts.service.ContactsService()
try: try:

View File

@ -1,3 +1,24 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# 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/>.
#
##############################################################################
{ {
'name': 'Google Docs integration', 'name': 'Google Docs integration',
'version': '0.2', 'version': '0.2',

View File

@ -25,7 +25,6 @@ try:
from gdata.client import RequestError from gdata.client import RequestError
from gdata.docs.service import DOCUMENT_LABEL from gdata.docs.service import DOCUMENT_LABEL
import gdata.auth import gdata.auth
import webbrowser
except ImportError: 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')) raise osv.except_osv(_('Google Docs Error!'), _('Please install gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list'))
@ -33,17 +32,17 @@ class google_docs_ir_attachment(osv.osv):
_inherit = 'ir.attachment' _inherit = 'ir.attachment'
def _auth(self, cr, uid,context=None): def _auth(self, cr, uid,context=None):
# check google_base_account '''
users_obj = self.pool.get('res.users') Connexion with google base account
user = users_obj.browse(cr, uid, [uid])[0] @return client object for connexion
if not user.gmail_user or not user.gmail_password: '''
return False #pool the google.login in google_base_account
google_pool = self.pool.get('google.login')
# login
client = gdata.docs.client.DocsClient(source='openerp.com') #get gmail password and login
client.ssl = True user_config = google_pool.default_get( cr, uid, {'user' : '' , 'password' : ''},context=context)
client.http_client.debug = False #login gmail account
client.ClientLogin(user.gmail_user, user.gmail_password, client.source, service='writely') #authentification in a gmail account client = google_pool.google_login( user_config['user'], user_config['password'], type='docs_client', context=context)
return client return client
@ -56,47 +55,31 @@ class google_docs_ir_attachment(osv.osv):
@return the document object. @return the document object.
@return False if the google_base_account hasn't been configured yet. @return False if the google_base_account hasn't been configured yet.
''' '''
# authenticate #login with the base account google module
client = self._auth(cr, uid,context=context)
'''
client = self._auth(cr, uid)
if client == False:
return False
'''
client = self.pool.get('google.oauth').login(cr,uid,ids)
# create the document in google docs # create the document in google docs
if type_doc=='slide': local_resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL)
local_resource = gdata.docs.data.Resource(gdata.docs.data.PRESENTATION_LABEL) #create a new doc in Google Docs
elif type_doc=='spreadsheet': gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/')
local_resource = gdata.docs.data.Resource(gdata.docs.data.SPREADSHEET_LABEL) name = gdocs_resource.title
else:
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/')
# register into the db # register into the db
self.create(cr, uid, { self.create(cr, uid, {
'res_model': model, 'res_model': model,
'res_id': ids[0], 'res_id': ids[0],
'type': 'url', 'type': 'url',
'name': 'new_foo %s' % (type_doc,) , # TODO pending from the working config 'name': name,
'url': ''#gdocs_resource.get_alternate_link().href 'url': gdocs_resource.get_alternate_link().href,
},context=context) },context=context)
return 1 return 1
def copy_gdoc(self, cr, uid, model, ids,context=None): def copy_gdoc(self, cr, uid, model, google_res_id, ids,context=None):
#client = self._auth(cr, uid) client = self._auth(cr, uid)
#with oauth already connect check for the correct token
#if client == False:
# return False
# fetch and copy the original document # fetch and copy the original document
original_resource = client.get_resource_by_id(gdocs_resource_id) original_resource = client.get_resource_by_id(google_res_id)
copy_resource = client.copy_resource(entry=original_resource) #copy the document you choose in the configuration
copy_resource = client.copy_resource(original_resource,'copy_foo')
# register into the db # register into the db
self.create(cr, uid, { self.create(cr, uid, {
@ -104,7 +87,7 @@ class google_docs_ir_attachment(osv.osv):
'res_id': ids[0], 'res_id': ids[0],
'type': 'url', 'type': 'url',
'name': 'file_name', 'name': 'file_name',
'name': 'copy_foo %s' (type_doc,) , #TODO pending from the working config 'name': 'copy_foo' , #TODO pending from the working config
'url': copy_resource.get_alternate_link().href 'url': copy_resource.get_alternate_link().href
},context=context) },context=context)
@ -113,12 +96,14 @@ class google_docs_ir_attachment(osv.osv):
class google_docs(osv.osv): class google_docs(osv.osv):
_name = 'google.docs' _name = 'google.docs'
def doc_get(self, cr, uid, model, id, type_doc): def doc_get(self, cr, uid, model, id, type_doc,context=None):
ir_attachment_ref = self.pool.get('ir.attachment') ir_attachment_ref = self.pool.get('ir.attachment')
google_docs_config = self.pool.get('google.docs.config').search(cr, uid, [('context_model_id', '=', model)]) google_docs_config = self.pool.get('google.docs.config').search(cr, uid, [('context_model_id', '=', model)])
# check if a model is configurate with a template
if google_docs_config: if google_docs_config:
google_document = ir_attachment_ref.copy_gdoc(cr, uid, model, id) for google_config in self.pool.get('google.docs.config').browse(cr,uid,google_docs_config,context=context):
google_res_id = google_config.context_gdocs_resource_id
google_document = ir_attachment_ref.copy_gdoc(cr, uid, model,google_res_id, id)
else: else:
google_document = ir_attachment_ref.create_empty_google_doc(cr, uid, model, id, type_doc) google_document = ir_attachment_ref.create_empty_google_doc(cr, uid, model, id, type_doc)
return -1 return -1
@ -133,13 +118,11 @@ class config(osv.osv):
'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'), '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'),
'context_name_template': fields.char('GDoc name template ', size=64, help='This is the name which appears on google side'), 'context_name_template': fields.char('GDoc name template ', size=64, help='This is the name which appears on google side'),
'context_name': fields.char('Name', size=64, help='This is the attachment\'s name. As well, it appears on the panel.'), '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')
} }
_defaults = { _defaults = {
'context_name_template': 'Google Document', 'context_name_template': 'Google Document',
'context_name': 'pr_%(name)', 'context_name': 'pr_%(name)',
'context_multiple': False,
} }
def get_config(self, cr, uid, model): def get_config(self, cr, uid, model):
domain = [('context_model_id', '=', model)] domain = [('context_model_id', '=', model)]
@ -147,57 +130,7 @@ class config(osv.osv):
return False return False
else: else:
return self.search(cr, uid, domain) return self.search(cr, uid, domain)
config() config()
class oauth (osv.osv):
_name = 'google.oauth'
'''
def open_url(self,uid,ids,url,context=None):
return {'type' : 'ir.actions.act_url',
'url' : url,
'target': 'new',
}
'''
def login(self,cr,uid,ids,context=None):
# 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

View File

@ -4,7 +4,7 @@
<!-- add google docs config field in user form --> <!-- add google docs config field in user form -->
<record model="ir.ui.view" id="view_google_docs_model_config_tree"> <record model="ir.ui.view" id="view_google_docs_config_tree">
<field name="name">google_docs.config.tree</field> <field name="name">google_docs.config.tree</field>
<field name="model">google.docs.config</field> <field name="model">google.docs.config</field>
<field name="type">tree</field> <field name="type">tree</field>
@ -15,7 +15,7 @@
</field> </field>
</record> </record>
<record model="ir.ui.view" id="view_google_docs_users_config_inherited_form"> <record model="ir.ui.view" id="view_google_docs_config_form">
<field name="name">google_docs.config.form</field> <field name="name">google_docs.config.form</field>
<field name="model">google.docs.config</field> <field name="model">google.docs.config</field>
<field name="type">form</field> <field name="type">form</field>
@ -34,7 +34,7 @@
<field name='res_model'>google.docs.config</field> <field name='res_model'>google.docs.config</field>
<field name='type'>ir.actions.act_window</field> <field name='type'>ir.actions.act_window</field>
<field name='view_type'>form</field> <field name='view_type'>form</field>
<field name='view_id' ref='view_google_docs_model_config_tree'/> <field name='view_id' ref='view_google_docs_config_form'/>
</record> </record>
<menuitem name='Google Docs configuration' id='menu_gdocs_config' parent='base.menu_administration'/> <menuitem name='Google Docs configuration' id='menu_gdocs_config' parent='base.menu_administration'/>
<menuitem name='Models configuration' id='menu_gdocs_model_config' parent='menu_gdocs_config' action='action_google_docs_users_config'/> <menuitem name='Models configuration' id='menu_gdocs_model_config' parent='menu_gdocs_config' action='action_google_docs_users_config'/>

View File

@ -5,17 +5,8 @@
<t t-extend="FormView.sidebar.attachments"> <t t-extend="FormView.sidebar.attachments">
<t t-jquery="div.oe-binary-file-set" t-operation="append"> <t t-jquery="div.oe-binary-file-set" t-operation="append">
<button class="oe_google_docs_text_button" type="button"> <button class="oe_google_docs_text_button" type="button">
<!--<img src="/web/static/src/img/throbber.gif" width="16" height="16"/>-->
<span>Add A Goole Docs</span> <span>Add A Goole Docs</span>
</button> </button>
<!--<button class="oe_google_docs_spreadsheet_button" type="button">
<img src="/web/static/src/img/throbber.gif" width="16" height="16" style="display: none"/>
<span>Add GDoc - spreadsheet</span>
</button>
<button class="oe_google_docs_slide_button" type="button">
<img src="/web/static/src/img/throbber.gif" width="16" height="16" style="display: none"/>
<span>Add GDoc - presentation</span>
</button>-->
</t> </t>
</t> </t>
</templates> </templates>