Browse Source

[RFR] new api/views

pull/113/head
Holger Brunn 10 years ago
parent
commit
62e4a72bdd
  1. 66
      auth_dynamic_groups/model/res_groups.py
  2. 20
      auth_dynamic_groups/view/res_groups.xml

66
auth_dynamic_groups/model/res_groups.py

@ -19,59 +19,45 @@
#
##############################################################################
import logging
from openerp.osv.orm import Model
from openerp.osv import fields
from openerp import models, fields, api, exceptions
from openerp.tools.safe_eval import safe_eval
from openerp import SUPERUSER_ID
from openerp import SUPERUSER_ID, _
class res_groups(Model):
class res_groups(models.Model):
_inherit = 'res.groups'
_columns = {
'is_dynamic': fields.boolean('Dynamic'),
'dynamic_group_condition': fields.text(
'Condition', help='The condition to be met for a user to be a '
'member of this group. It is evaluated as python code at login '
'time, you get `user` passed as a browse record')
}
is_dynamic = fields.Boolean('Dynamic')
dynamic_group_condition = fields.Text(
'Condition', help='The condition to be met for a user to be a '
'member of this group. It is evaluated as python code at login '
'time, you get `user` passed as a browse record')
def eval_dynamic_group_condition(self, cr, uid, ids, context=None):
result = True
user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid,
context=context)
for this in self.browse(cr, uid, ids, context=context):
result &= bool(
safe_eval(
@api.multi
def eval_dynamic_group_condition(self):
result = all(
self.mapped(
lambda this: safe_eval(
this.dynamic_group_condition,
{
'user': user,
'user': self.env.user,
'any': any,
'all': all,
'filter': filter,
}))
})))
return result
def _check_dynamic_group_condition(self, cr, uid, ids, context=None):
@api.multi
@api.constrains('dynamic_group_condition')
def _check_dynamic_group_condition(self):
try:
for this in self.browse(cr, uid, ids, context=context):
if this.is_dynamic:
this.eval_dynamic_group_condition()
self.filtered('is_dynamic').eval_dynamic_group_condition()
except (NameError, SyntaxError, TypeError) as e:
logging.info(e)
return False
return True
raise exceptions.ValidationError(
_('The condition doesn\'t evaluate correctly!'))
_constraints = [
(_check_dynamic_group_condition,
'The condition doesn\'t evaluate correctly!',
['dynamic_group_condition']),
]
def action_evaluate(self, cr, uid, ids, context=None):
user_obj = self.pool.get('res.users')
for user in user_obj.browse(
cr, uid,
user_obj.search(cr, uid, [], context=context),
context=context):
user_obj.update_dynamic_groups(user.id, cr.dbname)
@api.multi
def action_evaluate(self):
res_users = self.env['res.users']
for user in res_users.search([]):
res_users.update_dynamic_groups(user.id, self.env.cr.dbname)

20
auth_dynamic_groups/view/res_groups.xml

@ -13,15 +13,17 @@
<attribute name="attrs">{'readonly': [('is_dynamic', '=', True)]}</attribute>
</field>
<field name="users" position="before">
<field
name="dynamic_group_condition"
colspan="4" nolabel="1"
attrs="{'invisible': [('is_dynamic', '=', False)]}" />
<button
type="object"
name="action_evaluate"
string="Evaluate"
attrs="{'invisible': [('is_dynamic', '=', False)]}" />
<group name="auth_dynamic_groups" attrs="{'invisible': [('is_dynamic', '=', False)]}">
<field
name="dynamic_group_condition"
placeholder="Fill in your condition..."
attrs="{'required': [('is_dynamic', '=', True)]}"
colspan="4" nolabel="1" />
<button
type="object"
name="action_evaluate"
string="Evaluate" />
</group>
</field>
</data>
</field>

Loading…
Cancel
Save