Permalink
Browse files

[IMP] models: change API of `_group_by_full`

The dictionary `_group_by_full` is replaced by a field parameter `group_expand`
that is assigned to the method name.  The API of the method has been simplified
as well:

    @api.multi
    def _read_group_stage_ids(self, domain, read_group_order=None, access_rights_uid=None):
        # the stages are given by self.ids (wrong model);
        # read_group_order is the order given to read_group() on self;
        # return stages.name_get(), {stage.id: stage.fold)

    _group_by_full = {'stage_id': _read_group_stage_ids}

is now written:

    stage_id = fields.Many2one(..., group_expand='_read_group_stage_ids')

    @api.model
    def _read_group_stage_ids(self, stages, domain, order):
        # stages is a recordset;
        # order is the order to use on stages' model;
        # return a recordset which is a superset of stages
  • Loading branch information...
rco-odoo committed Sep 15, 2016
1 parent fe0478a commit fa082019a0f36c39ec8c235e812701158bbe1899
@@ -5,7 +5,7 @@
from datetime import datetime, timedelta, date
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models, tools
from odoo import api, fields, models, tools, SUPERUSER_ID
from odoo.tools.translate import _
from odoo.tools import email_re, email_split
from odoo.exceptions import UserError, AccessError
@@ -97,7 +97,7 @@ def _default_stage_id(self):
stage_id = fields.Many2one('crm.stage', string='Stage', track_visibility='onchange', index=True,
domain="['|', ('team_id', '=', False), ('team_id', '=', team_id)]",
default=lambda self: self._default_stage_id())
group_expand='_read_group_stage_ids', default=lambda self: self._default_stage_id())
user_id = fields.Many2one('res.users', string='Salesperson', index=True, track_visibility='onchange', default=lambda self: self.env.user)
referred = fields.Char('Referred By')
@@ -147,37 +147,21 @@ def _default_stage_id(self):
('check_probability', 'check(probability >= 0 and probability <= 100)', 'The probability of closing the deal should be between 0% and 100%!')
]
@api.multi
def _read_group_stage_ids(self, domain, read_group_order=None, access_rights_uid=None):
if access_rights_uid:
self = self.sudo(access_rights_uid)
Stage = self.env['crm.stage']
order = Stage._order
# lame hack to allow reverting search, should just work in the trivial case
if read_group_order == 'stage_id desc':
order = "%s desc" % order
@api.model
def _read_group_stage_ids(self, stages, domain, order):
# retrieve team_id from the context and write the domain
# - ('id', 'in', 'ids'): add columns that should be present
# - ('id', 'in', stages.ids): add columns that should be present
# - OR ('fold', '=', False): add default columns that are not folded
# - OR ('team_ids', '=', team_id), ('fold', '=', False) if team_id: add team columns that are not folded
team_id = self._context.get('default_team_id')
if team_id:
search_domain = ['|', ('id', 'in', self.ids), '|', ('team_id', '=', False), ('team_id', '=', team_id)]
search_domain = ['|', ('id', 'in', stages.ids), '|', ('team_id', '=', False), ('team_id', '=', team_id)]
else:
search_domain = ['|', ('id', 'in', self.ids), ('team_id', '=', False)]
search_domain = ['|', ('id', 'in', stages.ids), ('team_id', '=', False)]
# perform search
stages = Stage.browse(Stage._search(search_domain, order=order, access_rights_uid=access_rights_uid))
fold = {
stage.id: stage.fold or False
for stage in stages
}
return stages.name_get(), fold
_group_by_full = {
'stage_id': _read_group_stage_ids
}
stage_ids = stages._search(search_domain, order=order, access_rights_uid=SUPERUSER_ID)
return stages.browse(stage_ids)
@api.multi
def _compute_kanban_state(self):
@@ -3,7 +3,7 @@
from datetime import datetime
from odoo import api, fields, models, tools
from odoo import api, fields, models, tools, SUPERUSER_ID
from odoo.tools.translate import _
from odoo.exceptions import UserError
@@ -125,6 +125,7 @@ def _default_company_id(self):
stage_id = fields.Many2one('hr.recruitment.stage', 'Stage', track_visibility='onchange',
domain="['|', ('job_id', '=', False), ('job_id', '=', job_id)]",
copy=False, index=True,
group_expand='_read_group_stage_ids',
default=_default_stage_id)
last_stage_id = fields.Many2one('hr.recruitment.stage', "Last Stage",
help="Stage of the applicant before being in the current stage. Used for lost cases analysis.")
@@ -180,33 +181,18 @@ def _get_attachment_number(self):
for record in self:
record.attachment_number = attach_data.get(record.id, 0)
@api.multi
def _read_group_stage_ids(self, domain, read_group_order=None, access_rights_uid=None):
access_rights_uid = access_rights_uid or self.env.uid
Stage = self.env['hr.recruitment.stage']
order = Stage._order
@api.model
def _read_group_stage_ids(self, stages, domain, order):
# retrieve job_id from the context and write the domain: ids + contextual columns (job or default)
job_id = self._context.get('default_job_id')
search_domain = [('job_id', '=', False)]
if job_id:
search_domain = ['|'] + search_domain + [('job_id', '=', job_id)]
if self.ids:
search_domain = ['|'] + search_domain + [('id', 'in', self.ids)]
stage_ids = Stage._search(search_domain, order=order, access_rights_uid=access_rights_uid)
stages = Stage.sudo(access_rights_uid).browse(stage_ids)
result = stages.name_get()
# restore order of the search
result.sort(lambda x, y: cmp(stage_ids.index(x[0]), stage_ids.index(y[0])))
fold = {}
for stage in stages:
fold[stage.id] = stage.fold or False
return result, fold
_group_by_full = {
'stage_id': _read_group_stage_ids
}
search_domain = ['|', ('job_id', '=', job_id)] + search_domain
if stages:
search_domain = ['|', ('id', 'in', stages.ids)] + search_domain
stage_ids = stages._search(search_domain, order=order, access_rights_uid=SUPERUSER_ID)
return stages.browse(stage_ids)
@api.onchange('job_id')
def onchange_job_id(self):
@@ -2,7 +2,7 @@
from datetime import date, datetime, timedelta
from odoo import api, fields, models, _
from odoo import api, fields, models, SUPERUSER_ID, _
from odoo.exceptions import UserError
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
@@ -123,7 +123,8 @@ def name_search(self, name, args=None, operator='ilike', limit=100):
active = fields.Boolean(default=True)
technician_user_id = fields.Many2one('res.users', string='Technician', track_visibility='onchange', oldname='user_id')
owner_user_id = fields.Many2one('res.users', string='Owner', track_visibility='onchange')
category_id = fields.Many2one('maintenance.equipment.category', string='Equipment Category', track_visibility='onchange')
category_id = fields.Many2one('maintenance.equipment.category', string='Equipment Category',
track_visibility='onchange', group_expand='_read_group_category_ids')
partner_id = fields.Many2one('res.partner', string='Vendor', domain="[('supplier', '=', 1)]")
partner_ref = fields.Char('Vendor Reference')
location = fields.Char('Location')
@@ -181,26 +182,13 @@ def write(self, vals):
self.message_subscribe_users(user_ids=[vals['owner_user_id']])
return super(MaintenanceEquipment, self).write(vals)
@api.multi
def _read_group_category_ids(self, domain, read_group_order=None, access_rights_uid=None):
""" Read group customization in order to display all the category in the
kanban view, even if they are empty
@api.model
def _read_group_category_ids(self, categories, domain, order):
""" Read group customization in order to display all the categories in
the kanban view, even if they are empty.
"""
category_obj = self.env['maintenance.equipment.category']
order = category_obj._order
access_rights_uid = access_rights_uid or self._uid
if read_group_order == 'category_id desc':
order = '%s desc' % order
category_ids = category_obj._search([], order=order, access_rights_uid=access_rights_uid)
result = [category.name_get()[0] for category in category_obj.browse(category_ids)]
# restore order of the search
result.sort(lambda x, y: cmp(category_ids.index(x[0]), category_ids.index(y[0])))
fold = {}
for category in category_obj.browse(category_ids):
fold[category.id] = category.fold
return result, fold
category_ids = categories._search([], order=order, access_rights_uid=SUPERUSER_ID)
return categories.browse(category_ids)
@api.model
def _cron_generate_requests(self):
@@ -214,10 +202,6 @@ def _cron_generate_requests(self):
'maintenance_type': 'preventive',
})
_group_by_full = {
'category_id': _read_group_category_ids
}
class MaintenanceRequest(models.Model):
_name = 'maintenance.request'
@@ -249,7 +233,8 @@ def _get_default_team_id(self):
category_id = fields.Many2one('maintenance.equipment.category', related='equipment_id.category_id', string='Category', store=True, readonly=True)
equipment_id = fields.Many2one('maintenance.equipment', string='Equipment', index=True)
technician_user_id = fields.Many2one('res.users', string='Owner', track_visibility='onchange', oldname='user_id')
stage_id = fields.Many2one('maintenance.stage', string='Stage', track_visibility='onchange', default=_default_stage)
stage_id = fields.Many2one('maintenance.stage', string='Stage', track_visibility='onchange',
group_expand='_read_group_stage_ids', default=_default_stage)
priority = fields.Selection([('0', 'Very Low'), ('1', 'Low'), ('2', 'Normal'), ('3', 'High')], string='Priority')
color = fields.Integer('Color Index')
close_date = fields.Date('Close Date')
@@ -310,32 +295,13 @@ def write(self, vals):
self.write({'close_date': fields.Date.today()})
return res
@api.multi
def _read_group_stage_ids(self, domain, read_group_order=None, access_rights_uid=None):
@api.model
def _read_group_stage_ids(self, stages, domain, order):
""" Read group customization in order to display all the stages in the
kanban view, even if they are empty
"""
stage_obj = self.env['maintenance.stage']
order = stage_obj._order
access_rights_uid = access_rights_uid or self._uid
if read_group_order == 'stage_id desc':
order = '%s desc' % order
stage_ids = stage_obj._search([], order=order, access_rights_uid=access_rights_uid)
result = [stage.name_get()[0] for stage in stage_obj.browse(stage_ids)]
# restore order of the search
result.sort(lambda x, y: cmp(stage_ids.index(x[0]), stage_ids.index(y[0])))
fold = {}
for stage in stage_obj.browse(stage_ids):
fold[stage.id] = stage.fold or False
return result, fold
_group_by_full = {
'stage_id': _read_group_stage_ids
}
stage_ids = stages._search([], order=order, access_rights_uid=SUPERUSER_ID)
return stages.browse(stage_ids)
class MaintenanceTeam(models.Model):
@@ -3,7 +3,7 @@
from lxml import etree
from odoo import api, fields, models, tools, _
from odoo import api, fields, models, tools, SUPERUSER_ID, _
from odoo.exceptions import UserError, ValidationError
from odoo.tools.safe_eval import safe_eval
@@ -304,28 +304,14 @@ def _get_default_stage_id(self):
return False
return self.stage_find(project_id, [('fold', '=', False)])
@api.multi
def _read_group_stage_ids(self, domain, read_group_order=None, access_rights_uid=None):
TaskType = self.env['project.task.type']
order = TaskType._order
access_rights_uid = access_rights_uid or self.env.uid
if read_group_order == 'stage_id desc':
order = '%s desc' % order
@api.model
def _read_group_stage_ids(self, stages, domain, order):
search_domain = [('id', 'in', stages.ids)]
if 'default_project_id' in self.env.context:
search_domain = ['|', ('project_ids', '=', self.env.context['default_project_id']), ('id', 'in', self.ids)]
else:
search_domain = [('id', 'in', self.ids)]
stage_ids = TaskType._search(search_domain, order=order, access_rights_uid=access_rights_uid)
stages = TaskType.sudo(access_rights_uid).browse(stage_ids)
result = stages.name_get()
# restore order of the search
result.sort(lambda x, y: cmp(stage_ids.index(x[0]), stage_ids.index(y[0])))
return result, {stage.id: stage.fold for stage in stages}
search_domain = ['|', ('project_ids', '=', self.env.context['default_project_id'])] + search_domain
_group_by_full = {
'stage_id': _read_group_stage_ids,
}
stage_ids = stages._search(search_domain, order=order, access_rights_uid=SUPERUSER_ID)
return stages.browse(stage_ids)
active = fields.Boolean(default=True)
name = fields.Char(string='Task Title', track_visibility='onchange', required=True, index=True)
@@ -337,7 +323,7 @@ def _read_group_stage_ids(self, domain, read_group_order=None, access_rights_uid
sequence = fields.Integer(string='Sequence', index=True, default=10,
help="Gives the sequence order when displaying a list of tasks.")
stage_id = fields.Many2one('project.task.type', string='Stage', track_visibility='onchange', index=True,
default=_get_default_stage_id,
default=_get_default_stage_id, group_expand='_read_group_stage_ids',
domain="[('project_ids', '=', project_id)]", copy=False)
tag_ids = fields.Many2many('project.tags', string='Tags', oldname='categ_ids')
kanban_state = fields.Selection([
@@ -46,6 +46,7 @@ def _get_default_stage_id(self):
priority = fields.Selection([('0', 'Low'), ('1', 'Normal'), ('2', 'High')], 'Priority', index=True, default='0')
stage_id = fields.Many2one('project.task.type', string='Stage', track_visibility='onchange', index=True,
domain="[('project_ids', '=', project_id)]", copy=False,
group_expand='_read_group_stage_ids',
default=_get_default_stage_id)
project_id = fields.Many2one('project.project', string='Project', track_visibility='onchange', index=True)
duration = fields.Float('Duration')
@@ -67,31 +68,14 @@ def _get_default_stage_id(self):
legend_done = fields.Char(related="stage_id.legend_done", string='Kanban Valid Explanation', readonly=True)
legend_normal = fields.Char(related="stage_id.legend_normal", string='Kanban Ongoing Explanation', readonly=True)
@api.multi
def _read_group_stage_ids(self, domain, read_group_order=None, access_rights_uid=None):
access_rights_uid = access_rights_uid or self.env.uid
ProjectTaskType = self.env['project.task.type']
order = ProjectTaskType._order
# lame hack to allow reverting search, should just work in the trivial case
if read_group_order == 'stage_id desc':
order = "%s desc" % order
@api.model
def _read_group_stage_ids(self, stages, domain, order):
search_domain = [('id', 'in', stages.ids)]
# retrieve project_id from the context, add them to already fetched columns (ids)
if 'default_project_id' in self.env.context:
search_domain = ['|', ('project_ids', '=', self.env.context['default_project_id']), ('id', 'in', self.ids)]
else:
search_domain = [('id', 'in', self.ids)]
search_domain = ['|', ('project_ids', '=', self.env.context['default_project_id'])] + search_domain
# perform search
project_task_types = ProjectTaskType.sudo(access_rights_uid).search(search_domain, order=order)
result = project_task_types.sudo(access_rights_uid).name_get()
# restore order of the search
project_task_type_ids = project_task_types.mapped('id')
result.sort(lambda x, y: cmp(project_task_type_ids.index(x[0]), project_task_type_ids.index(y[0])))
fold = {project_task_type.id: project_task_type.fold for project_task_type in project_task_types}
return result, fold
_group_by_full = {
'stage_id': _read_group_stage_ids
}
return stages.search(search_domain, order=order)
@api.multi
@api.depends('create_date', 'date_closed', 'date_open')
@@ -9,7 +9,7 @@
from collections import Counter, OrderedDict
from itertools import product
from odoo import api, fields, models, tools, _
from odoo import api, fields, models, tools, SUPERUSER_ID, _
from odoo.exceptions import UserError, ValidationError
from odoo.addons.website.models.website import slug
@@ -60,7 +60,8 @@ def _default_stage(self):
title = fields.Char('Title', required=True, translate=True)
page_ids = fields.One2many('survey.page', 'survey_id', string='Pages', copy=True)
stage_id = fields.Many2one('survey.stage', string="Stage", default=_default_stage, ondelete="set null", copy=False)
stage_id = fields.Many2one('survey.stage', string="Stage", default=_default_stage,
ondelete="set null", copy=False, group_expand='_read_group_stage_ids')
auth_required = fields.Boolean('Login required', help="Users with a public link will be requested to login before taking part to the survey",
oldname="authenticate")
users_can_go_back = fields.Boolean('Users can go back', help="If checked, users can go back to previous pages.")
@@ -110,26 +111,13 @@ def _compute_survey_url(self):
survey.result_url = urljoin(base_url, "survey/results/%s" % (slug(survey)))
survey.public_url_html = '<a href="%s">%s</a>' % (survey.public_url, _("Click here to start survey"))
@api.multi
def _read_group_stage_ids(self, domain, read_group_order=None, access_rights_uid=None):
@api.model
def _read_group_stage_ids(self, stages, domain, order):
""" Read group customization in order to display all the stages in the
kanban view, even if they are empty
"""
SurveyStage = self.env['survey.stage']
order = SurveyStage._order
access_rights_uid = access_rights_uid or self.env.user.id
if read_group_order == 'stage_id desc':
order = '%s desc' % order
stage_ids = SurveyStage._search([], order=order, access_rights_uid=access_rights_uid)
stages = SurveyStage.sudo(user=access_rights_uid).browse(stage_ids)
result = stages.name_get()
return result, {stage.id: stage.fold for stage in stages}
_group_by_full = {
'stage_id': _read_group_stage_ids
}
stage_ids = stages._search([], order=order, access_rights_uid=SUPERUSER_ID)
return stages.browse(stage_ids)
# Public methods #
def copy_data(self, default=None):
Oops, something went wrong.

0 comments on commit fa08201

Please sign in to comment.