diff --git a/addons/base_setup/res_config.py b/addons/base_setup/res_config.py index 33c5aa863bf..1e5169fc2b3 100644 --- a/addons/base_setup/res_config.py +++ b/addons/base_setup/res_config.py @@ -36,6 +36,8 @@ class base_config_settings(osv.osv_memory): help="""Enable the public part of openerp, openerp becomes a public website."""), 'module_auth_oauth': fields.boolean('Use external authentication providers, sign in with google, facebook, ...'), 'module_base_import': fields.boolean("Allow users to import data from CSV files"), + 'module_google_drive': fields.boolean('Attach a google document to any record', + help="""This installs the module google_docs."""), } def open_company(self, cr, uid, ids, context=None): diff --git a/addons/base_setup/res_config_view.xml b/addons/base_setup/res_config_view.xml index 8c48f5d7a6b..73685be95df 100644 --- a/addons/base_setup/res_config_view.xml +++ b/addons/base_setup/res_config_view.xml @@ -81,6 +81,15 @@ + + diff --git a/addons/crm/crm_lead_demo.xml b/addons/crm/crm_lead_demo.xml index e7e6beae33e..4c6cf47c4f7 100644 --- a/addons/crm/crm_lead_demo.xml +++ b/addons/crm/crm_lead_demo.xml @@ -709,5 +709,6 @@ Andrew eval="[ ref('msg_case18_1'), ref('msg_case18_2')], True, {}" /> + diff --git a/addons/google_base_account/__init__.py b/addons/google_base_account/__init__.py index e1a476431af..44a5aabe277 100644 --- a/addons/google_base_account/__init__.py +++ b/addons/google_base_account/__init__.py @@ -20,7 +20,6 @@ ############################################################################## import google_base_account -import wizard # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/google_base_account/__openerp__.py b/addons/google_base_account/__openerp__.py index b4d996f2807..99678d2785d 100644 --- a/addons/google_base_account/__openerp__.py +++ b/addons/google_base_account/__openerp__.py @@ -30,10 +30,9 @@ The module adds google user in res user. """, 'author': 'OpenERP SA', 'website': 'http://www.openerp.com', - 'depends': ['base'], + 'depends': ['base_setup'], 'data': [ - 'google_base_account_view.xml', - 'wizard/google_login_view.xml', + 'google_base_account_data.xml', ], 'demo': [], 'installable': True, diff --git a/addons/google_base_account/google_base_account.py b/addons/google_base_account/google_base_account.py index 06e5643344c..39ccc569767 100644 --- a/addons/google_base_account/google_base_account.py +++ b/addons/google_base_account/google_base_account.py @@ -19,14 +19,45 @@ # ############################################################################## -from openerp.osv import fields,osv - -class res_users(osv.osv): - _inherit = "res.users" - _columns = { - 'gmail_user': fields.char('Username', size=64,), - 'gmail_password': fields.char('Password', size=64), - } +from openerp.osv import osv +from openerp import SUPERUSER_ID + +from httplib2 import Http +import urllib +import simplejson + + +class base_config_settings(osv.osv): + _inherit = "base.config.settings" + + def onchange_google_authorization_code(self, cr, uid, ids, service, authorization_code, context=None): + res = {} + if authorization_code: + ir_config = self.pool['ir.config_parameter'] + client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_%s_client_id' % service) + client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_%s_client_secret' % service) + redirect_uri = ir_config.get_param(cr, SUPERUSER_ID, 'google_redirect_uri') + + #Get the Refresh Token From Google And store it in ir.config_parameter + headers = {"Content-type": "application/x-www-form-urlencoded"} + data = dict(code=authorization_code, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, grant_type="authorization_code") + data = urllib.urlencode(data) + resp, content = Http().request("https://accounts.google.com/o/oauth2/token", "POST", data, headers) + content = simplejson.loads(content) + if 'refresh_token' in content.keys(): + ir_config.set_param(cr, uid, 'google_%s_refresh_token' % service, content['refresh_token']) + return res + + def _get_google_token_uri(self, cr, uid, service, context=None): + ir_config = self.pool['ir.config_parameter'] + params = { + 'scope': 'https://www.googleapis.com/auth/drive', + 'redirect_uri': ir_config.get_param(cr, SUPERUSER_ID, 'google_redirect_uri'), + 'client_id': ir_config.get_param(cr, SUPERUSER_ID, 'google_%s_client_id' % service), + 'response_type': 'code', + 'client_id': ir_config.get_param(cr, SUPERUSER_ID, 'google_%s_client_id' % service), + } + uri = 'https://accounts.google.com/o/oauth2/auth?%s' % urllib.urlencode(params) + return uri # vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4: - diff --git a/addons/google_base_account/google_base_account_data.xml b/addons/google_base_account/google_base_account_data.xml new file mode 100644 index 00000000000..c6748df7dc9 --- /dev/null +++ b/addons/google_base_account/google_base_account_data.xml @@ -0,0 +1,9 @@ + + + + + google_redirect_uri + urn:ietf:wg:oauth:2.0:oob + + + \ No newline at end of file diff --git a/addons/google_base_account/google_base_account_view.xml b/addons/google_base_account/google_base_account_view.xml deleted file mode 100644 index 8613582692f..00000000000 --- a/addons/google_base_account/google_base_account_view.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - res.users.google.form1 - res.users - - - - - - - - - - - - - - diff --git a/addons/google_base_account/wizard/__init__.py b/addons/google_base_account/wizard/__init__.py deleted file mode 100644 index a6b9c644a1e..00000000000 --- a/addons/google_base_account/wizard/__init__.py +++ /dev/null @@ -1,25 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). -# -# 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 . -# -############################################################################## - -import google_login - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: - diff --git a/addons/google_base_account/wizard/google_login.py b/addons/google_base_account/wizard/google_login.py deleted file mode 100644 index 1771d99b51e..00000000000 --- a/addons/google_base_account/wizard/google_login.py +++ /dev/null @@ -1,85 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). -# -# 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 . -# -############################################################################## - -from openerp.osv import fields,osv -from openerp.tools.translate import _ -try: - import gdata.contacts.service - import gdata.contacts.client - import gdata.calendar.service -except ImportError: - raise osv.except_osv(_('Google Contacts Import Error!'), _('Please install gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list')) - -class google_login(osv.osv_memory): - _description ='Google Contact' - _name = 'google.login' - _columns = { - 'user': fields.char('Google Username', size=64, required=True), - 'password': fields.char('Google Password', size=64), - } - - def google_login(self, user, password, type='', context=None): - if type == 'group': - gd_client = gdata.contacts.service.ContactsService() - elif type == 'contact': - gd_client = gdata.contacts.service.ContactsService() - elif type == 'calendar': - gd_client = gdata.calendar.service.CalendarService() - elif type =='docs_client': - gd_client = gdata.docs.client.DocsClient() - else: - gd_client = gdata.contacts.service.ContactsService() - try: - gd_client.ClientLogin(user, password, gd_client.source) - except Exception: - return False - return gd_client - - - def default_get(self, cr, uid, fields, context=None): - res = super(google_login, self).default_get(cr, uid, fields, context=context) - user_obj = self.pool.get('res.users').browse(cr, uid, uid) - if 'user' in fields: - res.update({'user': user_obj.gmail_user}) - if 'password' in fields: - res.update({'password': user_obj.gmail_password}) - return res - - def login(self, cr, uid, ids, context=None): - data = self.read(cr, uid, ids)[0] - user = data['user'] - password = data['password'] - if self.google_login(user, password): - res = { - 'gmail_user': user, - 'gmail_password': password - } - self.pool.get('res.users').write(cr, uid, uid, res, context=context) - else: - raise osv.except_osv(_('Error!'), _("Authentication failed. Check the user and password.")) - - return self._get_next_action(cr, uid, context=context) - - def _get_next_action(self, cr, uid, context=None): - return {'type': 'ir.actions.act_window_close'} - - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/google_base_account/wizard/google_login_view.xml b/addons/google_base_account/wizard/google_login_view.xml deleted file mode 100644 index ed345ab5a0f..00000000000 --- a/addons/google_base_account/wizard/google_login_view.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - google.login.form - google.login - -
- - - - -
-
-
-
-
- - - - Google Login - ir.actions.act_window - google.login - form - form - new - - -
-
diff --git a/addons/google_docs/__init__.py b/addons/google_docs/__init__.py deleted file mode 100644 index dd6285502c0..00000000000 --- a/addons/google_docs/__init__.py +++ /dev/null @@ -1 +0,0 @@ -import google_docs diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py deleted file mode 100644 index 2fd1976ae3b..00000000000 --- a/addons/google_docs/google_docs.py +++ /dev/null @@ -1,193 +0,0 @@ -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2012 OpenERP SA (). -# -# 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 . -# -############################################################################## -import logging -from datetime import datetime - -from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT -from openerp.osv import fields, osv -from openerp.tools.translate import _ - -_logger = logging.getLogger(__name__) - -try: - import gdata.docs.data - import gdata.docs.client - - # API breakage madness in the gdata API - those guys are insane. - try: - # gdata 2.0.15+ - gdata.docs.client.DocsClient.copy_resource - except AttributeError: - # gdata 2.0.14- : copy_resource() was copy() - gdata.docs.client.DocsClient.copy_resource = gdata.docs.client.DocsClient.copy - - try: - # gdata 2.0.16+ - gdata.docs.client.DocsClient.get_resource_by_id - except AttributeError: - try: - # gdata 2.0.15+ - gdata.docs.client.DocsClient.get_resource_by_self_link - def get_resource_by_id_2_0_16(self, resource_id, **kwargs): - return self.GetResourceBySelfLink( - gdata.docs.client.RESOURCE_FEED_URI + ('/%s' % resource_id), **kwargs) - gdata.docs.client.DocsClient.get_resource_by_id = get_resource_by_id_2_0_16 - except AttributeError: - # gdata 2.0.14- : alias get_resource_by_id() - gdata.docs.client.DocsClient.get_resource_by_id = gdata.docs.client.DocsClient.get_doc - - try: - import atom.http_interface - _logger.info('GData lib version `%s` detected' % atom.http_interface.USER_AGENT) - except (ImportError, AttributeError): - _logger.debug('GData lib version could not be detected', exc_info=True) - -except ImportError: - _logger.warning("Please install latest gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list") - - -class google_docs_ir_attachment(osv.osv): - _inherit = 'ir.attachment' - - def _auth(self, cr, uid, context=None): - ''' - 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. We use default_get() instead of a create() followed by a read() on the - # google.login object, because it is easier. The keys 'user' and 'password' ahve to be passed in the dict - # but the values will be replaced by the user 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) - if not client: - raise osv.except_osv(_('Google Docs Error!'), _("Check your google configuration in Users/Users/Synchronization tab.")) - _logger.info('Logged into google docs as %s', user_config['user']) - return client - - def create_empty_google_doc(self, cr, uid, res_model, res_id, context=None): - '''Create a new google document, empty and with a default type (txt) - :param res_model: the object for which the google doc is created - :param res_id: the Id of the object for which the google doc is created - :return: the ID of the google document object created - ''' - #login with the base account google module - client = self._auth(cr, uid, context=context) - # create the document in google docs - title = "%s %s" % (context.get("name","Untitled Document."), datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT)) - local_resource = gdata.docs.data.Resource(gdata.docs.data.DOCUMENT_LABEL,title=title) - #create a new doc in Google Docs - gdocs_resource = client.post(entry=local_resource, uri='https://docs.google.com/feeds/default/private/full/') - # create an ir.attachment into the db - self.create(cr, uid, { - 'res_model': res_model, - 'res_id': res_id, - 'type': 'url', - 'name': title, - 'url': gdocs_resource.get_alternate_link().href, - }, context=context) - return {'resource_id': gdocs_resource.resource_id.text, - 'title': title, - 'url': gdocs_resource.get_alternate_link().href} - - def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None): - ''' - copy an existing document in google docs - :param res_model: the object for which the google doc is created - :param res_id: the Id of the object for which the google doc is created - :param name_gdocs: the name of the future ir.attachment that will be created. Based on the google doc template foun. - :param gdoc_template_id: the id of the google doc document to copy - :return: the ID of the google document object created - ''' - #login with the base account google module - client = self._auth(cr, uid) - # fetch and copy the original document - try: - doc = client.get_resource_by_id(gdoc_template_id) - #copy the document you choose in the configuration - copy_resource = client.copy_resource(doc, name_gdocs) - except: - raise osv.except_osv(_('Google Docs Error!'), _("Your resource id is not correct. You can find the id in the google docs URL.")) - # create an ir.attachment - self.create(cr, uid, { - 'res_model': res_model, - 'res_id': res_id, - 'type': 'url', - 'name': name_gdocs, - 'url': copy_resource.get_alternate_link().href - }, context=context) - return copy_resource.resource_id.text - - def google_doc_get(self, cr, uid, res_model, ids, context=None): - ''' - Function called by the js, when no google doc are yet associated with a record, with the aim to create one. It - will first seek for a google.docs.config associated with the model `res_model` to find out what's the template - of google doc to copy (this is usefull if you want to start with a non-empty document, a type or a name - different than the default values). If no config is associated with the `res_model`, then a blank text document - with a default name is created. - :param res_model: the object for which the google doc is created - :param ids: the list of ids of the objects for which the google doc is created. This list is supposed to have - a length of 1 element only (batch processing is not supported in the code, though nothing really prevent it) - :return: the google document object created - ''' - if len(ids) != 1: - raise osv.except_osv(_('Google Docs Error!'), _("Creating google docs may only be done by one at a time.")) - res_id = ids[0] - pool_ir_attachment = self.pool.get('ir.attachment') - pool_gdoc_config = self.pool.get('google.docs.config') - name_gdocs = '' - model_fields_dic = self.pool[res_model].read(cr, uid, res_id, [], context=context) - - # check if a model is configured with a template - google_docs_config = pool_gdoc_config.search(cr, uid, [('model_id', '=', res_model)], context=context) - if google_docs_config: - name_gdocs = pool_gdoc_config.browse(cr, uid, google_docs_config, context=context)[0].name_template - try: - name_gdocs = name_gdocs % model_fields_dic - except: - raise osv.except_osv(_('Key Error!'), _("Your Google Doc Name Pattern's key does not found in object.")) - google_template_id = pool_gdoc_config.browse(cr, uid, google_docs_config[0], context=context).gdocs_resource_id - google_document = pool_ir_attachment.copy_gdoc(cr, uid, res_model, res_id, name_gdocs, google_template_id, context=context) - else: - google_document = pool_ir_attachment.create_empty_google_doc(cr, uid, res_model, res_id, context=context) - return google_document - -class config(osv.osv): - _name = 'google.docs.config' - _description = "Google Docs templates config" - - _columns = { - 'model_id': fields.many2one('ir.model', 'Model', required=True), - 'gdocs_resource_id': fields.char('Google Resource ID to Use as Template', size=64, help=''' -This is the id of the template document, on google side. You can find it thanks to its URL: -*for a text document with url like `https://docs.google.com/a/openerp.com/document/d/123456789/edit`, the ID is `document:123456789` -*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` -*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` -*for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789` -... -''', required=True), - '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), - } - - _defaults = { - 'name_template': 'gdoc_%(name)s', - } diff --git a/addons/google_docs/res_config_user_view.xml b/addons/google_docs/res_config_user_view.xml deleted file mode 100644 index e6c4223735f..00000000000 --- a/addons/google_docs/res_config_user_view.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - - - - - - google_docs.config.tree - google.docs.config - - - - - - - - - - google_docs.config.form - google.docs.config - -
- - - -
-
-
- - - Models configuration - google.docs.config - ir.actions.act_window - form - - - - -
-
diff --git a/addons/google_docs/security/ir.model.access.csv b/addons/google_docs/security/ir.model.access.csv deleted file mode 100644 index 9393d586fb8..00000000000 --- a/addons/google_docs/security/ir.model.access.csv +++ /dev/null @@ -1,3 +0,0 @@ -id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink -access_google_docs,google.docs.config,model_google_docs_config,,1,0,0,0 -access_google_docs,google.docs.config,model_google_docs_config,base.group_system,1,1,1,1 \ No newline at end of file diff --git a/addons/google_docs/static/src/js/gdocs.js b/addons/google_docs/static/src/js/gdocs.js deleted file mode 100644 index 1cc511ef4d9..00000000000 --- a/addons/google_docs/static/src/js/gdocs.js +++ /dev/null @@ -1,40 +0,0 @@ -openerp.google_docs = function(instance, m) { -var _t = instance.web._t, - QWeb = instance.web.qweb; - - instance.web.Sidebar.include({ - redraw: function() { - var self = this; - this._super.apply(this, arguments); - self.$el.find('.oe_sidebar_add_attachment').after(QWeb.render('AddGoogleDocumentItem', {widget: self})) - self.$el.find('.oe_sidebar_add_google_doc').on('click', function (e) { - self.on_google_doc(); - }); - }, - on_google_doc: function() { - var self = this; - var view = self.getParent(); - var ids = ( view.fields_view.type != "form" )? view.groups.get_selection().ids : [ view.datarecord.id ]; - if( !_.isEmpty(ids) ){ - view.sidebar_eval_context().done(function (context) { - var ds = new instance.web.DataSet(this, 'ir.attachment', context); - ds.call('google_doc_get', [view.dataset.model, ids, context]).done(function(r) { - if (r == 'False') { - var params = { - error: response, - message: _t("The user google credentials are not set yet. Contact your administrator for help.") - } - $(openerp.web.qweb.render("DialogWarning", params)).dialog({ - title: _t("User Google credentials are not yet set."), - modal: true, - }); - } - }).done(function(r){ - window.open(r.url,"_blank"); - view.reload(); - }); - }); - } - } - }); -}; diff --git a/addons/google_drive/__init__.py b/addons/google_drive/__init__.py new file mode 100644 index 00000000000..e1cf2d00f3c --- /dev/null +++ b/addons/google_drive/__init__.py @@ -0,0 +1 @@ +import google_drive diff --git a/addons/google_docs/__openerp__.py b/addons/google_drive/__openerp__.py similarity index 63% rename from addons/google_docs/__openerp__.py rename to addons/google_drive/__openerp__.py index 2d064fdb8a9..9e6c73d2649 100644 --- a/addons/google_docs/__openerp__.py +++ b/addons/google_drive/__openerp__.py @@ -20,22 +20,31 @@ ############################################################################## { - 'name': 'Google Docs integration', + 'name': 'Google Driveā„¢ integration', 'version': '0.2', 'author': 'OpenERP SA', 'website': 'http://openerp.com', 'category': 'Tools', 'installable': True, 'auto_install': False, - 'js': ['static/src/js/gdocs.js'], - 'qweb': ['static/src/xml/gdocs.xml'], + 'js': [ + 'static/lib/gapi/client.js', + 'static/src/js/gdrive.js', + ], 'data': [ 'security/ir.model.access.csv', - 'res_config_user_view.xml' + 'res_config_user_view.xml', + 'google_drive_data.xml' ], - 'depends': ['google_base_account','document'], + 'demo': [ + 'google_drive_demo.xml' + ], + 'depends': ['base_setup', 'google_base_account'], 'description': """ -Module to attach a google document to any model. -================================================ +Integrate google document to OpenERP record. +============================================ + +This module allows you to integrate google documents to any of your OpenERP record quickly and easily using OAuth 2.0 for Installed Applications, +You can configure your google Authorization Code from Settings > Configuration > General Settings by clicking on "Generate Google Authorization Code" """ } diff --git a/addons/google_drive/google_drive.py b/addons/google_drive/google_drive.py new file mode 100644 index 00000000000..d5d433787bb --- /dev/null +++ b/addons/google_drive/google_drive.py @@ -0,0 +1,211 @@ +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2012 OpenERP SA (). +# +# 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 . +# +############################################################################## +import logging + +from openerp import SUPERUSER_ID +from openerp.osv import fields, osv +from openerp.tools.translate import _ +from urlparse import urlparse + +from httplib2 import Http +import urllib +import json + +_logger = logging.getLogger(__name__) + + +class config(osv.osv): + _name = 'google.drive.config' + _description = "Google Drive templates config" + + def get_google_doc_name(self, cr, uid, ids, res_id, template_id, context=None): + res = {} + for config in self.browse(cr, SUPERUSER_ID, ids, context=context): + model = config.model_id + filter_name = config.filter_id and config.filter_id.name or False + record = self.pool.get(model.model).read(cr, uid, res_id, [], context=context) + record.update({'model': model.name, 'filter': filter_name}) + name_gdocs = config.name_template or "%(name)s_%(model)s_%(filter)s_gdrive" + try: + name_gdocs = name_gdocs % record + except: + raise osv.except_osv(_('Key Error!'), _("At least one key cannot be found in your Google Drive name pattern")) + + attach_pool = self.pool.get("ir.attachment") + attach_ids = attach_pool.search(cr, uid, [('res_model', '=', model.model), ('name', '=', name_gdocs), ('res_id', '=', res_id)]) + url = False + if attach_ids: + attachment = attach_pool.browse(cr, uid, attach_ids[0], context) + url = attachment.url + else: + url = self.copy_doc(cr, uid, ids, res_id, template_id, name_gdocs, model.model, context) + res[config.id] = {'url': url} + return res + + def copy_doc(self, cr, uid, ids, res_id, template_id, name_gdocs, res_model, context=None): + ir_config = self.pool['ir.config_parameter'] + google_drive_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_id') + google_drive_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_secret') + google_drive_refresh_token = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_refresh_token') + google_web_base_url = ir_config.get_param(cr, SUPERUSER_ID, 'web.base.url') + + #For Getting New Access Token With help of old Refresh Token + headers = {"Content-type": "application/x-www-form-urlencoded"} + data = dict(client_id=google_drive_client_id, + refresh_token=google_drive_refresh_token, + client_secret=google_drive_client_secret, + grant_type="refresh_token") + data = urllib.urlencode(data) + resp, content = Http().request("https://accounts.google.com/o/oauth2/token", "POST", data, headers) + content = json.loads(content) + + # Copy template in to drive with help of new access token + if 'access_token' in content: + request_url = "https://www.googleapis.com/drive/v2/files/%s?fields=parents/id&access_token=%s" % (template_id, content['access_token']) + resp, parents = Http().request(request_url, "GET") + parents_dict = json.loads(parents) + + headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} + record_url = "Click on link to open Record in OpenERP\n %s/?db=%s#id=%s&model=%s" % (google_web_base_url, cr.dbname, res_id, res_model) + data = {"title": name_gdocs, "description": record_url, "parents": parents_dict['parents']} + request_url = "https://www.googleapis.com/drive/v2/files/%s/copy?access_token=%s" % (template_id, content['access_token']) + resp, content = Http().request(request_url, "POST", json.dumps(data), headers) + content = json.loads(content) + res = False + if 'alternateLink' in content.keys(): + attach_pool = self.pool.get("ir.attachment") + attach_vals = {'res_model': res_model, 'name': name_gdocs, 'res_id': res_id, 'type': 'url', 'url': content['alternateLink']} + attach_pool.create(cr, uid, attach_vals) + res = content['alternateLink'] + else: + raise self.pool.get('res.config.settings').get_config_warning(cr, _("You haven't configured 'Authorization Code' generated from google, Please generate and configure it in %(menu:base_setup.menu_general_configuration)s."), context=context) + return res + + def get_google_drive_config(self, cr, uid, res_model, res_id, context=None): + ''' + Function called by the js, when no google doc are yet associated with a record, with the aim to create one. It + will first seek for a google.docs.config associated with the model `res_model` to find out what's the template + of google doc to copy (this is usefull if you want to start with a non-empty document, a type or a name + different than the default values). If no config is associated with the `res_model`, then a blank text document + with a default name is created. + :param res_model: the object for which the google doc is created + :param ids: the list of ids of the objects for which the google doc is created. This list is supposed to have + a length of 1 element only (batch processing is not supported in the code, though nothing really prevent it) + :return: the config id and config name + ''' + if not res_id: + raise osv.except_osv(_('Google Drive Error!'), _("Creating google drive may only be done by one at a time.")) + # check if a model is configured with a template + config_ids = self.search(cr, uid, [('model_id', '=', res_model)], context=context) + configs = [] + for config in self.browse(cr, SUPERUSER_ID, config_ids, context=context): + if config.filter_id: + if (config.filter_id.user_id and config.filter_id.user_id.id != uid): + #Private + continue + google_doc_configs = self._filter(cr, uid, config, config.filter_id, res_id, context=context) + if google_doc_configs: + configs.append({'id': config.id, 'name': config.name}) + else: + configs.append({'id': config.id, 'name': config.name}) + return configs + + def _filter(self, cr, uid, action, action_filter, record_ids, context=None): + """ filter the list record_ids that satisfy the action filter """ + if record_ids and action_filter: + if not action.model_id.model == action_filter.model_id: + raise osv.except_osv(_('Warning!'), _("Something went wrong with the configuration of attachments with google drive.Please contact your Administrator to fix the problem.")) + model = self.pool.get(action_filter.model_id) + domain = [('id', 'in', [record_ids])] + eval(action_filter.domain) + ctx = dict(context or {}) + ctx.update(eval(action_filter.context)) + record_ids = model.search(cr, uid, domain, context=ctx) + return record_ids + + def _resource_get(self, cr, uid, ids, name, arg, context=None): + result = {} + for data in self.browse(cr, uid, ids, context): + template_url = data.google_drive_template_url + try: + url = urlparse(template_url) + res = url.path.split('/') + if res[1] == "spreadsheet": + key = url.query.split('=')[1] + else: + key = res[3] + result[data.id] = str(key) + except: + raise osv.except_osv(_('Incorrect URL!'), _("Please enter a valid URL.")) + return result + + def _client_id_get(self, cr, uid, ids, name, arg, context=None): + result = {} + for config_id in ids: + config = self.pool['ir.config_parameter'] + result[config_id] = config.get_param(cr, SUPERUSER_ID, 'google_drive_client_id') + return result + + _columns = { + 'name': fields.char('Template Name', required=True, size=1024), + 'model_id': fields.many2one('ir.model', 'Model', ondelete='set null', required=True), + 'model': fields.related('model_id', 'model', type='char', string='Model', readonly=True), + 'filter_id': fields.many2one('ir.filters', 'Filter', domain="[('model_id', '=', model)]"), + 'google_drive_template_url': fields.char('Template URL', required=True, size=1024), + 'google_drive_resource_id': fields.function(_resource_get, type="char", string='Resource Id'), + 'google_drive_client_id': fields.function(_client_id_get, type="char", string='Google Client '), + 'name_template': fields.char('Google Drive Name Pattern', size=64, help='Choose how the new google drive will be named, on google side. Eg. gdoc_%(field_name)s', required=True), + } + + def onchange_model_id(self, cr, uid, ids, model_id, context=None): + res = {} + if model_id: + model = self.pool['ir.model'].browse(cr, uid, model_id, context=context) + res['value'] = {'model': model.model} + else: + res['value'] = {'filter_id': False, 'model': False} + return res + + _defaults = { + 'name_template': '%(name)s_%(model)s_%(filter)s_gdrive', + } + + def _check_model_id(self, cr, uid, ids, context=None): + config_id = self.browse(cr, uid, ids[0], context=context) + if config_id.filter_id and config_id.model_id.model != config_id.filter_id.model_id: + return False + return True + + _constraints = [ + (_check_model_id, 'Model of selected filter is not matching with model of current template.', ['model_id', 'filter_id']), + ] + +config() + + +class base_config_settings(osv.osv): + _inherit = "base.config.settings" + + _columns = { + 'google_drive_authorization_code': fields.char('Authorization Code', size=124), + 'google_drive_uri': fields.char('URI', readonly=True, help="The URL to generate the authorization code from Google"), + } + _defaults = { + 'google_drive_uri': lambda s, cr, uid, c: s._get_google_token_uri(cr, uid, 'drive', context=c), + } diff --git a/addons/google_drive/google_drive_data.xml b/addons/google_drive/google_drive_data.xml new file mode 100644 index 00000000000..9bb81e2744a --- /dev/null +++ b/addons/google_drive/google_drive_data.xml @@ -0,0 +1,25 @@ + + + + + + google_drive_client_id + 39623646228-eg3ggo3mk6o40m7rguobi3rkl9frh4tb.apps.googleusercontent.com + + + + google_drive_client_secret + Ul-PtmnSWs3euWs20fdono0e + + + + google_drive_refresh_token + - + + + + google_drive_authorization_code + - + + + \ No newline at end of file diff --git a/addons/google_drive/google_drive_demo.xml b/addons/google_drive/google_drive_demo.xml new file mode 100644 index 00000000000..330d1d4a66b --- /dev/null +++ b/addons/google_drive/google_drive_demo.xml @@ -0,0 +1,24 @@ + + + + + + + Customer + res.partner + [['customer', '=', 1]] + + + + + + Customer Doc + + + https://docs.google.com/spreadsheet/ccc?key=0Ah2qnrLAoZmUdGRvdVdmS1VoSDctWk1kd18taGZ4ckE#gid=0 + %(name)s_%(model)s_%(filter)s_gdrive + + + + + diff --git a/addons/google_docs/i18n/ar.po b/addons/google_drive/i18n/ar.po similarity index 100% rename from addons/google_docs/i18n/ar.po rename to addons/google_drive/i18n/ar.po diff --git a/addons/google_docs/i18n/cs.po b/addons/google_drive/i18n/cs.po similarity index 100% rename from addons/google_docs/i18n/cs.po rename to addons/google_drive/i18n/cs.po diff --git a/addons/google_docs/i18n/de.po b/addons/google_drive/i18n/de.po similarity index 100% rename from addons/google_docs/i18n/de.po rename to addons/google_drive/i18n/de.po diff --git a/addons/google_docs/i18n/es.po b/addons/google_drive/i18n/es.po similarity index 100% rename from addons/google_docs/i18n/es.po rename to addons/google_drive/i18n/es.po diff --git a/addons/google_docs/i18n/fr.po b/addons/google_drive/i18n/fr.po similarity index 100% rename from addons/google_docs/i18n/fr.po rename to addons/google_drive/i18n/fr.po diff --git a/addons/google_docs/i18n/google_docs.pot b/addons/google_drive/i18n/google_docs.pot similarity index 100% rename from addons/google_docs/i18n/google_docs.pot rename to addons/google_drive/i18n/google_docs.pot diff --git a/addons/google_docs/i18n/hr.po b/addons/google_drive/i18n/hr.po similarity index 100% rename from addons/google_docs/i18n/hr.po rename to addons/google_drive/i18n/hr.po diff --git a/addons/google_docs/i18n/hu.po b/addons/google_drive/i18n/hu.po similarity index 100% rename from addons/google_docs/i18n/hu.po rename to addons/google_drive/i18n/hu.po diff --git a/addons/google_docs/i18n/it.po b/addons/google_drive/i18n/it.po similarity index 100% rename from addons/google_docs/i18n/it.po rename to addons/google_drive/i18n/it.po diff --git a/addons/google_docs/i18n/ln.po b/addons/google_drive/i18n/ln.po similarity index 100% rename from addons/google_docs/i18n/ln.po rename to addons/google_drive/i18n/ln.po diff --git a/addons/google_docs/i18n/mk.po b/addons/google_drive/i18n/mk.po similarity index 100% rename from addons/google_docs/i18n/mk.po rename to addons/google_drive/i18n/mk.po diff --git a/addons/google_docs/i18n/mn.po b/addons/google_drive/i18n/mn.po similarity index 100% rename from addons/google_docs/i18n/mn.po rename to addons/google_drive/i18n/mn.po diff --git a/addons/google_docs/i18n/nl.po b/addons/google_drive/i18n/nl.po similarity index 100% rename from addons/google_docs/i18n/nl.po rename to addons/google_drive/i18n/nl.po diff --git a/addons/google_docs/i18n/pl.po b/addons/google_drive/i18n/pl.po similarity index 100% rename from addons/google_docs/i18n/pl.po rename to addons/google_drive/i18n/pl.po diff --git a/addons/google_docs/i18n/pt.po b/addons/google_drive/i18n/pt.po similarity index 100% rename from addons/google_docs/i18n/pt.po rename to addons/google_drive/i18n/pt.po diff --git a/addons/google_docs/i18n/pt_BR.po b/addons/google_drive/i18n/pt_BR.po similarity index 100% rename from addons/google_docs/i18n/pt_BR.po rename to addons/google_drive/i18n/pt_BR.po diff --git a/addons/google_docs/i18n/ro.po b/addons/google_drive/i18n/ro.po similarity index 100% rename from addons/google_docs/i18n/ro.po rename to addons/google_drive/i18n/ro.po diff --git a/addons/google_docs/i18n/ru.po b/addons/google_drive/i18n/ru.po similarity index 100% rename from addons/google_docs/i18n/ru.po rename to addons/google_drive/i18n/ru.po diff --git a/addons/google_docs/i18n/sl.po b/addons/google_drive/i18n/sl.po similarity index 100% rename from addons/google_docs/i18n/sl.po rename to addons/google_drive/i18n/sl.po diff --git a/addons/google_docs/i18n/sv.po b/addons/google_drive/i18n/sv.po similarity index 100% rename from addons/google_docs/i18n/sv.po rename to addons/google_drive/i18n/sv.po diff --git a/addons/google_docs/i18n/tr.po b/addons/google_drive/i18n/tr.po similarity index 100% rename from addons/google_docs/i18n/tr.po rename to addons/google_drive/i18n/tr.po diff --git a/addons/google_docs/i18n/zh_CN.po b/addons/google_drive/i18n/zh_CN.po similarity index 100% rename from addons/google_docs/i18n/zh_CN.po rename to addons/google_drive/i18n/zh_CN.po diff --git a/addons/google_drive/res_config_user_view.xml b/addons/google_drive/res_config_user_view.xml new file mode 100644 index 00000000000..8dfdee31143 --- /dev/null +++ b/addons/google_drive/res_config_user_view.xml @@ -0,0 +1,93 @@ + + + + + + + + google_drive.config.tree + google.drive.config + + + + + + + + + + google_drive.config.form + google.drive.config + +
+ + + + + + +
+
+ + + Google Drive Templates + google.drive.config + ir.actions.act_window + form + + +

