This question has been flagged
5 Replies
8612 Views

I want to hide some fields for group_stock_manager and make visible the field to group_stock_user and administrator.

If i use this code  

<field name = "operation_ids" groups = "base.group_erp_manager">

only administrator view that field and not visible for both the stock manager and stock user .I am also changing the the code as

<field name ="operation_ids" groups ="!stock.group_stock_manager>

This can be visible the field only for stock user. Because,Administrator is one of the stock manager.

How I achieve when both the administrator and stock user seen that field except stock manager?

Avatar
Discard
Best Answer

#I have not known your model, so i have used "mrp" model

# -*- coding: utf-8 -*-
from odoo import api, fields, models, exceptions
from odoo import SUPERUSER_ID

class Test(models.Model):

    _inherit = 'mrp.routing'
    

    inv = fields.Boolean(string="Invisible", compute="c_inv", store=False)

    @api.one
    def c_inv(self):
        user = self.env['res.users'].browse(self.env.uid)
        group_stock_manager = user.has_group('stock.group_stock_manager')

        group_user = user.has_group('base.group_user')
        if SUPERUSER_ID == self.env.uid or group_user == True:
            self.inv = False

        else:
            self.inv = True


<?xml version="1.0" encoding="UTF-8" ?>
<odoo>

    <data>

        <record id="inherit_mrp.routing_form" model="ir.ui.view">
            <field name="name">mrp.routing.form</field>
            <field name="model">mrp.routing</field>
            <field name="inherit_id" ref="mrp.mrp_routing_form_view"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='company_id']" position="after">
                    <field name="inv" invisible="True"/>
                </xpath>
                <xpath expr="//field[@name='company_id']" position="attributes">
                    <attribute name="attrs">{'invisible': [('inv','=',True)]}</attribute>
                </xpath>
            </field>
        </record>

    </data>

</odoo>

#I have not test it

Avatar
Discard
Best Answer

Simple way is create new group inherit with group_stock_user.
<field name = "operation_ids" groups = "new group">
new group users only can view

Avatar
Discard
Best Answer

Hi,

You can achieve this by overriding fields_view_get function. Like this : 
First you need to import SUPERUSER_ID like this:

from odoo import SUPERUSER_ID 
@api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        res = super(YourClass, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar,
                                                    submenu=submenu)
        if view_type == 'form':
            doc = etree.XML(res['arch'])
           user = self.env.user
            for node in doc.xpath("//field[@name='YOUR_FIELD']"):
                if user.has_group('MODEL.group_stock_manager ') and user.id != SUPERUSER_ID:
                    node.set('invisible', "1")
            res['arch'] = etree.tostring(doc)
        return res


Thanks.

Avatar
Discard
Best Answer

The idea is create a computed boolean field and make that field TRUE for particular user group and use that to visible/invisible your fields.

Reference:

1- http://learnopenerp.blogspot.com/2016/10/how-to-visible-and-invisible-fields-in.html

2- http://learnopenerp.blogspot.com/2017/10/how-to-check-login-user-group-in-odoo.html

Avatar
Discard