|
| 1 | +import random |
| 2 | + |
| 3 | +from odoo import _, api, fields, models |
| 4 | +from odoo.exceptions import AccessError, UserError |
| 5 | +from odoo.http import request |
| 6 | + |
| 7 | + |
| 8 | +class IrUiView(models.Model): |
| 9 | + _inherit = "ir.ui.view" |
| 10 | + |
| 11 | + ab_testing_enabled = fields.Boolean(string="A/B Testing", copy=False) |
| 12 | + |
| 13 | + master_id = fields.Many2one(comodel_name="ir.ui.view", copy=False) |
| 14 | + |
| 15 | + variant_ids = fields.One2many( |
| 16 | + comodel_name="ir.ui.view", inverse_name="master_id", string="Variants" |
| 17 | + ) |
| 18 | + |
| 19 | + def render(self, values=None, engine="ir.qweb", minimal_qcontext=False): |
| 20 | + website = self.env["website"].get_current_website() |
| 21 | + if ( |
| 22 | + request and request.session and website |
| 23 | + and self.ab_testing_enabled |
| 24 | + and not self.env.user.has_group("website.group_website_publisher") |
| 25 | + ): |
| 26 | + if "ab_testing" not in request.session: |
| 27 | + request.session["ab_testing"] = {"active_variants": {}} |
| 28 | + if self.id not in request.session["ab_testing"]["active_variants"]: |
| 29 | + random_index = random.randint(0, len(self.variant_ids)) |
| 30 | + selected_view = self |
| 31 | + if random_index: |
| 32 | + selected_view = self.variant_ids[random_index - 1] |
| 33 | + ab_testing = request.session["ab_testing"].copy() |
| 34 | + ab_testing["active_variants"][self.id] = selected_view.id |
| 35 | + request.session["ab_testing"] = ab_testing |
| 36 | + return selected_view.render(values, engine, minimal_qcontext) |
| 37 | + else: |
| 38 | + selection_view_id = request.session["ab_testing"]["active_variants"][ |
| 39 | + self.id |
| 40 | + ] |
| 41 | + if selection_view_id == self.id: |
| 42 | + return super().render(values, engine, minimal_qcontext) |
| 43 | + selected_view = self.search([("id", "=", selection_view_id)]) |
| 44 | + if selected_view: |
| 45 | + return selected_view.render(values, engine, minimal_qcontext) |
| 46 | + ab_testing = request.session["ab_testing"].copy() |
| 47 | + del ab_testing["active_variants"][self.id] |
| 48 | + elif request and request.session and website and self.env.user.has_group("website.group_website_publisher"): |
| 49 | + variants = self.env["ir.ui.view"] |
| 50 | + if self.master_id: |
| 51 | + variants += self.master_id |
| 52 | + variants += self.master_id.variant_ids |
| 53 | + else: |
| 54 | + variants += self |
| 55 | + variants += self.variant_ids |
| 56 | + if values is None: |
| 57 | + values = {} |
| 58 | + values["ab_testing_variants"] = variants |
| 59 | + |
| 60 | + if ( |
| 61 | + "ab_testing" in request.session |
| 62 | + and not self.master_id |
| 63 | + and self.id in request.session["ab_testing"]["active_variants"] |
| 64 | + ): |
| 65 | + active_variant = self.variant_ids.filtered( |
| 66 | + lambda v: v.id |
| 67 | + == request.session["ab_testing"]["active_variants"][self.id] |
| 68 | + ) |
| 69 | + if active_variant: |
| 70 | + values["active_variant"] = active_variant |
| 71 | + return active_variant.render(values, engine, minimal_qcontext) |
| 72 | + |
| 73 | + return super().render(values, engine, minimal_qcontext) |
| 74 | + |
| 75 | + def create_variant(self, name): |
| 76 | + self.ensure_one() |
| 77 | + if self.master_id: |
| 78 | + raise UserError(_("Cannot create variant of variant.")) |
| 79 | + if self.variant_ids.filtered(lambda v: v.name == name): |
| 80 | + raise UserError(_("Variant '%s' already exists.") % name) |
| 81 | + variant = self.copy({"name": name, "master_id": self.id}) |
| 82 | + self._copy_inheritance(variant.id) |
| 83 | + return variant.id |
| 84 | + |
| 85 | + def _copy_inheritance(self, new_id): |
| 86 | + """Copy the inheritance recursively""" |
| 87 | + for view in self: |
| 88 | + for child in view.inherit_children_ids: |
| 89 | + copy = child.copy({"inherit_id": new_id}) |
| 90 | + child._copy_inheritance(copy.id) |
| 91 | + |
| 92 | + def toggle_ab_testing_enabled(self): |
| 93 | + self.ensure_one() |
| 94 | + if self.master_id: |
| 95 | + raise UserError(_("This is not the master page.")) |
| 96 | + self.ab_testing_enabled = not self.ab_testing_enabled |
| 97 | + |
| 98 | + def switch_variant(self, variant_id): |
| 99 | + self.ensure_one() |
| 100 | + if not self.env.user.has_group("website.group_website_publisher"): |
| 101 | + raise AccessError( |
| 102 | + _("Cannot deliberately switch variant as non-designer user.") |
| 103 | + ) |
| 104 | + if not variant_id: |
| 105 | + raise UserError(_("No variant specified.")) |
| 106 | + |
| 107 | + if "ab_testing" not in request.session: |
| 108 | + request.session["ab_testing"] = {"active_variants": {}} |
| 109 | + ab_testing = request.session["ab_testing"].copy() |
| 110 | + ab_testing["active_variants"][self.id] = variant_id |
| 111 | + request.session["ab_testing"] = ab_testing |
| 112 | + |
| 113 | + @api.model |
| 114 | + def get_active_variants(self): |
| 115 | + if "ab_testing" not in request.session: |
| 116 | + request.session["ab_testing"] = {"active_variants": {}} |
| 117 | + ids = list(request.session["ab_testing"]["active_variants"].values()) |
| 118 | + return self.search([("id", "in", ids)]) |
0 commit comments