Manage webservice from odoo

2019-08-28 05:58发布

问题:

I am new to Odoo 11, I have created a module called 'coupon', for this module I have created a security group and a default user that is added to this group:

        <record id="default_coupon_user" model="res.users">
            <field name="login">couponuser</field>
            <field name="password">couponuser</field>
            <field name="password_crypt">couponuser</field>
            <field name="name">Default User Coupon</field>
            <field name="display_name">Default Coupon User</field>
            <field name="customer">False</field>
        </record>

        <record id="coupon_managers_group" model="res.groups" >
            <field name="name">Coupon Managers Group</field>
            <field name="comment">Coupon Managers Group that will be used in the Coupon microservice.</field>
            <field name="category_id" ref="base.module_category_coupon"/>
            <field name="users" eval="[(4, ref('coupon.default_coupon_user'))]"/>
        </record>

My module depends on website because I need to create a website, for that site I created an index page:

        <!-- === Coupon Page  === -->
        <template id="index_template">
            <t t-call="website.layout">
                <div id="wrap">
                    <div class="container">
                        <h1>Coupons</h1>
                        <a href="/payum_coupon/create" class="btn btn-primary btn-lg">
                            Add
                        </a>
                        <p>paginas:
                            <t t-esc="coupons['pg']"/>
                        </p>
                        <p>total:
                            <t t-esc="coupons['total']"/>
                        </p>
                        <ul class="clientes">
                            <t t-foreach="coupons['items']" t-as="i">
                                <li>
                                    <a t-attf-href="/payum_coupon/{{i['email']}}">
                                        <t t-esc="i['firstName']"/>
                                        <t t-esc="i['lastName']"/>
                                        -
                                        <t t-esc="i['email']"/>
                                    </a>
                                </li>
                            </t>
                        </ul>
                    </div>
                </div>
            </t>
        </template>

        <record id="coupon_page" model="website.page">
            <field name="name">Index Coupon page</field>
            <field name="website_published">True</field>
            <field name="url">/coupon</field>
            <field name="view_id" ref="index_template"/>
            <field name="groups">coupon.coupon_managers_group</field>
        </record>

and website.menu with the following code:

     <record id="coupon_page_link" model="website.menu">
        <field name="name">Coupon</field>
        <field name="page_id" ref="coupon_page"/>
        <field name="parent_id" ref="website.main_menu"/>
    </record>

the python code in the controller that will be executed by said menu is this:

@http.route('/coupon', auth='user', website=True)
def index(self, **kw):
    #<<my code here>>

I need several things:

  1. when the 'index' page is displayed if the user in session is not in the 'coupon.coupon_managers_group' group then the menu is not shown

  2. And that when the controller method '/coupon' is executed it is verified that the user in session is in the group 'coupon.coupon_managers_group'

This site has a particularity and that my module does not have models, since it is to manage the data of an api rest application, that is, in odoo I have to create the views of list, create, etc., but the data is written and they are read from a remote web service.

I have searched many sites on the internet but I have not found anything, as they always refer to local cases in odoo.

回答1:

To hide the website menu based on the user group you could do something like this:

from odoo import api, models

class Menu(models.Model):
    _inherit = "website.menu"

    @api.one
    def _compute_visible(self):
        if self.clean_url() == '/coupon' and not self.env.user.has_group('coupon.coupon_managers_group'):
            return False
        return super(Menu, self)._compute_visible()

In the controller of the route /coupon you could check the user group like:

request.env.user.has_group('coupon.coupon_managers_group')