openerp学习笔记 domain 增加扩展支持,例如支持 [('type','=','get_user_ht_type()')]

示例代码1,ir_action_window.read :

# -*- coding: utf-8 -*-
from openerp.osv import fields,osv

class res_users(osv.osv):
   
_name = 'res.users'
    _inherit = 'res.users'

    _columns =
{
               
'ht_type' :
fields.selection([
               
('makings',u'材料'),
               
('theme',u'主题'),
               
('decorate',u'装修')
               
],u'类型'),
               
}

#获取用户具有审核权限的合同类型   
   
def _get_ht_type(self, cr, uid,
context=None):
        return
self.browse(cr, uid, uid,
context=context).ht_type
       

res_users()

# 为 domain 增加扩展支持,使用 get_user_ht_type() 获取用户具有审核权限的合同类型
class
ir_action_window(osv.osv):
    _inherit =
'ir.actions.act_window'

    def read(self, cr, uid, ids, fields=None, context=None,
load='_classic_read'):

        if context
is None:
           
context = {}
        obj_user =
self.pool.get('res.users')
        select
= ids
        if isinstance(ids, (int,
long)):
           
select = [ids]
        res =
super(ir_action_window, self).read(cr, uid, select, fields=fields,
context=context, load=load)
        for r
in res:
           
mystring =
'get_user_ht_type()'

           
if mystring in (r.get('domain', '[]') or
''):

               
r['domain'] = r['domain'].replace(mystring, str(obj_user._get_ht_type(cr,
uid)))

        if isinstance(ids, (int,
long)):
            if
res:
               
return
res[0]
           
else:
               
return False
        return res

ir_action_window()

视图中调用:

<field name="domain">[('type','=','get_user_ht_type()')]</field>

其它示例代码:

 # -*- coding: utf-8 -*-

 from openerp.osv import fields, osv
from openerp import tools class hr_department(osv.osv):
def name_get(self, cr, uid, ids, context=None):
if context is None:
context = {}
if not ids:
return []
reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
res = []
for record in reads:
name = record['name']
if record['parent_id']:
name = record['parent_id'][1]+' / '+name
res.append((record['id'], name))
return res def _dept_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
res = self.name_get(cr, uid, ids, context=context)
return dict(res) _name = "hr.department"
_columns = {
'name': fields.char('Department Name', size=64, required=True),
'complete_name': fields.function(_dept_name_get_fnc, type="char", string='Name'),
'company_id': fields.many2one('res.company', 'Company', select=True, required=False),
'parent_id': fields.many2one('hr.department', 'Parent Department', select=True),
'child_ids': fields.one2many('hr.department', 'parent_id', 'Child Departments'),
'note': fields.text('Note'),
} _defaults = {
'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.department', context=c),
} def _get_members(self, cr, uid, context=None):
mids = self.search(cr, uid, [('manager_id', '=', uid)], context=context)
result = {uid: 1}
for m in self.browse(cr, uid, mids, context=context):
for user in m.member_ids:
result[user.id] = 1
return result.keys() def _check_recursion(self, cr, uid, ids, context=None):
if context is None:
context = {}
level = 100
while len(ids):
cr.execute('select distinct parent_id from hr_department where id IN %s',(tuple(ids),))
ids = filter(None, map(lambda x:x[0], cr.fetchall()))
if not level:
return False
level -= 1
return True _constraints = [
(_check_recursion, 'Error! You cannot create recursive departments.', ['parent_id'])
] hr_department() class ir_action_window(osv.osv):
_inherit = 'ir.actions.act_window' def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
if context is None:
context = {}
obj_dept = self.pool.get('hr.department')
select = ids
if isinstance(ids, (int, long)):
select = [ids]
res = super(ir_action_window, self).read(cr, uid, select, fields=fields, context=context, load=load)
for r in res:
mystring = 'department_users_get()'
if mystring in (r.get('domain', '[]') or ''):
r['domain'] = r['domain'].replace(mystring, str(obj_dept._get_members(cr, uid)))
if isinstance(ids, (int, long)):
if res:
return res[0]
else:
return False
return res ir_action_window() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 # -*- encoding: utf-8 -*-

 from osv import osv, fields

 class partner_category(osv.osv):
