← Back to team overview

openerp-community-reviewer team mailing list archive

[Merge] lp:~sylvain-legal/web-addons/7.0-web_easy_switch_company into lp:web-addons

 

Sylvain LE GAL (GRAP) has proposed merging lp:~sylvain-legal/web-addons/7.0-web_easy_switch_company into lp:web-addons.

Requested reviews:
  Guewen Baconnier @ Camptocamp (gbaconnier-c2c)
  Pedro Manuel Baeza (pedro.baeza): code review and test
  arthru (arthru): tested, no-review

For more details, see:
https://code.launchpad.net/~sylvain-legal/web-addons/7.0-web_easy_switch_company/+merge/210289

Add a new module 'web_easy_switch_company'. 

This module allow a user to switch from a company to another more easily. (with just 2 mouse click & without warning message).

-- 
https://code.launchpad.net/~sylvain-legal/web-addons/7.0-web_easy_switch_company/+merge/210289
Your team Web-Addons Core Editors is subscribed to branch lp:web-addons.
=== added directory 'web_easy_switch_company'
=== added file 'web_easy_switch_company/__init__.py'
--- web_easy_switch_company/__init__.py	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/__init__.py	2014-04-02 09:20:18 +0000
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    Web Easy Switch Company module for OpenERP
+#    Copyright (C) 2014 GRAP (http://www.grap.coop)
+#    @author Sylvain LE GAL (https://twitter.com/legalsylvain)
+#
+#    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 . import model
+from . import controllers

=== added file 'web_easy_switch_company/__openerp__.py'
--- web_easy_switch_company/__openerp__.py	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/__openerp__.py	2014-04-02 09:20:18 +0000
@@ -0,0 +1,71 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    Web Easy Switch Company module for OpenERP
+#    Copyright (C) 2014 GRAP (http://www.grap.coop)
+#    @author Sylvain LE GAL (https://twitter.com/legalsylvain)
+#
+#    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': 'Multicompany - Easy Switch Company',
+    'version': '1.0',
+    'category': 'web',
+    'description': """
+Add menu to allow user to switch to another company more easily
+===============================================================
+
+Functionality:
+--------------
+    * Add a new menu in the top bar to switch to another company more easily;
+    * Remove the old behaviour to switch company;
+
+Documentations:
+---------------
+    * Video : http://www.youtube.com/watch?v=Cpm6dg-IEQQ
+
+Technical information:
+----------------------
+    * Create a field function 'logo_topbar' in res_company to have a good"""
+    """resized logo;
+
+Limits:
+-------
+    * It would be interesting to show the structure of the companies;
+
+Copyright, Author and Licence:
+------------------------------
+    * Copyright: 2014, Groupement Régional Alimentaire de Proximité;
+    * Author: Sylvain LE GAL (https://twitter.com/legalsylvain);
+    * Licence: AGPL-3 (http://www.gnu.org/licenses/)""",
+    'author': 'GRAP',
+    'website': 'http://www.grap.coop',
+    'license': 'AGPL-3',
+    'depends': [
+        'web',
+    ],
+    'data': [
+        'view/res_users_view.xml',
+    ],
+    'js': [
+        'static/src/js/switch_company.js',
+    ],
+    'qweb': [
+        'static/src/xml/switch_company.xml',
+    ],
+    'installable': True,
+    'auto_install': False,
+}

=== added directory 'web_easy_switch_company/controllers'
=== added file 'web_easy_switch_company/controllers/__init__.py'
--- web_easy_switch_company/controllers/__init__.py	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/controllers/__init__.py	2014-04-02 09:20:18 +0000
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    Web Easy Switch Company module for OpenERP
+#    Copyright (C) 2014 GRAP (http://www.grap.coop)
+#    @author Sylvain LE GAL (https://twitter.com/legalsylvain)
+#
+#    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 . import main

=== added file 'web_easy_switch_company/controllers/main.py'
--- web_easy_switch_company/controllers/main.py	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/controllers/main.py	2014-04-02 09:20:18 +0000
@@ -0,0 +1,31 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    Web Easy Switch Company module for OpenERP
+#    Copyright (C) 2014 GRAP (http://www.grap.coop)
+#    @author Sylvain LE GAL (https://twitter.com/legalsylvain)
+#
+#    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/>.
+#
+##############################################################################
+
+import openerp
+
+
+class WebEasySwitchCompanyController(openerp.addons.web.http.Controller):
+    _cp_path = '/web_easy_switch_company/switch'
+
+    @openerp.addons.web.http.jsonrequest
+    def change_current_company(self, req, company_id):
+        req.session.model('res.users').change_current_company(company_id)

=== added directory 'web_easy_switch_company/model'
=== added file 'web_easy_switch_company/model/__init__.py'
--- web_easy_switch_company/model/__init__.py	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/model/__init__.py	2014-04-02 09:20:18 +0000
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    Web Easy Switch Company module for OpenERP
+#    Copyright (C) 2014 GRAP (http://www.grap.coop)
+#    @author Sylvain LE GAL (https://twitter.com/legalsylvain)
+#
+#    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 . import res_users
+from . import res_company

