2017-06-23 64 views
10

Tôi đang sử dụng odoo 10 enterpeise. Tôi muốn hiển thị các nút cho những người dùng cụ thể không theo nhóm vì sẽ có nhiều người dùng trong một nhóm và tôi muốn chỉ hiển thị nút bên dưới có quyền từ chối/phê duyệt một đối tượng. Đây là nútOdoo - Ẩn nút cho người dùng cụ thể

<xpath expr="//sheet" position="before"> 
      <header> 
      <button name="update_approve" attrs="{'invisible':[('first_approve', '=', uid)]}" string="Approve" type="object" class="oe_highlight"/> 
       <button name="update_reject" attrs="{'invisible':[('second_approve', '=', uid)]}" string="Reject" type="object" class="btn-danger"/> 
      </header> 
     </xpath> 

tôi đã cố gắng để làm điều này bằng uid nhưng uid không có sẵn trong xml

first_approvesecond_approve là các trường trong mô hình của tôi trên cơ sở đó tôi muốn hiển thị nút duy nhất cho người dùng mà được chỉ định trong first_approve/second_approve

Trả lời

1

Tôi đã không thử điều này tôi hy vọng nó hoạt động.

Một trong những điều tôi biết để sử dụng trường trong attrs trường phải được đề cập trong biểu mẫu. tôi không biết cách lấy giá trị của id người dùng trong biểu mẫu. nhưng nếu không có cách ngắn như uid hoặc user bạn có thể làm việc vất vả, chỉ cần tạo trường m2o để res.users làm trường tính toán trường này bằng store = False.

# by default store = False this means the value of this field 
    # is always computed. 
    current_user = fields.Many2one('res.users', compute='_get_current_user') 

    @api.depends() 
    def _get_current_user(self): 
     for rec in self: 
      rec.current_user = self.env.user 
     # i think this work too so you don't have to loop 
     self.update({'current_user' : self.env.user.id}) 

và bạn có thể sử dụng trường này trong biểu mẫu của mình.

<xpath expr="//sheet" position="before"> 
       <header> 
       <!-- fin a good place for the field if i make the header look ugly --> 
       <!-- make invisible --> 
       <field name="current_user" invisible="1"/> 
                        <!-- hope it work like this --> 
       <button name="update_approve" attrs="{'invisible':[('first_approve', '=', current_user)]}" string="Approve" type="object" class="oe_highlight"/> 
       <button name="update_reject" attrs="{'invisible':[('second_approve', '=', current_user)]}" string="Reject" type="object" class="btn-danger"/> 
       </header> 
    </xpath> 

xin lỗi vì tiếng anh của tôi.

1

Tôi hiểu rằng bạn có danh sách người dùng cố định sẽ có thể phê duyệt và danh sách người dùng cố định khác có thể từ chối. Mặc dù là một vài người dùng, tôi sẽ tạo hai nhóm và sử dụng groups thuộc tính trên các nút của bạn, nhưng nếu ngay cả như vậy bạn không muốn tạo ra một vài nhóm cho họ, bạn có thể làm điều này:

from openerp import models, api 
import json 
from lxml import etree 

FIRST_APPROVE = [] # Fill this list with the IDs of the users who can update approve 
SECOND_APPROVE = [] # Fill this list with the IDs of the users who can update reject 

class YourClass(models.Model): 
    _inherit = 'your.class' 

    def update_json_data(self, json_data=False, update_data={}): 
     ''' It updates JSON data. It gets JSON data, converts it to a Python 
     dictionary, updates this, and converts the dictionary to JSON data 
     again. ''' 
     dict_data = json.loads(json_data) if json_data else {} 
     dict_data.update(update_data) 
     return json.dumps(dict_data, ensure_ascii=False) 

    def set_modifiers(self, element=False, modifiers_upd={}): 
     ''' It updates the JSON modifiers with the specified data to indicate 
     if a XML tag is readonly or invisible or not. ''' 
     if element is not False: # Do not write only if element: 
      modifiers = element.get('modifiers') or {} 
      modifiers_json = self.update_json_data(
       modifiers, modifiers_upd) 
      element.set('modifiers', modifiers_json) 

    @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) 

     doc = etree.XML(res['arch']) 

     if view_type == 'form': 
      if self.env.uid in FIRST_APPROVE: 
       upd_approve_btn_search = doc.xpath("//button[@name='update_approve']") 
       upd_approve_btn = upd_approve_btn_search[0] \ 
        if upd_approve_btn_search else False 
       if upd_approve_btn: 
        self.set_modifiers(upd_approve_btn, {'invisible': False, }) 

      if self.env.uid in SECOND_APPROVE: 
       upd_reject_btn_search = doc.xpath("//button[@name='update_reject']") 
       upd_reject_btn = upd_reject_btn_search[0] \ 
        if upd_reject_btn_search else False 
       if upd_reject_btn: 
        self.set_modifiers(upd_reject_btn, {'invisible': False, }) 

     res['arch'] = etree.tostring(doc) 
     return res 

FIRST APPROVESECOND_APPROVE sẽ là const trong đó bạn phải giới thiệu ID cố định của người dùng có thể thực hiện hành động tương ứng (ví dụ: FIRST APPROVE = [2, 7, 9]).

YourClass phải là lớp mà bạn đã khai báo phương pháp cho các nút của bạn (một trong đó bạn đã khai báo update_approveupdate_reject).

QUAN TRỌNG: với mã này, các nút của bạn phải luôn luôn vô hình (viết invisible="1" tại view XML của bạn), bởi vì sau khi tải mã XML, các fields_view_get sẽ ghi đè lên giá trị invisible thiết 0.

này là một cách không phổ biến để quản lý mục đích của bạn, nhưng tiếc là tôi nghĩ đó là cách đơn giản nhất nếu bạn không muốn tạo nhóm. Tôi hy vọng nó sẽ giúp bạn và những người dùng khác!

Các vấn đề liên quan