summaryrefslogtreecommitdiff
path: root/addons/google_docs
diff options
context:
space:
mode:
authordusan <dusan@ubuntu>2012-11-16 18:47:13 +0100
committerdusan <dusan@ubuntu>2012-11-16 18:47:13 +0100
commitd1a874ebd751c74ffce81f81e4a471f3ce4c6915 (patch)
tree0f56b09179b4c78adfa1e24ea554621b88835fc1 /addons/google_docs
parent0226031b246909f4090c888f0e04845e21a1ea00 (diff)
parent8ccbab82547e45300de0ca3c83497ae7c3e4523d (diff)
[MERGE] merge with parent
bzr revid: dusan@ubuntu-20121116174713-rrcta3h4ktr0pqui
Diffstat (limited to 'addons/google_docs')
-rw-r--r--addons/google_docs/__init__.py1
-rw-r--r--addons/google_docs/__openerp__.py41
-rw-r--r--addons/google_docs/google_docs.py163
-rw-r--r--addons/google_docs/res_config_user_view.xml54
-rw-r--r--addons/google_docs/security/ir.model.access.csv3
-rw-r--r--addons/google_docs/static/src/js/gdocs.js40
-rw-r--r--addons/google_docs/static/src/xml/gdocs.xml8
7 files changed, 310 insertions, 0 deletions
diff --git a/addons/google_docs/__init__.py b/addons/google_docs/__init__.py
new file mode 100644
index 00000000000..dd6285502c0
--- /dev/null
+++ b/addons/google_docs/__init__.py
@@ -0,0 +1 @@
+import google_docs
diff --git a/addons/google_docs/__openerp__.py b/addons/google_docs/__openerp__.py
new file mode 100644
index 00000000000..2d064fdb8a9
--- /dev/null
+++ b/addons/google_docs/__openerp__.py
@@ -0,0 +1,41 @@
+# -*- 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',
+ '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'],
+ 'data': [
+ 'security/ir.model.access.csv',
+ 'res_config_user_view.xml'
+ ],
+ 'depends': ['google_base_account','document'],
+ 'description': """
+Module to attach a google document to any model.
+================================================
+"""
+}
diff --git a/addons/google_docs/google_docs.py b/addons/google_docs/google_docs.py
new file mode 100644
index 00000000000..462c8240810
--- /dev/null
+++ b/addons/google_docs/google_docs.py
@@ -0,0 +1,163 @@
+##############################################################################
+#
+# 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/>.
+#
+##############################################################################
+from datetime import datetime
+from tools import DEFAULT_SERVER_DATETIME_FORMAT
+from osv import osv, fields
+from tools.translate import _
+try:
+ import gdata.docs.data
+ import gdata.docs.client
+ from gdata.client import RequestError
+ from gdata.docs.service import DOCUMENT_LABEL
+ import gdata.auth
+ from gdata.docs.data import Resource
+except ImportError:
+ import logging
+ _logger = logging.getLogger(__name__)
+ _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."))
+ 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:
+ original_resource = client.get_resource_by_id(gdoc_template_id)
+ #copy the document you choose in the configuration
+ copy_resource = client.copy_resource(original_resource, 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.get(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',
+ }
+config()
diff --git a/addons/google_docs/res_config_user_view.xml b/addons/google_docs/res_config_user_view.xml
new file mode 100644
index 00000000000..e6c4223735f
--- /dev/null
+++ b/addons/google_docs/res_config_user_view.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+ <data>
+
+ <!-- add google docs config field in user form -->
+
+ <record model="ir.ui.view" id="view_google_docs_config_tree">
+ <field name="name">google_docs.config.tree</field>
+ <field name="model">google.docs.config</field>
+ <field name="arch" type="xml">
+ <tree string="Google Docs Configuration">
+ <field name="model_id"/>
+ <field name="name_template"/>
+ </tree>
+ </field>
+ </record>
+
+ <record model="ir.ui.view" id="view_google_docs_config_form">
+ <field name="name">google_docs.config.form</field>
+ <field name="model">google.docs.config</field>
+ <field name="arch" type="xml">
+ <form string="Google Docs Configuration" version="7.0">
+ <group>
+ <field name="model_id"/>
+ <label for='gdocs_resource_id'/>
+ <div>
+ <field name='gdocs_resource_id'/>
+ <p class="oe_grey">
+This is the id of the template document, on google side. You can find it thanks to its URL:
+<ul>
+<li>for a text document with url like `https://docs.google.com/a/openerp.com/document/d/123456789/edit`, the ID is `document:123456789`</li>
+<li>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`</li>
+<li>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`</li>
+<li>for a drawing document with url like `https://docs.google.com/a/openerp.com/drawings/d/123456789/edit`, the ID is `drawings:123456789`</li>
+</ul>
+ </p>
+ </div>
+ <field name='name_template'/>
+ </group>
+ </form>
+ </field>
+ </record>
+
+ <record model='ir.actions.act_window' id='action_google_docs_users_config'>
+ <field name='name'>Models configuration</field>
+ <field name='res_model'>google.docs.config</field>
+ <field name='type'>ir.actions.act_window</field>
+ <field name='view_type'>form</field>
+ <field name='view_id' ref='view_google_docs_config_tree'/>
+ </record>
+ <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'/>
+ </data>
+</openerp>
diff --git a/addons/google_docs/security/ir.model.access.csv b/addons/google_docs/security/ir.model.access.csv
new file mode 100644
index 00000000000..9393d586fb8
--- /dev/null
+++ b/addons/google_docs/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_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
new file mode 100644
index 00000000000..45e1d09d42c
--- /dev/null
+++ b/addons/google_docs/static/src/js/gdocs.js
@@ -0,0 +1,40 @@
+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_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_docs/static/src/xml/gdocs.xml b/addons/google_docs/static/src/xml/gdocs.xml
new file mode 100644
index 00000000000..0c8d9c3b01f
--- /dev/null
+++ b/addons/google_docs/static/src/xml/gdocs.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- vim:fdl=1:
+-->
+<templates id="template" xml:space="preserve">
+<t t-name="AddGoogleDocumentItem">
+ <li class="oe_sidebar_add_google_doc"><span><b>Add Google Doc...</b></span></li>
+</t>
+</templates>