=== added file 'web_easy_switch_company/model/res_company.py'
--- web_easy_switch_company/model/res_company.py	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/model/res_company.py	2014-04-02 09:20:18 +0000
@@ -0,0 +1,57 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    Web Easy Switch Company module for OpenERP
+#    Copyright (C) 2014 GRAP (http://www.grap.coop)
+#    @author Sylvain LE GAL (https://twitter.com/legalsylvain)
+#
+#    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 openerp.osv import fields
+from openerp.osv.orm import Model
+from openerp.tools import image_resize_image
+
+
+class res_company(Model):
+    _inherit = 'res.company'
+
+    # Custom Section
+    def _switch_company_get_companies_from_partner(
+            self, cr, uid, ids, context=None):
+        return self.pool['res.company'].search(
+            cr, uid, [('partner_id', 'in', ids)], context=context)
+
+    # Fields function Section
+    def _get_logo_topbar(self, cr, uid, ids, _field_name, _args, context=None):
+        result = dict.fromkeys(ids, False)
+        for record in self.browse(cr, uid, ids, context=context):
+            size = (48, 48)
+            result[record.id] = image_resize_image(
+                record.partner_id.image, size)
+        return result
+
+    # Columns Section
+    _columns = {
+        'logo_topbar': fields.function(
+            _get_logo_topbar,
+            string="Logo displayed in the switch company menu",
+            type="binary", store={
+                'res.company': (lambda s, c, u, i, x: i, ['partner_id'], 10),
+                'res.partner': (_switch_company_get_companies_from_partner,
+                                ['image'], 10),
+            }
+        ),
+    }

=== added file 'web_easy_switch_company/model/res_users.py'
--- web_easy_switch_company/model/res_users.py	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/model/res_users.py	2014-04-02 09:20:18 +0000
@@ -0,0 +1,31 @@
+# -*- encoding: utf-8 -*-
+##############################################################################
+#
+#    Web Easy Switch Company module for OpenERP
+#    Copyright (C) 2014 GRAP (http://www.grap.coop)
+#    @author Sylvain LE GAL (https://twitter.com/legalsylvain)
+#
+#    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 openerp.osv.orm import Model
+
+
+class res_users(Model):
+    _inherit = 'res.users'
+
+    # Custom Function Section
+    def change_current_company(self, cr, uid, company_id, context=None):
+        return self.write(cr, uid, uid, {'company_id': company_id})

