From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- addons/google_drive/static/description/icon.png | Bin 0 -> 10435 bytes addons/google_drive/static/description/icon.svg | 22 +++ .../google_drive/static/src/css/google_drive.css | 5 + addons/google_drive/static/src/img/drive_icon.png | Bin 0 -> 1542 bytes .../static/src/img/drive_icon_mono.png | Bin 0 -> 479 bytes addons/google_drive/static/src/js/gdrive.js | 93 +++++++++++++ addons/google_drive/static/src/xml/gdrive.xml | 14 ++ addons/google_drive/static/tests/gdrive_test.js | 154 +++++++++++++++++++++ addons/google_drive/static/tests/mock_server.js | 22 +++ 9 files changed, 310 insertions(+) create mode 100644 addons/google_drive/static/description/icon.png create mode 100644 addons/google_drive/static/description/icon.svg create mode 100644 addons/google_drive/static/src/css/google_drive.css create mode 100644 addons/google_drive/static/src/img/drive_icon.png create mode 100644 addons/google_drive/static/src/img/drive_icon_mono.png create mode 100644 addons/google_drive/static/src/js/gdrive.js create mode 100644 addons/google_drive/static/src/xml/gdrive.xml create mode 100644 addons/google_drive/static/tests/gdrive_test.js create mode 100644 addons/google_drive/static/tests/mock_server.js (limited to 'addons/google_drive/static') diff --git a/addons/google_drive/static/description/icon.png b/addons/google_drive/static/description/icon.png new file mode 100644 index 00000000..3d828754 Binary files /dev/null and b/addons/google_drive/static/description/icon.png differ diff --git a/addons/google_drive/static/description/icon.svg b/addons/google_drive/static/description/icon.svg new file mode 100644 index 00000000..d0f8c5aa --- /dev/null +++ b/addons/google_drive/static/description/icon.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/addons/google_drive/static/src/css/google_drive.css b/addons/google_drive/static/src/css/google_drive.css new file mode 100644 index 00000000..ce2b2259 --- /dev/null +++ b/addons/google_drive/static/src/css/google_drive.css @@ -0,0 +1,5 @@ +li.oe_share_gdoc > a:after { + content: url('/google_drive/static/src/img/drive_icon.png'); + position: absolute; + right: 5px; +} diff --git a/addons/google_drive/static/src/img/drive_icon.png b/addons/google_drive/static/src/img/drive_icon.png new file mode 100644 index 00000000..1553aa31 Binary files /dev/null and b/addons/google_drive/static/src/img/drive_icon.png differ diff --git a/addons/google_drive/static/src/img/drive_icon_mono.png b/addons/google_drive/static/src/img/drive_icon_mono.png new file mode 100644 index 00000000..9fd836b6 Binary files /dev/null and b/addons/google_drive/static/src/img/drive_icon_mono.png differ diff --git a/addons/google_drive/static/src/js/gdrive.js b/addons/google_drive/static/src/js/gdrive.js new file mode 100644 index 00000000..d6090aa9 --- /dev/null +++ b/addons/google_drive/static/src/js/gdrive.js @@ -0,0 +1,93 @@ +odoo.define('google_drive.ActionMenus', function (require) { + "use strict"; + + const ActionMenus = require('web.ActionMenus'); + const DropdownMenuItem = require('web.DropdownMenuItem'); + + /** + * Fetches the google drive action menu item props. To do so this function + * is given its parent props and env, as well as the RPC function bound to + * the parent context. + * Note that we use the bound RPC to benefit from its added behaviour (see + * web/component_extension). + * @param {Object} props + * @param {number[]} props.activeIds + * @param {Object} props.context + * @param {Object} env + * @param {Object} env.action The current action + * @param {Object} env.view The current view + * @param {Function} rpc Bound to the ActionMenus context + * @returns {Object | boolean} item props or false + */ + async function googleDrivePropsGetter(props, env, rpc) { + const [activeId] = props.activeIds; + const { context } = props; + if (env.view.type !== "form" || !activeId) { + return false; + } + const items = await rpc({ + args: [env.action.res_model, activeId], + context, + method: 'get_google_drive_config', + model: 'google.drive.config', + }); + return Boolean(items.length) && { activeId, context, items }; + } + + /** + * Google drive menu + * + * This component is actually a set of list items used to enrich the ActionMenus's + * "Action" dropdown list (@see ActionMenus). It will fetch + * the current user's google drive configuration and set the result as its + * items if any. + * @extends DropdownMenuItem + */ + class GoogleDriveMenu extends DropdownMenuItem { + + //--------------------------------------------------------------------- + // Handlers + //--------------------------------------------------------------------- + + /** + * @private + * @param {number} itemId + * @returns {Promise} + */ + async _onGoogleDocItemClick(itemId) { + const resID = this.props.activeId; + const domain = [['id', '=', itemId]]; + const fields = ['google_drive_resource_id', 'google_drive_client_id']; + const configs = await this.rpc({ + args: [domain, fields], + method: 'search_read', + model: 'google.drive.config', + }); + const url = await this.rpc({ + args: [itemId, resID, configs[0].google_drive_resource_id], + context: this.props.context, + method: 'get_google_drive_url', + model: 'google.drive.config', + }); + if (url) { + window.open(url, '_blank'); + } + } + } + GoogleDriveMenu.props = { + activeId: Number, + context: Object, + items: { + type: Array, + element: Object, + }, + }; + GoogleDriveMenu.template = 'GoogleDriveMenu'; + + ActionMenus.registry.add('google-drive-menu', { + Component: GoogleDriveMenu, + getProps: googleDrivePropsGetter, + }); + + return GoogleDriveMenu; +}); diff --git a/addons/google_drive/static/src/xml/gdrive.xml b/addons/google_drive/static/src/xml/gdrive.xml new file mode 100644 index 00000000..d12b3459 --- /dev/null +++ b/addons/google_drive/static/src/xml/gdrive.xml @@ -0,0 +1,14 @@ + + +
  • + +
  • +
    +
    diff --git a/addons/google_drive/static/tests/gdrive_test.js b/addons/google_drive/static/tests/gdrive_test.js new file mode 100644 index 00000000..3c684f8d --- /dev/null +++ b/addons/google_drive/static/tests/gdrive_test.js @@ -0,0 +1,154 @@ +odoo.define('google_drive.gdrive_integration', function (require) { + "use strict"; + + const FormView = require('web.FormView'); + const testUtils = require('web.test_utils'); + + const cpHelpers = testUtils.controlPanel; + + QUnit.module('Google Drive Integration', { + beforeEach() { + this.data = { + partner: { + fields: { + display_name: { string: "Displayed name", type: "char", searchable: true }, + }, + records: [ + { id: 1, display_name: "Locomotive Breath" }, + { id: 2, display_name: "Hey Macarena" }, + ], + }, + }; + }, + }, function () { + + QUnit.module('Google Drive ActionMenus'); + + QUnit.test('rendering of the google drive attachments in action menus', async function (assert) { + assert.expect(3); + + const form = await testUtils.createView({ + actionMenusRegistry: true, + arch: + `
    + + `, + data: this.data, + async mockRPC(route, args) { + switch (route) { + case '/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 [{ + id: 27, + name: 'Cyberdyne Systems', + }]; + case '/web/dataset/call_kw/google.drive.config/search_read': + return [{ + google_drive_resource_id: "T1000", + google_drive_client_id: "cyberdyne.org", + id: 1, + }]; + case '/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'); + return; // do not return anything or it will open a new tab. + } + }, + model: 'partner', + res_id: 1, + View: FormView, + viewOptions: { + hasActionMenus: true, + }, + }); + await cpHelpers.toggleActionMenu(form); + + assert.containsOnce(form, '.oe_share_gdoc_item', + "The button to the google action should be present"); + + await cpHelpers.toggleMenuItem(form, "Cyberdyne Systems"); + + form.destroy(); + }); + + QUnit.test("no google drive data", async function (assert) { + assert.expect(1); + + const form = await testUtils.createView({ + actionMenusRegistry: true, + arch: + `
    + + `, + data: this.data, + model: 'partner', + res_id: 1, + View: FormView, + viewOptions: { + hasActionMenus: true, + ids: [1, 2], + index: 0, + }, + }); + + assert.containsNone(form, ".o_cp_action_menus .o_embed_menu"); + + form.destroy(); + }); + + QUnit.test('click on the google drive attachments after switching records', async function (assert) { + assert.expect(4); + + let currentRecordId = 1; + const form = await testUtils.createView({ + actionMenusRegistry: true, + arch: + `
    + + `, + data: this.data, + async mockRPC(route, args) { + switch (route) { + case '/web/dataset/call_kw/google.drive.config/get_google_drive_config': + assert.deepEqual(args.args, ['partner', currentRecordId], + 'The route to get google drive config should have been called'); + return [{ + id: 27, + name: 'Cyberdyne Systems', + }]; + case '/web/dataset/call_kw/google.drive.config/search_read': + return [{ + google_drive_resource_id: "T1000", + google_drive_client_id: "cyberdyne.org", + id: 1, + }]; + case '/web/dataset/call_kw/google.drive.config/get_google_drive_url': + assert.deepEqual(args.args, [27, currentRecordId, 'T1000'], + 'The route to get the Google url should have been called'); + return; // do not return anything or it will open a new tab. + } + }, + model: 'partner', + res_id: 1, + View: FormView, + viewOptions: { + hasActionMenus: true, + ids: [1, 2], + index: 0, + }, + }); + + await cpHelpers.toggleActionMenu(form); + await cpHelpers.toggleMenuItem(form, "Cyberdyne Systems"); + + currentRecordId = 2; + await cpHelpers.pagerNext(form); + + await cpHelpers.toggleActionMenu(form); + await cpHelpers.toggleMenuItem(form, "Cyberdyne Systems"); + + form.destroy(); + }); + }); +}); diff --git a/addons/google_drive/static/tests/mock_server.js b/addons/google_drive/static/tests/mock_server.js new file mode 100644 index 00000000..5fb190e3 --- /dev/null +++ b/addons/google_drive/static/tests/mock_server.js @@ -0,0 +1,22 @@ +odoo.define('google_drive.MockServer', function (require) { + 'use strict'; + + var MockServer = require('web.MockServer'); + + MockServer.include({ + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @override + * @private + */ + async _performRpc(route, args) { + if (args.method === 'get_google_drive_config') { + return []; + } + return this._super(...arguments); + }, + }); +}); -- cgit v1.2.3