Debian Codegraph @

addons/website_sale_loyalty/models/loyalty_rule.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import _, api, fields, models
4 from odoo.exceptions import ValidationError
5
6
7 class LoyaltyRule(models.Model):
8 _inherit = 'loyalty.rule'
9
10 website_id = fields.Many2one(related='program_id.website_id', store=True)
11
12 # NOTE: is this sufficient?
13 @api.constrains('code', 'website_id', 'active')
14 def _constrains_code(self):
15 #Programs with the same code are allowed to coexist as long
16 # as they are not both accessible from a website.
17 with_code = self.filtered(lambda r: r.mode == 'with_code' and r.active)
18 mapped_codes = with_code.mapped('code')
19 read_result = self.env['loyalty.rule'].search_read(
20 [('website_id', 'in', [False] + [w.id for w in self.website_id]),
21 ('mode', '=', 'with_code'),
22 ('code', 'in', mapped_codes),
23 ('id', 'not in', with_code.ids),
24 ('active', '=', True)],
25 fields=['code', 'website_id']) + [{'code': p.code, 'website_id': p.website_id} for p in with_code]
26 existing_codes = set()
27 for res in read_result:
28 website_checks = (res['website_id'], False) if res['website_id'] else (False,)
29 for website in website_checks:
30 val = (res['code'], website)
31 if val in existing_codes:
32 raise ValidationError(_('The promo code must be unique.'))
33 existing_codes.add(val)
34 # Prevent coupons and programs from sharing a code
35 if self.env['loyalty.card'].search_count([
36 ('code', 'in', mapped_codes), ('active', '=', True)
37 ]):
38 raise ValidationError(_('A coupon with the same code was found.'))