=== added directory 'web_easy_switch_company/static'
=== added directory 'web_easy_switch_company/static/src'
=== added directory 'web_easy_switch_company/static/src/img'
=== added file 'web_easy_switch_company/static/src/img/icon.png'
Binary files web_easy_switch_company/static/src/img/icon.png	1970-01-01 00:00:00 +0000 and web_easy_switch_company/static/src/img/icon.png	2014-04-02 09:20:18 +0000 differ
=== added file 'web_easy_switch_company/static/src/img/selection-off.png'
Binary files web_easy_switch_company/static/src/img/selection-off.png	1970-01-01 00:00:00 +0000 and web_easy_switch_company/static/src/img/selection-off.png	2014-04-02 09:20:18 +0000 differ
=== added file 'web_easy_switch_company/static/src/img/selection-on.png'
Binary files web_easy_switch_company/static/src/img/selection-on.png	1970-01-01 00:00:00 +0000 and web_easy_switch_company/static/src/img/selection-on.png	2014-04-02 09:20:18 +0000 differ
=== added directory 'web_easy_switch_company/static/src/js'
=== added file 'web_easy_switch_company/static/src/js/switch_company.js'
--- web_easy_switch_company/static/src/js/switch_company.js	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/static/src/js/switch_company.js	2014-04-02 09:20:18 +0000
@@ -0,0 +1,137 @@
+/******************************************************************************
+    Web Easy Switch Company module for OpenERP
+    Copyright (C) 2014 GRAP (http://www.grap.coop)
+    @author Sylvain LE GAL (https://twitter.com/legalsylvain)
+
+    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/>.
+******************************************************************************/
+
+openerp.web_easy_switch_company = function (instance) {
+
+    /***************************************************************************
+    Create an new 'SwitchCompanyWidget' widget that allow users to switch 
+    from a company to another more easily.
+    ***************************************************************************/
+    instance.web.SwitchCompanyWidget = instance.web.Widget.extend({
+
+        template:'web_easy_switch_company.SwitchCompanyWidget',
+
+        /***********************************************************************
+        Overload section 
+        ***********************************************************************/
+
+        /**
+         * Overload 'init' function to initialize the values of the widget.
+         */
+        init: function(parent){
+            this._super(parent);
+            this.companies = [];
+            this.current_company_id = 0;
+            this.current_company_name = '';
+        },
+
+        /**
+         * Overload 'start' function to load datas from DB.
+         */
+        start: function () {
+            this._super();
+            this._load_data();
+        },
+
+        /**
+         * Overload 'renderElement' function to set events on company items.
+         */
+        renderElement: function() {
+            var self = this;
+            this._super();
+            if (this.companies.length === 1) {
+                this.$el.hide();
+            }
+            else{
+                this.$el.show();
+                this.$el.find('.easy_switch_company_company_item').on('click', function(ev) {
+                    var company_id = $(ev.target).data("company-id");
+                    if (company_id != self.current_company_id){
+                        var func = '/web_easy_switch_company/switch/change_current_company';
+                        var param = {'company_id': company_id}
+                        self.rpc(func, param).done(function(res) {
+                            window.location.reload()
+                        });
+                    }
+                });
+            }
+        },
+
+
+        /***********************************************************************
+        Custom section 
+        ***********************************************************************/
+
+        /**
+         * helper function to load data from the server
+         */
+        _fetch: function(model, fields, domain, ctx){
+            return new instance.web.Model(model).query(fields).filter(domain).context(ctx).all();
+        },
+
+        /**
+         * - Load data of the companies allowed to the current users;
+         * - Launch the rendering of the current widget;
+         */
+        _load_data: function(){
+            var self = this;
+            // Request for current users information
+            this._fetch('res.users',['company_id','company_ids'],[['id','=',this.session.uid]]).then(function(res_users){
+                self.current_company_id = res_users[0].company_id[0];
+                self.current_company_name = res_users[0].company_id[1];
+                // Request for other companies
+                self._fetch('res.company',['name',],[['id','in', res_users[0].company_ids]]).then(function(res_company){
+                    for ( var i=0 ; i < res_company.length; i++) {
+                        res_company[i]['logo_topbar'] = self.session.url(
+                            '/web/binary/image', {
+                                model:'res.company', 
+                                field: 'logo_topbar', 
+                                id: res_company[i].id
+                            });
+                        if (res_company[i].id == self.current_company_id){
+                            res_company[i]['logo_state'] = '/web_easy_switch_company/static/src/img/selection-on.png';
+                        }
+                        else{
+                            res_company[i]['logo_state'] = '/web_easy_switch_company/static/src/img/selection-off.png';
+                        }
+                        self.companies.push(res_company[i]);
+                    }
+                    // Update rendering
+                    self.renderElement();
+                });
+            });
+        },
+
+    });
+
+    /***************************************************************************
+    Extend 'UserMenu' Widget to insert a 'SwitchCompanyWidget' widget.
+    ***************************************************************************/
+    instance.web.UserMenu =  instance.web.UserMenu.extend({
+
+        init: function(parent) {
+            this._super(parent);
+            var switch_button = new instance.web.SwitchCompanyWidget();
+            switch_button.appendTo(instance.webclient.$el.find('.oe_systray'));
+        },
+
+    });
+
+};
+

=== added directory 'web_easy_switch_company/static/src/xml'
=== added file 'web_easy_switch_company/static/src/xml/switch_company.xml'
--- web_easy_switch_company/static/src/xml/switch_company.xml	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/static/src/xml/switch_company.xml	2014-04-02 09:20:18 +0000
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- ********************************************************************** -->
+<!--                                                                        -->
+<!--Web Easy Switch Company module for OpenERP                              -->
+<!--Copyright (C) 2014 GRAP (http://www.grap.coop)                          -->
+<!--@author Sylvain LE GAL (https://twitter.com/legalsylvain)               -->
+
+<!--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/>.   -->
+<!-- ********************************************************************** -->
+<template>
+
+    <t t-name="web_easy_switch_company.SwitchCompanyWidget">
+        <span class="oe_user_menu oe_topbar_item oe_dropdown_toggle oe_dropdown_arrow">
+            <span>
+                <t t-esc="widget.current_company_name"/>
+            </span>
+            <ul class="oe_dropdown_menu">
+                <t t-foreach="widget.companies" t-as="company">
+                    <li>
+                        <a class="easy_switch_company_company_item" 
+                                href="#" 
+                                t-att-data-company-id="company.id">
+                            <img class="oe_topbar_avatar" t-att-src="company.logo_topbar"/>
+                            <img class="oe_topbar_avatar" t-att-src="company.logo_state"/>
+                            <t t-esc="company.name"/>
+                        </a>
+                    </li>
+                </t>
+            </ul>
+        </span>
+    </t>
+
+</template>
+

=== added directory 'web_easy_switch_company/view'
=== added file 'web_easy_switch_company/view/res_users_view.xml'
--- web_easy_switch_company/view/res_users_view.xml	1970-01-01 00:00:00 +0000
+++ web_easy_switch_company/view/res_users_view.xml	2014-04-02 09:20:18 +0000
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<openerp>
+    <data>
+
+        <record id="view_res_users_simple_modif_form" model="ir.ui.view">
+            <field name="name">res.users.form</field>
+            <field name="model">res.users</field>
+            <field name="inherit_id" ref="base.view_users_form_simple_modif"/>
+            <field name="arch" type="xml">
+                <!-- hide old behaviour to change company -->
+                <field name="company_id" position="replace" />
+            </field>
+        </record>
+    </data>
+</openerp>


Follow ups