Debian Codegraph @

addons/website_sale_loyalty/wizard/coupon_share.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from werkzeug.urls import url_encode
4
5 from odoo import _, api, fields, models
6 from odoo.exceptions import UserError, ValidationError
7
8
9 class CouponShare(models.TransientModel):
10 _name = 'coupon.share'
11 _description = 'Create links that apply a coupon and redirect to a specific page'
12
13 def _get_default_website_id(self):
14 program_website_id = self.env['loyalty.program'].browse(self.env.context.get('default_program_id')).website_id
15 if program_website_id:
16 return program_website_id
17 else:
18 Website = self.env['website']
19 websites = Website.search([])
20 return len(websites) == 1 and websites or Website
21
22 website_id = fields.Many2one('website', required=True, default=_get_default_website_id)
23 coupon_id = fields.Many2one('loyalty.card', domain="[('program_id', '=', program_id)]")
24 program_id = fields.Many2one('loyalty.program', required=True, domain=[
25 '|', ('program_type', '=', 'coupons'), # All coupons programs
26 '|', ('trigger', '=', 'with_code'), # All programs that require a code
27 ('rule_ids.code', '!=', False), # All programs that can not trigger without a code
28 ])
29 program_website_id = fields.Many2one('website', string='Program Website', related='program_id.website_id')
30
31 promo_code = fields.Char(compute='_compute_promo_code')
32 share_link = fields.Char(compute='_compute_share_link')
33 redirect = fields.Char(required=True, default='/shop')
34
35 @api.constrains('coupon_id', 'program_id')
36 def _check_program(self):
37 if self.filtered(lambda record: not record.coupon_id and record.program_id.program_type == 'coupons'):
38 raise ValidationError(_("A coupon is needed for coupon programs."))
39
40 @api.constrains('website_id', 'program_id')
41 def _check_website(self):
42 if self.filtered(lambda record: record.program_website_id and record.program_website_id != record.website_id):
43 raise ValidationError(_("The shared website should correspond to the website of the program."))
44
45 @api.depends('coupon_id.code', 'program_id.rule_ids.code')
46 def _compute_promo_code(self):
47 for record in self:
48 record.promo_code = record.coupon_id.code or record.program_id.rule_ids.filtered('code')[:1].code
49
50 @api.depends('website_id', 'redirect')
51 @api.depends_context('use_short_link')
52 def _compute_share_link(self):
53 for record in self:
54 target_url = '{base}/coupon/{code}?{query}'.format(
55 base=record.website_id.get_base_url(),
56 code=record.promo_code,
57 query=url_encode({'r': record.redirect}),
58 )
59
60 if record.env.context.get('use_short_link'):
61 tracker = self.env['link.tracker'].search([('url', '=', target_url)], limit=1)
62 if not tracker:
63 tracker = self.env['link.tracker'].create({'url': target_url})
64 record.share_link = tracker.short_url
65 else:
66 record.share_link = target_url
67
68 def action_generate_short_link(self):
69 return {
70 'name': _('Share'),
71 'type': 'ir.actions.act_window',
72 'view_mode': 'form',
73 'res_model': 'coupon.share',
74 'target': 'new',
75 'res_id': self.id,
76 'context': {
77 'use_short_link': True,
78 }
79 }
80
81 @api.model
82 def create_share_action(self, coupon=None, program=None):
83 if bool(program) == bool(coupon):
84 raise UserError(_("Provide either a coupon or a program."))
85
86 return {
87 'name': _('Share %s', self.env["loyalty.program"]._program_items_name().get((program or coupon).program_type, "")),
88 'type': 'ir.actions.act_window',
89 'view_mode': 'form',
90 'res_model': 'coupon.share',
91 'target': 'new',
92 'context': {
93 'default_program_id': program and program.id or coupon.program_id.id,
94 'default_coupon_id': coupon and coupon.id or None,
95 }
96 }