3
0
Fork 0

[PORT] 7.0 -> 8.0 API;

16.0
Sylvain LE GAL 2015-07-17 11:21:31 +02:00
parent 4a64a30408
commit e51469266f
1 changed files with 86 additions and 113 deletions

View File

@ -23,11 +23,13 @@
# #
############################################################################## ##############################################################################
from openerp.osv import orm, fields from openerp import api, fields
from openerp.models import Model
from openerp.exceptions import except_orm
from openerp.tools.translate import _ from openerp.tools.translate import _
class tile(orm.Model): class TileTile(Model):
_name = 'tile.tile' _name = 'tile.tile'
_order = 'sequence, name' _order = 'sequence, name'
@ -39,58 +41,49 @@ class tile(orm.Model):
half = (len(aList) - 1) / 2 half = (len(aList) - 1) / 2
return sum(sorted(aList)[half:half + even]) / float(even) return sum(sorted(aList)[half:half + even]) / float(even)
def _get_tile_info(self, cr, uid, ids, fields, args, context=None): def _get_tile_info(self):
ima_obj = self.pool['ir.model.access'] ima_obj = self.env['ir.model.access']
res = {} res = {}
records = self.browse(cr, uid, ids, context=context) for r in self:
for r in records: r.active = False
res[r.id] = { r.count = 0
'active': False, r.computed_value = 0
'count': 0, helper = ''
'computed_value': 0, if ima_obj.check(r.model_id.model, 'read', False):
'helper': '',
}
if ima_obj.check(
cr, uid, r.model_id.model, 'read', False, context):
# Compute count item # Compute count item
model = self.pool.get(r.model_id.model) model = self.env[r.model_id.model]
count = model.search_count( r.count = model.search_count(eval(r.domain))
cr, uid, eval(r.domain), context=context) r.active = True
res[r.id].update({
'active': True,
'count': count,
})
# Compute datas for field_id depending of field_function # Compute datas for field_id depending of field_function
if r.field_function and r.field_id and count != 0: if r.field_function and r.field_id and r.count != 0:
ids = model.search( records = model.search(eval(r.domain))
cr, uid, eval(r.domain), context=context) vals = [x[r.field_id.name] for x in records]
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 desc = r.field_id.field_description
if r.field_function == 'min': if r.field_function == 'min':
value = min(vals) r.computed_value = min(vals)
helper = _("Minimum value of '%s'") % desc r.helper = _("Minimum value of '%s'") % desc
elif r.field_function == 'max': elif r.field_function == 'max':
value = max(vals) r.computed_value = max(vals)
helper = _("Maximum value of '%s'") % desc r.helper = _("Maximum value of '%s'") % desc
elif r.field_function == 'sum': elif r.field_function == 'sum':
value = sum(vals) r.computed_value = sum(vals)
helper = _("Total value of '%s'") % desc r.helper = _("Total value of '%s'") % desc
elif r.field_function == 'avg': elif r.field_function == 'avg':
value = sum(vals) / len(vals) r.computed_value = sum(vals) / len(vals)
helper = _("Average value of '%s'") % desc r.helper = _("Average value of '%s'") % desc
elif r.field_function == 'median': elif r.field_function == 'median':
value = self.median(vals) r.computed_value = self.median(vals)
helper = _("Median value of '%s'") % desc r.helper = _("Median 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, operator, value):
ima_obj = self.pool['ir.model.access'] cr = self.env.cr
if operator != '=':
raise except_orm(
'Unimplemented Feature',
'Search on Active field disabled.')
ima_obj = self.env['ir.model.access']
ids = [] ids = []
cr.execute(""" cr.execute("""
SELECT tt.id, im.model SELECT tt.id, im.model
@ -98,45 +91,41 @@ class tile(orm.Model):
INNER JOIN ir_model im INNER JOIN ir_model im
ON tt.model_id = im.id""") ON tt.model_id = im.id""")
for result in cr.fetchall(): for result in cr.fetchall():
if (ima_obj.check(cr, uid, result[1], 'read', False) == if (ima_obj.check(result[1], 'read', False) == value):
arg[0][2]):
ids.append(result[0]) ids.append(result[0])
return [('id', 'in', ids)] return [('id', 'in', ids)]
_columns = { # Column Section
'name': fields.char('Tile Name'), name=fields.Char(required=True)
'model_id': fields.many2one('ir.model', 'Model', required=True), model_id=fields.Many2one(
'user_id': fields.many2one('res.users', 'User'), comodel_name='ir.model', string='Model', required=True)
'domain': fields.text('Domain'), user_id=fields.Many2one(
'action_id': fields.many2one('ir.actions.act_window', 'Action'), comodel_name='res.users', string='User')
'count': fields.function( domain=fields.Text(default='[]')
_get_tile_info, type='int', string='Count', action_id=fields.Many2one(
multi='tile_info', readonly=True), comodel_name='ir.actions.act_window', string='Action')
'computed_value': fields.function( count=fields.Integer(
_get_tile_info, type='float', string='Computed Value', compute='_get_tile_info', readonly=True) #readonly usefull ?
multi='tile_info', readonly=True), computed_value=fields.Float(
'helper': fields.function( compute='_get_tile_info', readonly=True) #readonly usefull ?
_get_tile_info, type='char', string='Helper Text', helper=fields.Char(
multi='tile_info', readonly=True), compute='_get_tile_info', readonly=True) #readonly usefull ?
'field_function': fields.selection([ field_function=fields.Selection(selection=[
('min', 'Minimum'), ('min', 'Minimum'),
('max', 'Maximum'), ('max', 'Maximum'),
('sum', 'Sum'), ('sum', 'Sum'),
('avg', 'Average'), ('avg', 'Average'),
('median', 'Median'), ('median', 'Median'),
], 'Function'), ], string='Function')
'field_id': fields.many2one( field_id=fields.Many2one(
'ir.model.fields', 'Field', comodel_name='ir.model.fields', string='Field',
domain="[('model_id', '=', model_id)," domain="[('model_id', '=', model_id),"
" ('ttype', 'in', ['float', 'int'])]"), " ('ttype', 'in', ['float', 'int'])]")
'active': fields.function( active=fields.Boolean(
_get_tile_info, type='boolean', string='Active', compute='_get_tile_info', readonly=True, search='_search_active')
multi='tile_info', readonly=True, fnct_search=_search_active), color=fields.Char(default='#0E6C7E', string='Background Color')
'color': fields.char('Background color'), font_color=fields.Char(default='#FFFFFF')
'font_color': fields.char('Font Color'), sequence=fields.Integer(default=0, required=True)
'sequence': fields.integer(
'Sequence', required=True),
}
# Constraint Section # Constraint Section
def _check_model_id_field_id(self, cr, uid, ids, context=None): def _check_model_id_field_id(self, cr, uid, ids, context=None):
@ -163,47 +152,31 @@ class tile(orm.Model):
['field_id', 'field_function']), ['field_id', 'field_function']),
] ]
_defaults = { # View / action Section
'domain': '[]', @api.multi
'color': '#0E6C7E', def open_link(self):
'font_color': '#FFFFFF', res = {
'sequence': 0, 'name': self.name,
}
def open_link(self, cr, uid, ids, context=None):
tile_id = ids[0]
tile_object = self.browse(cr, uid, tile_id, context=context)
if tile_object.action_id:
act_obj = self.pool.get('ir.actions.act_window')
result = act_obj.read(cr, uid, [tile_object.action_id.id],
context=context)[0]
# FIXME: restore original Domain + Filter would be better
result['domain'] = tile_object.domain
return result
# we have no action_id stored,
# so try to load a default tree view
return {
'name': tile_object.name,
'view_type': 'form', 'view_type': 'form',
'view_mode': 'tree', 'view_mode': 'tree',
'view_id': [False], 'view_id': [False],
'res_model': tile_object.model_id.model, 'res_model': self.model_id.model,
'type': 'ir.actions.act_window', 'type': 'ir.actions.act_window',
'context': context, 'context': self.env.context,
'nodestroy': True, 'nodestroy': True,
'target': 'current', 'target': 'current',
'domain': tile_object.domain, 'domain': self.domain,
} }
if self.action_id:
res.update(self.action_id.read(
['view_type', 'view_mode', 'view_id', 'type'])[0])
# FIXME: restore original Domain + Filter would be better
return res
def add(self, cr, uid, vals, context=None): @api.model
# TODO: check if string def add(self, vals):
if 'model_id' in vals: if 'model_id' in vals and not vals['model_id'].isdigit():
# need to replace model_name with its id # need to replace model_name with its id
model_ids = self.pool.get('ir.model').search(cr, uid, vals['model_id'] = self.env['ir.model'].search(
[('model', '=', [('model', '=', vals['model_id'])]).id
vals['model_id'])]) self.create(vals)
vals['model_id'] = model_ids[0]
return self.create(cr, uid, vals, context)