|
@ -24,7 +24,7 @@ |
|
|
############################################################################## |
|
|
############################################################################## |
|
|
|
|
|
|
|
|
from openerp.osv import orm, fields |
|
|
from openerp.osv import orm, fields |
|
|
import random |
|
|
|
|
|
|
|
|
from openerp.tools.translate import _ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class tile(orm.Model): |
|
|
class tile(orm.Model): |
|
@ -36,16 +36,46 @@ class tile(orm.Model): |
|
|
res = {} |
|
|
res = {} |
|
|
records = self.browse(cr, uid, ids, context=context) |
|
|
records = self.browse(cr, uid, ids, context=context) |
|
|
for r in records: |
|
|
for r in records: |
|
|
|
|
|
res[r.id] = { |
|
|
|
|
|
'active': False, |
|
|
|
|
|
'count': 0, |
|
|
|
|
|
'computed_value': 0, |
|
|
|
|
|
'helper': '', |
|
|
|
|
|
} |
|
|
if ima_obj.check( |
|
|
if ima_obj.check( |
|
|
cr, uid, r.model_id.model, 'read', False, context): |
|
|
cr, uid, r.model_id.model, 'read', False, context): |
|
|
|
|
|
# Compute count item |
|
|
model = self.pool.get(r.model_id.model) |
|
|
model = self.pool.get(r.model_id.model) |
|
|
res[r.id] = { |
|
|
|
|
|
|
|
|
count = model.search_count( |
|
|
|
|
|
cr, uid, eval(r.domain), context=context) |
|
|
|
|
|
res[r.id].update({ |
|
|
'active': True, |
|
|
'active': True, |
|
|
'count': model.search_count( |
|
|
|
|
|
cr, uid, eval(r.domain), context), |
|
|
|
|
|
} |
|
|
|
|
|
else: |
|
|
|
|
|
res[r.id] = {'active': False, 'count': 0} |
|
|
|
|
|
|
|
|
'count': count, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
# Compute datas for field_id depending of field_function |
|
|
|
|
|
if r.field_function and r.field_id and count != 0: |
|
|
|
|
|
ids = model.search( |
|
|
|
|
|
cr, uid, eval(r.domain), context=context) |
|
|
|
|
|
vals = [x[r.field_id.name] for x in model.read( |
|
|
|
|
|
cr, uid, ids, [r.field_id.name], context=context)] |
|
|
|
|
|
desc = r.field_id.field_description |
|
|
|
|
|
if r.field_function == 'min': |
|
|
|
|
|
value = min(vals) |
|
|
|
|
|
helper = _("'Minimum value of %s'" % desc) |
|
|
|
|
|
elif r.field_function == 'max': |
|
|
|
|
|
value = max(vals) |
|
|
|
|
|
helper = _("'Maximum value of %s'" % desc) |
|
|
|
|
|
elif r.field_function == 'sum': |
|
|
|
|
|
value = sum(vals) |
|
|
|
|
|
helper = _("'Total value of %s'" % desc) |
|
|
|
|
|
elif r.field_function == 'avg': |
|
|
|
|
|
value = sum(vals) / len(vals) |
|
|
|
|
|
helper = _("'Average value of %s'" % desc) |
|
|
|
|
|
res[r.id].update({ |
|
|
|
|
|
'computed_value': value, |
|
|
|
|
|
'helper': helper, |
|
|
|
|
|
}) |
|
|
return res |
|
|
return res |
|
|
|
|
|
|
|
|
def _search_active(self, cr, uid, obj, name, arg, context=None): |
|
|
def _search_active(self, cr, uid, obj, name, arg, context=None): |
|
@ -64,13 +94,28 @@ class tile(orm.Model): |
|
|
|
|
|
|
|
|
_columns = { |
|
|
_columns = { |
|
|
'name': fields.char('Tile Name'), |
|
|
'name': fields.char('Tile Name'), |
|
|
'model_id': fields.many2one('ir.model', 'Model'), |
|
|
|
|
|
|
|
|
'model_id': fields.many2one('ir.model', 'Model', required=True), |
|
|
'user_id': fields.many2one('res.users', 'User'), |
|
|
'user_id': fields.many2one('res.users', 'User'), |
|
|
'domain': fields.text('Domain'), |
|
|
'domain': fields.text('Domain'), |
|
|
'action_id': fields.many2one('ir.actions.act_window', 'Action'), |
|
|
'action_id': fields.many2one('ir.actions.act_window', 'Action'), |
|
|
'count': fields.function( |
|
|
'count': fields.function( |
|
|
_get_tile_info, type='int', string='Count', |
|
|
_get_tile_info, type='int', string='Count', |
|
|
multi='tile_info', readonly=True), |
|
|
multi='tile_info', readonly=True), |
|
|
|
|
|
'computed_value': fields.function( |
|
|
|
|
|
_get_tile_info, type='float', string='Computed Value', |
|
|
|
|
|
multi='tile_info', readonly=True), |
|
|
|
|
|
'helper': fields.function( |
|
|
|
|
|
_get_tile_info, type='char', string='Helper', |
|
|
|
|
|
multi='tile_info', readonly=True), |
|
|
|
|
|
'field_function': fields.selection([ |
|
|
|
|
|
('min', 'Minimum'), |
|
|
|
|
|
('max', 'Maximum'), |
|
|
|
|
|
('sum', 'Sum'), |
|
|
|
|
|
('avg', 'Average')], 'Function'), |
|
|
|
|
|
'field_id': fields.many2one( |
|
|
|
|
|
'ir.model.fields', 'Field', |
|
|
|
|
|
domain="[('model_id', '=', model_id)," |
|
|
|
|
|
" ('ttype', 'in', ['float', 'int'])]"), |
|
|
'active': fields.function( |
|
|
'active': fields.function( |
|
|
_get_tile_info, type='boolean', string='Active', |
|
|
_get_tile_info, type='boolean', string='Active', |
|
|
multi='tile_info', readonly=True, fnct_search=_search_active), |
|
|
multi='tile_info', readonly=True, fnct_search=_search_active), |
|
@ -80,6 +125,31 @@ class tile(orm.Model): |
|
|
'Sequence', required=True), |
|
|
'Sequence', required=True), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
# Constraint Section |
|
|
|
|
|
def _check_model_id_field_id(self, cr, uid, ids, context=None): |
|
|
|
|
|
for t in self.browse(cr, uid, ids, context=context): |
|
|
|
|
|
if t.field_id and t.field_id.model_id.id != t.model_id.id: |
|
|
|
|
|
return False |
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def _check_field_id_field_function(self, cr, uid, ids, context=None): |
|
|
|
|
|
for t in self.browse(cr, uid, ids, context=context): |
|
|
|
|
|
if t.field_id and not t.field_function or\ |
|
|
|
|
|
t.field_function and not t.field_id: |
|
|
|
|
|
return False |
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
_constraints = [ |
|
|
|
|
|
( |
|
|
|
|
|
_check_model_id_field_id, |
|
|
|
|
|
"Error ! Please select a field of the select model.", |
|
|
|
|
|
['model_id', 'field_id']), |
|
|
|
|
|
( |
|
|
|
|
|
_check_field_id_field_function, |
|
|
|
|
|
"Error ! Please set both fields: 'Field' and 'Function'.", |
|
|
|
|
|
['field_id', 'field_function']), |
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
_defaults = { |
|
|
_defaults = { |
|
|
'domain': '[]', |
|
|
'domain': '[]', |
|
|
'color': '#0E6C7E', |
|
|
'color': '#0E6C7E', |
|
@ -123,6 +193,4 @@ class tile(orm.Model): |
|
|
[('model', '=', |
|
|
[('model', '=', |
|
|
vals['model_id'])]) |
|
|
vals['model_id'])]) |
|
|
vals['model_id'] = model_ids[0] |
|
|
vals['model_id'] = model_ids[0] |
|
|
if 'color' not in vals: |
|
|
|
|
|
vals['color'] = random.randint(1, 10) |
|
|
|
|
|
return self.create(cr, uid, vals, context) |
|
|
return self.create(cr, uid, vals, context) |