From a019a0e146cd1311e9833797e761f92a45268437 Mon Sep 17 00:00:00 2001 From: Benjamin Henquet Date: Wed, 26 Jun 2019 15:52:38 +0200 Subject: [PATCH] Prevent user product name modification (or translation) If the user modify or translate the name of the product "Gift Coupon" the system will be no more able to find it. So, I: 1) Create a field in the sale parameters page (related to a res.company field to be stored) to select the product use in sale order line 2) Use this parameter in the controller 3) Create a migration method for existing database with this module (so just pull the module version on the customer server, restart the server and update the module) --- website_coupon/__init__.py | 13 ++++++++++++ website_coupon/__manifest__.py | 4 +++- website_coupon/controllers/main.py | 4 ++-- website_coupon/data/product_data.xml | 1 + .../migrations/10.0.2.0.1/pre-migration.py | 20 +++++++++++++++++++ website_coupon/models/__init__.py | 1 + website_coupon/models/res_company.py | 16 +++++++++++++++ website_coupon/wizards/__init__.py | 5 +++++ website_coupon/wizards/res_config/__init__.py | 5 +++++ .../res_config/sale_config_settings.py | 12 +++++++++++ .../res_config/sale_config_settings.xml | 13 ++++++++++++ 11 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 website_coupon/migrations/10.0.2.0.1/pre-migration.py create mode 100644 website_coupon/models/res_company.py create mode 100644 website_coupon/wizards/__init__.py create mode 100644 website_coupon/wizards/res_config/__init__.py create mode 100644 website_coupon/wizards/res_config/sale_config_settings.py create mode 100644 website_coupon/wizards/res_config/sale_config_settings.xml diff --git a/website_coupon/__init__.py b/website_coupon/__init__.py index 1ded18c1b..78752a19a 100644 --- a/website_coupon/__init__.py +++ b/website_coupon/__init__.py @@ -23,3 +23,16 @@ import models import controllers +from . import wizards + +from odoo import SUPERUSER_ID +from odoo.api import Environment + + +def post_init_hook(cr, registry): + env = Environment(cr, SUPERUSER_ID, {}) + companies = env['res.company'].search([]) + gift_coupon_product_id = env['product.product'].search( + [('default_code', '=', 'gift_coupon')], limit=1) + for company in companies: + company.gift_coupon_product_id = gift_coupon_product_id diff --git a/website_coupon/__manifest__.py b/website_coupon/__manifest__.py index c161555f3..b1a8136ea 100644 --- a/website_coupon/__manifest__.py +++ b/website_coupon/__manifest__.py @@ -23,7 +23,7 @@ ############################################################################## { 'name': 'Website Coupon Code', - 'version': '10.0.2.0.0', + 'version': '10.0.2.0.1', 'summary': 'Manage Website Coupon Codes for Products/Categories/All Products & Its Redeem Operations', 'category': 'Website', 'author': 'Cybrosys Techno Solutions', @@ -36,6 +36,7 @@ 'views/gift_voucher.xml', 'views/applied_coupons.xml', 'views/templates.xml', + 'wizards/res_config/sale_config_settings.xml', 'security/ir.model.access.csv' ], 'images': ['static/description/banner.jpg'], @@ -43,4 +44,5 @@ 'installable': True, 'auto_install': False, 'application': False, + "post_init_hook": "post_init_hook", } diff --git a/website_coupon/controllers/main.py b/website_coupon/controllers/main.py index 965797d74..30cc5eb94 100644 --- a/website_coupon/controllers/main.py +++ b/website_coupon/controllers/main.py @@ -79,12 +79,12 @@ class WebsiteCoupon(http.Controller): voucher_type = coupon.voucher.voucher_type voucher_val = coupon.voucher_val type = coupon.type - coupon_product = request.env['product.product'].sudo().search([('name', '=', 'Gift Coupon')], limit=1) + coupon_product = request.env.user.company_id.gift_coupon_product_id if coupon_product: order = request.website.sale_get_order(force_create=1) flag_product = False for line in order.order_line: - if line.product_id.name == 'Gift Coupon': + if line.product_id == coupon_product: flag = False break if flag and order.order_line: diff --git a/website_coupon/data/product_data.xml b/website_coupon/data/product_data.xml index bd5871987..6fa3e84ce 100644 --- a/website_coupon/data/product_data.xml +++ b/website_coupon/data/product_data.xml @@ -2,6 +2,7 @@ Gift Coupon + gift_coupon \ No newline at end of file diff --git a/website_coupon/migrations/10.0.2.0.1/pre-migration.py b/website_coupon/migrations/10.0.2.0.1/pre-migration.py new file mode 100644 index 000000000..725448714 --- /dev/null +++ b/website_coupon/migrations/10.0.2.0.1/pre-migration.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Noviat NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import odoo + + +def update_discount_product_value(env): + discount_product = env.ref( + 'website_coupon.discount_product') + discount_product.default_code = 'gift_coupon' + + +def migrate(cr, version): + if not version: + # installation of the module + return + with odoo.api.Environment.manage(): + env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {}) + update_discount_product_value(env) diff --git a/website_coupon/models/__init__.py b/website_coupon/models/__init__.py index d91b1374f..2a9b5d73f 100644 --- a/website_coupon/models/__init__.py +++ b/website_coupon/models/__init__.py @@ -21,3 +21,4 @@ # ############################################################################## import gift_voucher +from . import res_company diff --git a/website_coupon/models/res_company.py b/website_coupon/models/res_company.py new file mode 100644 index 000000000..dcf3130e0 --- /dev/null +++ b/website_coupon/models/res_company.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Noviat NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models, fields, api + + +class ResCompany(models.Model): + _inherit = 'res.company' + + gift_coupon_product_id = fields.Many2one( + comodel_name='product.product', + string="Gift coupon", + default=lambda self: self.env['product.product'].search( + [('default_code', '=', 'gift_coupon')], limit=1), + ) diff --git a/website_coupon/wizards/__init__.py b/website_coupon/wizards/__init__.py new file mode 100644 index 000000000..742b150eb --- /dev/null +++ b/website_coupon/wizards/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Noviat NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import res_config diff --git a/website_coupon/wizards/res_config/__init__.py b/website_coupon/wizards/res_config/__init__.py new file mode 100644 index 000000000..aeccbad69 --- /dev/null +++ b/website_coupon/wizards/res_config/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Noviat NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import sale_config_settings diff --git a/website_coupon/wizards/res_config/sale_config_settings.py b/website_coupon/wizards/res_config/sale_config_settings.py new file mode 100644 index 000000000..2aa6e4809 --- /dev/null +++ b/website_coupon/wizards/res_config/sale_config_settings.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 Noviat NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class SaleConfigSettings(models.TransientModel): + _inherit = 'sale.config.settings' + + gift_coupon_product_id = fields.Many2one( + related='company_id.gift_coupon_product_id', string="Gift coupon") diff --git a/website_coupon/wizards/res_config/sale_config_settings.xml b/website_coupon/wizards/res_config/sale_config_settings.xml new file mode 100644 index 000000000..f4f568b72 --- /dev/null +++ b/website_coupon/wizards/res_config/sale_config_settings.xml @@ -0,0 +1,13 @@ + + + + sale settings + sale.config.settings + + + + + + + +