[IMP] various: improve code style and performance by improve `all()` usage
PURPOSE
Clean code. Be more performance oriented.
SPECIFICATIONS
Improvements applied in this commit
* not all() --> any(not) for earlier returns;
* all([generator]) --> all(generator) to avoid unnecessary list casting.
This code construct is better managed by all;
This commit will probably not have a big performance effect on standard
production databases. However each performance and cleaning improvement
is welcomed.
LINKS
Task ID-2328619
closes odoo/odoo#56810
X-original-commit: 1cc6bb1231401ea7f501d2f5b5e9641ec8734850
Related: odoo/enterprise#12802
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
This commit is contained in:
committed by
Thibault Delavallée
parent
d0af0ddcc7
commit
c482fdbf05
@@ -153,9 +153,7 @@ class AccountTax(models.Model):
|
||||
for tax in self:
|
||||
if not tax._check_m2m_recursion('children_tax_ids'):
|
||||
raise ValidationError(_("Recursion found for tax '%s'.") % (tax.name,))
|
||||
if not all(child.type_tax_use in ('none', tax.type_tax_use) for child in tax.children_tax_ids):
|
||||
raise ValidationError(_('The application scope of taxes in a group must be either the same as the group or left empty.'))
|
||||
if not all(child.tax_scope == tax.tax_scope for child in tax.children_tax_ids):
|
||||
if any(child.type_tax_use not in ('none', tax.type_tax_use) or child.tax_scope != tax.tax_scope for child in tax.children_tax_ids):
|
||||
raise ValidationError(_('The application scope of taxes in a group must be either the same as the group or left empty.'))
|
||||
|
||||
@api.constrains('company_id')
|
||||
|
||||
@@ -167,8 +167,8 @@ class GoogleEvent(abc.Set):
|
||||
return self.filter(lambda e: e._odoo_id)
|
||||
|
||||
def _get_model(self, env):
|
||||
if all([e.is_recurrence() for e in self]):
|
||||
if all(e.is_recurrence() for e in self):
|
||||
return env['calendar.recurrence']
|
||||
if all([not e.is_recurrence() for e in self]):
|
||||
if all(not e.is_recurrence() for e in self):
|
||||
return env['calendar.event']
|
||||
raise TypeError("Mixing Google events and Google recurrences")
|
||||
|
||||
@@ -846,7 +846,7 @@ class HrExpenseSheet(models.Model):
|
||||
@api.constrains('expense_line_ids', 'company_id')
|
||||
def _check_expense_lines_company(self):
|
||||
for sheet in self:
|
||||
if not all(expense.company_id == sheet.company_id for expense in sheet.expense_line_ids):
|
||||
if any(expense.company_id != sheet.company_id for expense in sheet.expense_line_ids):
|
||||
raise ValidationError(_('An expense report must contain only lines from the same company.'))
|
||||
|
||||
@api.model
|
||||
|
||||
@@ -157,7 +157,7 @@ class AccountAnalyticLine(models.Model):
|
||||
if partner_id:
|
||||
vals['partner_id'] = partner_id
|
||||
# set timesheet UoM from the AA company (AA implies uom)
|
||||
if 'product_uom_id' not in vals and all([v in vals for v in ['account_id', 'project_id']]): # project_id required to check this is timesheet flow
|
||||
if 'product_uom_id' not in vals and all(v in vals for v in ['account_id', 'project_id']): # project_id required to check this is timesheet flow
|
||||
analytic_account = self.env['account.analytic.account'].sudo().browse(vals['account_id'])
|
||||
vals['product_uom_id'] = analytic_account.company_id.project_time_mode_id.id
|
||||
return vals
|
||||
|
||||
@@ -66,13 +66,13 @@ class IrModel(models.Model):
|
||||
|
||||
def write(self, vals):
|
||||
if self and ('is_mail_thread' in vals or 'is_mail_activity' in vals or 'is_mail_blacklist' in vals):
|
||||
if not all(rec.state == 'manual' for rec in self):
|
||||
if any(rec.state != 'manual' for rec in self):
|
||||
raise UserError(_('Only custom models can be modified.'))
|
||||
if 'is_mail_thread' in vals and not all(rec.is_mail_thread <= vals['is_mail_thread'] for rec in self):
|
||||
if 'is_mail_thread' in vals and any(rec.is_mail_thread > vals['is_mail_thread'] for rec in self):
|
||||
raise UserError(_('Field "Mail Thread" cannot be changed to "False".'))
|
||||
if 'is_mail_activity' in vals and not all(rec.is_mail_activity <= vals['is_mail_activity'] for rec in self):
|
||||
if 'is_mail_activity' in vals and any(rec.is_mail_activity > vals['is_mail_activity'] for rec in self):
|
||||
raise UserError(_('Field "Mail Activity" cannot be changed to "False".'))
|
||||
if 'is_mail_blacklist' in vals and not all(rec.is_mail_blacklist <= vals['is_mail_blacklist'] for rec in self):
|
||||
if 'is_mail_blacklist' in vals and any(rec.is_mail_blacklist > vals['is_mail_blacklist'] for rec in self):
|
||||
raise UserError(_('Field "Mail Blacklist" cannot be changed to "False".'))
|
||||
res = super(IrModel, self).write(vals)
|
||||
self.flush()
|
||||
|
||||
@@ -216,8 +216,8 @@ class MicrosoftEvent(abc.Set):
|
||||
return self.filter(lambda e: e._odoo_id)
|
||||
|
||||
def _get_model(self, env):
|
||||
if all([e.is_recurrence() for e in self]):
|
||||
if all(e.is_recurrence() for e in self):
|
||||
return env['calendar.recurrence']
|
||||
if all([not e.is_recurrence() for e in self]):
|
||||
if all(not e.is_recurrence() for e in self):
|
||||
return env['calendar.event']
|
||||
raise TypeError("Mixing Microsoft events and Microsoft recurrences")
|
||||
|
||||
@@ -1030,7 +1030,7 @@ class Task(models.Model):
|
||||
days = list(DAYS.keys())
|
||||
week_start = fields.Datetime.today().weekday()
|
||||
|
||||
if all([d in default_fields for d in days]):
|
||||
if all(d in default_fields for d in days):
|
||||
vals[days[week_start]] = True
|
||||
if 'repeat_day' in default_fields:
|
||||
vals['repeat_day'] = str(fields.Datetime.today().day)
|
||||
|
||||
@@ -14,7 +14,7 @@ class ProjectDelete(models.TransientModel):
|
||||
|
||||
def _compute_projects_archived(self):
|
||||
for wizard in self.with_context(active_test=False):
|
||||
wizard.projects_archived = all([not p.active for p in wizard.project_ids])
|
||||
wizard.projects_archived = all(not p.active for p in wizard.project_ids)
|
||||
|
||||
def _compute_task_count(self):
|
||||
for wizard in self:
|
||||
|
||||
@@ -114,7 +114,7 @@ class PurchaseRequisition(models.Model):
|
||||
|
||||
def action_in_progress(self):
|
||||
self.ensure_one()
|
||||
if not all(obj.line_ids for obj in self):
|
||||
if not self.line_ids:
|
||||
raise UserError(_("You cannot confirm agreement '%s' because there is no product line.", self.name))
|
||||
if self.type_id.quantity_copy == 'none' and self.vendor_id:
|
||||
for requisition_line in self.line_ids:
|
||||
|
||||
@@ -54,7 +54,7 @@ class PurchaseOrder(models.Model):
|
||||
@api.depends('picking_ids', 'picking_ids.state')
|
||||
def _compute_is_shipped(self):
|
||||
for order in self:
|
||||
if order.picking_ids and all([x.state in ['done', 'cancel'] for x in order.picking_ids]):
|
||||
if order.picking_ids and all(x.state in ['done', 'cancel'] for x in order.picking_ids):
|
||||
order.is_shipped = True
|
||||
else:
|
||||
order.is_shipped = False
|
||||
|
||||
@@ -74,7 +74,7 @@ class SaleOrderLine(models.Model):
|
||||
# the products for this PO will set the qty_delivered. We might need to check the
|
||||
# state of all PO as well... but sale_mrp doesn't depend on purchase.
|
||||
if dropship:
|
||||
if order_line.move_ids and all([m.state == 'done' for m in order_line.move_ids]):
|
||||
if order_line.move_ids and all(m.state == 'done' for m in order_line.move_ids):
|
||||
order_line.qty_delivered = order_line.product_uom_qty
|
||||
else:
|
||||
order_line.qty_delivered = 0.0
|
||||
@@ -91,7 +91,7 @@ class SaleOrderLine(models.Model):
|
||||
# when the product sold is made only of kits. In this case, the BOM of the stock moves
|
||||
# do not correspond to the product sold => no relevant BOM.
|
||||
elif boms:
|
||||
if all([m.state == 'done' for m in order_line.move_ids]):
|
||||
if all(m.state == 'done' for m in order_line.move_ids):
|
||||
order_line.qty_delivered = order_line.product_uom_qty
|
||||
else:
|
||||
order_line.qty_delivered = 0.0
|
||||
|
||||
@@ -463,7 +463,7 @@ class Product(models.Model):
|
||||
def action_open_quants(self):
|
||||
domain = [('product_id', 'in', self.ids)]
|
||||
hide_location = not self.user_has_groups('stock.group_stock_multi_locations')
|
||||
hide_lot = all([product.tracking == 'none' for product in self])
|
||||
hide_lot = all(product.tracking == 'none' for product in self)
|
||||
self = self.with_context(
|
||||
hide_location=hide_location, hide_lot=hide_lot,
|
||||
no_at_date=True, search_default_on_hand=True,
|
||||
|
||||
@@ -1595,7 +1595,7 @@ class StockMove(models.Model):
|
||||
else:
|
||||
if move.procure_method == 'make_to_order' and not move.move_orig_ids:
|
||||
move.state = 'waiting'
|
||||
elif move.move_orig_ids and not all(orig.state in ('done', 'cancel') for orig in move.move_orig_ids):
|
||||
elif move.move_orig_ids and any(orig.state not in ('done', 'cancel') for orig in move.move_orig_ids):
|
||||
move.state = 'waiting'
|
||||
else:
|
||||
move.state = 'confirmed'
|
||||
|
||||
@@ -170,13 +170,13 @@ class WebsitePublishedMixin(models.AbstractModel):
|
||||
is_publish_modified = any(
|
||||
[set(v.keys()) & {'is_published', 'website_published'} for v in vals_list]
|
||||
)
|
||||
if is_publish_modified and not all(record.can_publish for record in records):
|
||||
if is_publish_modified and any(not record.can_publish for record in records):
|
||||
raise AccessError(self._get_can_publish_error_message())
|
||||
|
||||
return records
|
||||
|
||||
def write(self, values):
|
||||
if 'is_published' in values and not all(record.can_publish for record in self):
|
||||
if 'is_published' in values and any(not record.can_publish for record in self):
|
||||
raise AccessError(self._get_can_publish_error_message())
|
||||
|
||||
return super(WebsitePublishedMixin, self).write(values)
|
||||
|
||||
@@ -140,7 +140,7 @@
|
||||
</t>
|
||||
</div>
|
||||
</div>
|
||||
<form t-if="event.event_registrations_open and (not event.event_ticket_ids or not all([ticket.is_expired for ticket in event.event_ticket_ids]))"
|
||||
<form t-if="event.event_registrations_open and (not event.event_ticket_ids or any(not ticket.is_expired for ticket in event.event_ticket_ids))"
|
||||
id="registration_form"
|
||||
class="mb-5"
|
||||
t-attf-action="/event/#{slug(event)}/registration/new" method="post"
|
||||
|
||||
@@ -611,7 +611,7 @@ class Slide(models.Model):
|
||||
self.env.user.add_karma(karma_to_add)
|
||||
|
||||
def action_set_viewed(self, quiz_attempts_inc=False):
|
||||
if not all(slide.channel_id.is_member for slide in self):
|
||||
if any(not slide.channel_id.is_member for slide in self):
|
||||
raise UserError(_('You cannot mark a slide as viewed if you are not among its members.'))
|
||||
|
||||
return bool(self._action_set_viewed(self.env.user.partner_id, quiz_attempts_inc=quiz_attempts_inc))
|
||||
@@ -638,7 +638,7 @@ class Slide(models.Model):
|
||||
'vote': 0} for new_slide in new_slides])
|
||||
|
||||
def action_set_completed(self):
|
||||
if not all(slide.channel_id.is_member for slide in self):
|
||||
if any(not slide.channel_id.is_member for slide in self):
|
||||
raise UserError(_('You cannot mark a slide as completed if you are not among its members.'))
|
||||
|
||||
return self._action_set_completed(self.env.user.partner_id)
|
||||
@@ -663,7 +663,7 @@ class Slide(models.Model):
|
||||
return True
|
||||
|
||||
def _action_set_quiz_done(self):
|
||||
if not all(slide.channel_id.is_member for slide in self):
|
||||
if any(not slide.channel_id.is_member for slide in self):
|
||||
raise UserError(_('You cannot mark a slide quiz as completed if you are not among its members.'))
|
||||
|
||||
points = 0
|
||||
|
||||
@@ -621,7 +621,7 @@ class Module(models.Model):
|
||||
def button_uninstall(self):
|
||||
if 'base' in self.mapped('name'):
|
||||
raise UserError(_("The `base` module cannot be uninstalled"))
|
||||
if not all(state in ('installed', 'to upgrade') for state in self.mapped('state')):
|
||||
if any(state not in ('installed', 'to upgrade') for state in self.mapped('state')):
|
||||
raise UserError(_(
|
||||
"One or more of the selected modules have already been uninstalled, if you "
|
||||
"believe this to be an error, you may try again later or contact support."
|
||||
|
||||
@@ -89,7 +89,7 @@ class Lang(models.Model):
|
||||
'Provided as the thousand separator in each case.')
|
||||
for lang in self:
|
||||
try:
|
||||
if not all(isinstance(x, int) for x in json.loads(lang.grouping)):
|
||||
if any(not isinstance(x, int) for x in json.loads(lang.grouping)):
|
||||
raise ValidationError(warning)
|
||||
except Exception:
|
||||
raise ValidationError(warning)
|
||||
|
||||
+1
-1
@@ -5897,7 +5897,7 @@ Fields:
|
||||
|
||||
first_call = not names
|
||||
|
||||
if not all(name in self._fields for name in names):
|
||||
if any(name not in self._fields for name in names):
|
||||
return {}
|
||||
|
||||
def PrefixTree(model, dotnames):
|
||||
|
||||
Reference in New Issue
Block a user