[IMP]api.one if better than manual looping on recordset

This commit is contained in:
Fabien Bourgeois 2016-08-17 10:04:42 +02:00
parent 5d7077efc3
commit 5045c2ff11
4 changed files with 23 additions and 22 deletions

View File

@ -67,9 +67,9 @@ class GolemActivity(models.Model):
is_current = fields.Boolean('Current season?', store=True, default=False,
compute='_compute_is_current')
@api.one
@api.depends('season_id')
def _compute_is_current(self):
""" Checks if activity is active for current season """
default_season = self._default_season()
for a in self:
a.is_current = (default_season == a.season_id)
self.is_current = (default_season == self.season_id)

View File

@ -84,14 +84,14 @@ class GolemActivitySession(models.Model):
session_name = fields.Char('Name', compute='_compute_full_name',
store=True, index=True)
@api.one
@api.depends('name', 'default_code')
def _compute_full_name(self):
""" Provide a better displayed name """
for s in self:
session_name = unicode(s.name)
if s.default_code:
session_name = u'[{}] {}'.format(s.default_code, session_name)
s.session_name = session_name
session_name = unicode(self.name)
if self.default_code:
session_name = u'[{}] {}'.format(self.default_code, session_name)
self.session_name = session_name
member_ids = fields.Many2many('golem.member', string='Members')
type_of = fields.Selection([('activity', _('Activity')),
@ -109,10 +109,10 @@ class GolemActivitySession(models.Model):
places_used = fields.Integer('Places used', compute='_compute_places_used')
@api.one
@api.depends('member_ids')
def _compute_places_used(self):
for s in self:
s.places_used = len(s.member_ids)
self.places_used = len(self.member_ids)
# TODO: to link with calendar.event
activity_id = fields.Many2one('golem.activity', string='Activity',
@ -195,11 +195,11 @@ class GolemActivitySession(models.Model):
places_remain = fields.Integer('Remaining places', store=True,
compute='_compute_places_remain')
@api.one
@api.depends('places', 'member_ids')
def _compute_places_remain(self):
for s in self:
used = len(s.member_ids)
s.places_remain = s.places - used
used = len(self.member_ids)
self.places_remain = self.places - used
@api.constrains('places_remain')
def _check_remaining_places(self):

View File

@ -26,14 +26,14 @@ class GolemActivitySession(models.Model):
is_overbooked = fields.Boolean('Allow overbook?', default=False)
places_overbooked = fields.Integer('Places with overbook', default=0)
@api.one
@api.depends('places', 'is_overbooked', 'places_overbooked', 'member_ids')
def _compute_places_remain(self):
for s in self:
used = len(s.member_ids)
if not s.is_overbooked:
s.places_remain = s.places - used
else:
s.places_remain = s.places_overbooked - used
used = len(self.member_ids)
if not self.is_overbooked:
self.places_remain = self.places - used
else:
self.places_remain = self.places_overbooked - used
@api.onchange('is_overbooked', 'places')
def onchange_is_overbooked(self):

View File

@ -78,13 +78,14 @@ class GolemMember(models.Model):
'UNIQUE (number_manual)',
_('This member number has already been used.'))]
@api.one
@api.depends('season_ids')
def _compute_is_current(self):
""" Computes is current according to seasons """
default_s = self._default_season()
for m in self:
m.is_current = default_s in m.season_ids
self.is_current = default_s in self.season_ids
@api.one
@api.depends('number')
def _compute_is_number_manual(self):
conf = self.env['ir.config_parameter']
@ -183,10 +184,10 @@ class GolemMemberNumber(models.Model):
auto_join=True)
number = fields.Char('Number', index=True, readonly=True)
@api.one
@api.depends('season_id')
def _compute_name(self):
for row in self:
row.name = row.season_id.name
self.name = self.season_id.name
class GolemNumberConfig(models.TransientModel):