Skip to content

Commit

Permalink
[FIX] google_drive: adpating to new framework JS
Browse files Browse the repository at this point in the history
The google_drive module has not been converted to the new Odoo Javascript framework, and hence was not even started.
This commit corrects that.

The logic is essentially the same as in previous versions

The test implemented contains an inevitable hack that makes sure no other test is affected,
in particular by the new route that the module calls (and that any Sidebar will call)

OPW 801145

closes #21993
  • Loading branch information
kebeclibre committed Jan 17, 2018
1 parent f05b2b1 commit 5140d3d
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 54 deletions.
3 changes: 3 additions & 0 deletions addons/google_drive/models/google_drive.py
Expand Up @@ -155,6 +155,9 @@ def get_google_drive_config(self, res_model, res_id):
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
'''
# TO DO in master: fix my signature and my model
if isinstance(res_model, basestring):
res_model = self.env['ir.model'].search([('model', '=', res_model)]).id
if not res_id:
raise UserError(_("Creating google drive may only be done by one at a time."))
# check if a model is configured with a template
Expand Down
139 changes: 85 additions & 54 deletions addons/google_drive/static/src/js/gdrive.js
@@ -1,78 +1,109 @@
odoo.define('google_drive.google_drive', function (require) {
odoo.define('google_drive.sidebar', function (require) {
"use strict";

var data = require('web.data');
/**
* The purpose of this file is to include the Sidebar widget to add Google
* Drive related items.
*/

var Sidebar = require('web.Sidebar');


Sidebar.include({
init: function () {
var self = this;
var ids;
this._super.apply(this, arguments);
var view = self.getParent();
var result;
if (view.fields_view && view.fields_view.type === "form") {
ids = [];
view.on("load_record", self, function (r) {
ids = [r.id];
self.add_gdoc_items(view, r.id);
});
// TO DO: clean me in master
/**
* @override
*/
start: function () {
var def;
if (this.options.viewType === "form") {
def = this._addGoogleDocItems(this.env.model, this.env.activeIds[0]);
}
return $.when(def).then(this._super.bind(this));
},
add_gdoc_items: function (view, res_id) {

//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------

/**
* @private
* @param {string} model
* @param {integer} resID
* @returns {Deferred}
*/
_addGoogleDocItems: function (model, resID) {
var self = this;
if (!resID) {
return $.when();
}
var gdoc_item = _.indexOf(_.pluck(self.items.other, 'classname'), 'oe_share_gdoc');
if (gdoc_item !== -1) {
self.items.other.splice(gdoc_item, 1);
}
if (res_id) {
view.sidebar_eval_context().done(function (context) {
var ds = new data.DataSet(this, 'google.drive.config', context);
ds.call('get_google_drive_config', [view.dataset.model, res_id, context]).done(function (r) {
if (!_.isEmpty(r)) {
_.each(r, function (res) {
var already_there = false;
for (var i = 0;i < self.items.other.length;i++){
if (self.items.other[i].classname === "oe_share_gdoc" && self.items.other[i].label.indexOf(res.name) > -1){
already_there = true;
break;
}
}
if (!already_there){
self.add_items('other', [{
label: res.name,
config_id: res.id,
res_id: res_id,
res_model: view.dataset.model,
callback: self.on_google_doc,
classname: 'oe_share_gdoc'
},
]);
}
});
return this._rpc({
args: [this.env.model, resID],
context: this.env.context,
method: 'get_google_drive_config',
model: 'google.drive.config',
}).then(function (r) {
if (!_.isEmpty(r)) {
_.each(r, function (res) {
var already_there = false;
for (var i = 0; i < self.items.other.length; i++) {
var item = self.items.other[i];
if (item.classname === 'oe_share_gdoc' && item.label.indexOf(res.name) > -1) {
already_there = true;
break;
}
}
if (!already_there) {
self._addItems('other', [{
callback: self._onGoogleDocItemClicked.bind(self, res.id, resID),
classname: 'oe_share_gdoc',
config_id: res.id,
label: res.name,
res_id: resID,
res_model: model,
}]);
}
});
});
}
}
});
},
on_google_doc: function (doc_item) {

//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------

/**
* @private
* @param {integer} configID
* @param {integer} resID
*/
_onGoogleDocItemClicked: function (configID, resID) {
var self = this;
var domain = [['id', '=', doc_item.config_id]];
var domain = [['id', '=', configID]];
var fields = ['google_drive_resource_id', 'google_drive_client_id'];
this._rpc({
args: [domain, fields],
method: 'search_read',
model: 'google.drive.config',
}).then(function (configs) {
self._rpc({
args: [configID, resID, configs[0].google_drive_resource_id],
context: self.env.context,
method: 'get_google_drive_url',
model: 'google.drive.config',
method: 'search_read',
args: [domain, fields],
})
.then(function (configs) {
var ds = new data.DataSet(self, 'google.drive.config');
ds.call('get_google_drive_url', [doc_item.config_id, doc_item.res_id,configs[0].google_drive_resource_id, self.dataset.context]).done(function (url) {
if (url){
window.open(url, '_blank');
}
});
}).then(function (url) {
if (url){
window.open(url, '_blank');
}
});
});
},

});

return Sidebar;
});
112 changes: 112 additions & 0 deletions addons/google_drive/static/tests/gdrive_test.js
@@ -0,0 +1,112 @@
odoo.define('google_drive.gdrive_integration', function (require) {
"use strict";
//rebuild
var FormView = require('web.FormView');
var testUtils = require('web.test_utils');
var GoogleDriveSideBar = require('google_drive.sidebar');

var createView = testUtils.createView;

/*
* @override
* Avoid breaking other tests because of the new route
* that the module introduces
*/
var _addGoogleDocItemsOriginal = GoogleDriveSideBar.prototype._addGoogleDocItems;

var _addGoogleDocItemsMocked = function (model, resID) {
return $.when();
};

GoogleDriveSideBar.prototype._addGoogleDocItems = _addGoogleDocItemsMocked;

QUnit.module('gdrive_integration', {
beforeEach: function () {
// For our test to work, the _addGoogleDocItems function needs to be the original
GoogleDriveSideBar.prototype._addGoogleDocItems = _addGoogleDocItemsOriginal;

this.data = {
partner: {
fields: {
display_name: {string: "Displayed name", type: "char", searchable: true},
},
records: [{
id: 1,
display_name: "Locomotive Breath",
}],
},
'google.drive.config': {
fields: {
model_id: {string: 'Model', type: 'int'},
name: {string: 'Name', type: 'char'},
google_drive_resource_id: {string: 'Resource ID', type: 'char'},
},
records: [{
id: 27,
name: 'Cyberdyne Systems',
model_id: 1,
google_drive_resource_id: 'T1000',
}],
},
'ir.attachment': {
fields: {
name: {string: 'Name', type:'char'}
},
records: [],
}
};
},

afterEach: function() {
GoogleDriveSideBar.prototype._addGoogleDocItems = _addGoogleDocItemsMocked;
}

}, function () {
QUnit.module('Google Drive Sidebar');

QUnit.test('rendering of the google drive attachments in Sidebar', function (assert) {
assert.expect(3);

var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form string="Partners">' +
'<field name="display_name"/>' +
'</form>',
res_id: 1,
viewOptions: {sidebar: true},
mockRPC: function (route, args) {
if (route === '/web/dataset/call_kw/google.drive.config/get_google_drive_config') {
assert.deepEqual(args.args, ['partner', 1],
'The route to get google drive config should have been called');
return $.when([{id: 27, name: 'Cyberdyne Systems'}]);
}
if (route === '/web/dataset/call_kw/google.drive.config/search_read'){
return $.when([{google_drive_resource_id: "T1000",
google_drive_client_id: "cyberdyne.org",
id: 1}]);
}
if (route === '/web/dataset/call_kw/google.drive.config/get_google_drive_url') {
assert.deepEqual(args.args, [27, 1, 'T1000'],
'The route to get the Google url should have been called');
// We don't return anything useful, otherwise it will open a new tab
return $.when();
}
return this._super.apply(this, arguments);
}
});

var google_action = form.sidebar.$('.oe_share_gdoc');

assert.strictEqual(google_action.length, 1,
'The button to the google action should be present');

// Trigger opening of the dynamic link
google_action.find('a:first').click();

form.destroy();
});
});

});
5 changes: 5 additions & 0 deletions addons/google_drive/views/google_drive_templates.xml
Expand Up @@ -8,4 +8,9 @@
</xpath>
</template>

<template id="qunit_suite" name="google_drive tests" inherit_id="web.qunit_suite">
<xpath expr="//t[@t-set='head']" position="inside">
<script type="text/javascript" src="/google_drive/static/tests/gdrive_test.js"></script>
</xpath>
</template>
</odoo>

0 comments on commit 5140d3d

Please sign in to comment.