+ Click to add a new template. +

+

+ Link your own google drive templates to any record of OpenERP. If you have really specific documents you want your collaborator fill in, e.g. Use a spreadsheet to control the quality of your product or review the delivery checklist for each order in a foreign country, ... Its very easy to manage them, link them to OpenERP and use them to collaborate with your employees. +

+
+
+ + + General Settings + base.config.settings + + + +
+
+ + and paste it here + +
+
+
+
+
+ + + +
+
diff --git a/addons/google_drive/security/ir.model.access.csv b/addons/google_drive/security/ir.model.access.csv new file mode 100644 index 00000000000..ae98cb36976 --- /dev/null +++ b/addons/google_drive/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_google_drive_all,google.drive.config,model_google_drive_config,,1,0,0,0 +access_google_drive,google.drive.config,model_google_drive_config,base.group_system,1,1,1,1 \ No newline at end of file diff --git a/addons/google_drive/static/lib/gapi/client.js b/addons/google_drive/static/lib/gapi/client.js new file mode 100644 index 00000000000..7f40ec55ef9 --- /dev/null +++ b/addons/google_drive/static/lib/gapi/client.js @@ -0,0 +1,7 @@ +var gapi=window.gapi=window.gapi||{};gapi._bs=new Date().getTime();(function(){var f=null,g=encodeURIComponent,k=window,m=decodeURIComponent,n="push",r="test",t="shift",u="replace",y="length",B="split",C="join";var D=k,E=document,aa=D.location,ba=function(){},ca=/\[native code\]/,G=function(a,b,c){return a[b]=a[b]||c},da=function(a){for(var b=0;bda.call(b,e)&&c[n](e)}return c},sa=function(a){"loading"!=E.readyState?Z(a):E.write("<"+X+' src="'+encodeURI(a)+'">")},Z=function(a){var b=E.createElement(X);b.setAttribute("src",a);b.async="true";(a=E.getElementsByTagName(X)[0])?a.parentNode.insertBefore(b,a):(E.head||E.body||E.documentElement).appendChild(b)},ta=function(a,b){var c=b&&b._c;if(c)for(var d=0;d', + config_id: res.id, + res_id: res_id, + res_model: view.dataset.model, + callback: self.on_google_doc, + classname: 'oe_share_gdoc' + }, + ]); + }) + } + }); + }); + } + }, + + fetch: function (model, fields, domain, ctx) { + return new instance.web.Model(model).query(fields).filter(domain).context(ctx).all() + }, + + on_google_doc: function (doc_item) { + var self = this; + self.config = doc_item; + var loaded = self.fetch('google.drive.config', ['google_drive_resource_id', 'google_drive_client_id'], [['id', '=', doc_item.config_id]]) + .then(function (configs) { + var ds = new instance.web.DataSet(self, 'google.drive.config'); + ds.call('get_google_doc_name', [[doc_item.config_id], doc_item.res_id,configs[0].google_drive_resource_id]).done(function (r) { + if (!_.isEmpty(r)) { + _.each(r, function (res) { + if(res.url) + { + window.open(res.url, '_blank'); + } + }); + } + }); + }); + }, + + }); +}; \ No newline at end of file diff --git a/addons/google_docs/static/src/xml/gdocs.xml b/addons/google_drive/static/src/xml/gdocs.xml similarity index 100% rename from addons/google_docs/static/src/xml/gdocs.xml rename to addons/google_drive/static/src/xml/gdocs.xml