2017-06-23 14 views
10

odoo 10 enterpeiseを使用しています。グループには多くのユーザーがいて、オブジェクトを拒否/承認することはできません。 ここで私はuidを使用してこれを実行しようとしたボタンOdoo - 特定のユーザーのボタンを隠す

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

ですが、uidが

first_approvesecond_approve iはユーザーのみにボタンを表示したいかに基づいて、私のモデルのフィールドですXMLでは利用できませんfirst_approve/second_approve

答えて

1

私はこれがうまくいかないか試してみました。

私がattrsのフィールドを使用することを知っていることの1つは、フィールドにフォームの覚書が必要です。 フォームのユーザーIDの値を取得する方法がわかりません。しかし、短い のような場合uidまたはuserのようにすることができますarroundこれを行うには、単にres.usersにm2oフィールドを作成 は、このフィールドは、店舗= 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}) 

このフィールドは、フォーム内で使用できます。

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

ごめんなさい、私の英語です。

1

私はあなたが拒否できるユーザーの他の固定リストと承認できるユーザーの固定リストを持っていることを理解しました。少数のユーザーであるにもかかわらず、私は2つのグループを作成し、あなたのボタンの上にgroups属性を使用しますが、あなたは彼らのためにグループのカップルを作成したくないでももしそうなら、あなたはこれを行うことができます:

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は、それぞれの操作を実行できるユーザーの固定IDS(たとえば、FIRST APPROVE = [2, 7, 9])を導入する必要があります。

YourClassは、ボタンのメソッドを宣言したクラス(update_approveupdate_rejectと宣言したクラス)でなければなりません。

重要:XMLコードをロードした後、fields_view_getは0

これを設定するinvisible値が上書きされますので、このコードで、あなたのボタンは、(あなたのXMLビューでinvisible="1"を書く)は常に目に見えないでなければなりませんあなたの目的を管理する珍しい方法ですが、残念ながらグループを作成したくない場合は、最も簡単な方法だと思います。私はあなたと他のユーザーに役立つことを願っています!

関連する問題