_inherit = 'res.partner.category'
_columns = {
'company_id': fields.many2one('res.company', 'Company',select=1, help="The \"Company\" field is used to filter data in the \"Company Partners\" view.")
} def _get_company(self, cr, uid, id, context=None):
if context is None:
context={}
return self.pool.get('res.partner.category').browse(cr, uid, id, context).company_id.id def _default_company(self, cr, uid, context=None):
if context is None:
context={}
return self.pool.get('res.users').browse(cr, uid, uid, context).company_id.id def onchange_parent_id(self, cr, uid, ids, parent_id, context=None):
if context is None:
context={} v={} if parent_id:
parent_company = self._get_company(cr, uid, parent_id, context)
if parent_company:
v['company_id'] = parent_company
return {'value': v} _defaults = {
'company_id': _default_company,
}
partner_category() class ir_action_window(osv.osv):
_inherit = 'ir.actions.act_window' def read(self, cr, uid, ids, fields=None, context=None,
load='_classic_read'):
select = ids
if isinstance(ids, (int, long)):
select = [ids]
res = super(ir_action_window, self).read(cr, uid, select, fields=fields,
context=context, load=load)
for r in res:
mystring = 'get_company_categories()'
user_obj = self.pool.get('res.users').browse(cr, uid, uid)
if mystring in (r.get('domain', '[]') or ''):
r['domain'] = r['domain'].replace(mystring, str(
self.pool.get('res.partner.category').search(cr, uid, [('company_id', '=', user_obj.company_id.id)])))
if isinstance(ids, (int, long)):
if res:
return res[0]
else:
return False
return res ir_action_window()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

视图中调用:

        <record id="action_company_partners_form" model="ir.actions.act_window">
<field name="name">Company Partners</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.partner</field>
<field name="view_type">form</field>
<field name="domain">[('category_id', 'in', get_company_categories())]</field>
</record>

示例代码2,self.search

This is an example.
Look to USER_PARTNER_IDS and USER_PAYMENT_MODE_IDS.

Part of example.xml:
    <record id="action_income_payment_line_tree" model="ir.actions.act_window">
      <field name="name">Входящие платежи</field>
      <field name="res_model">payment.line</field>
      <field name="view_type">form</field>
      <field name="view_mode">tree,form</field>
      <field name="limit">300</field>
      <field name="context">{"search_default_today":1}</field>
      <field name="domain">[('partner_id','in','USER_PARTNER_IDS')]</field>
      <field name="search_view_id" ref="view_payment_line_search"/>
    </record>

<record id="action_outgoing_payment_line_tree" model="ir.actions.act_window">
      <field name="name">Исходящие платежи</field>
      <field name="res_model">payment.line</field>
      <field name="view_type">form</field>
      <field name="view_mode">tree,form</field>
      <field name="limit">300</field>
      <field name="context">{"search_default_today":1}</field>
      <field name="domain">[('payment_mode','in','USER_PAYMENT_MODE_IDS')]</field>
      <field name="search_view_id" ref="view_payment_line_search"/>
    </record>

Part of example.py:
    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False, xtra=None):
        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
        new_args = []
        for arg in args:
            if type(arg) is not tuple:
                new_args += arg
                continue
            if arg[2] == 'USER_PARTNER_IDS':
                new_args += [(arg[0], arg[1], [p.id for p in user.partner_ids])]
            elif arg[2] == 'USER_PAYMENT_MODE_IDS':
                new_args += [(arg[0], arg[1], [m.id for m in user.payment_mode_ids])]
            else:
                new_args += [arg]
        return super(payment_line, self).search(cr, uid, new_args, offset, limit, order, context, count)

上一篇:java I/O流


下一篇:2016.6.23 PHP实现新闻发布系统主体部分