Browse Source

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)
pull/120/head
Benjamin Henquet 6 years ago
parent
commit
a019a0e146
  1. 13
      website_coupon/__init__.py
  2. 4
      website_coupon/__manifest__.py
  3. 4
      website_coupon/controllers/main.py
  4. 1
      website_coupon/data/product_data.xml
  5. 20
      website_coupon/migrations/10.0.2.0.1/pre-migration.py
  6. 1
      website_coupon/models/__init__.py
  7. 16
      website_coupon/models/res_company.py
  8. 5
      website_coupon/wizards/__init__.py
  9. 5
      website_coupon/wizards/res_config/__init__.py
  10. 12
      website_coupon/wizards/res_config/sale_config_settings.py
  11. 13
      website_coupon/wizards/res_config/sale_config_settings.xml

13
website_coupon/__init__.py

@ -23,3 +23,16 @@
import models import models
import controllers 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

4
website_coupon/__manifest__.py

@ -23,7 +23,7 @@
############################################################################## ##############################################################################
{ {
'name': 'Website Coupon Code', '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', 'summary': 'Manage Website Coupon Codes for Products/Categories/All Products & Its Redeem Operations',
'category': 'Website', 'category': 'Website',
'author': 'Cybrosys Techno Solutions', 'author': 'Cybrosys Techno Solutions',
@ -36,6 +36,7 @@
'views/gift_voucher.xml', 'views/gift_voucher.xml',
'views/applied_coupons.xml', 'views/applied_coupons.xml',
'views/templates.xml', 'views/templates.xml',
'wizards/res_config/sale_config_settings.xml',
'security/ir.model.access.csv' 'security/ir.model.access.csv'
], ],
'images': ['static/description/banner.jpg'], 'images': ['static/description/banner.jpg'],
@ -43,4 +44,5 @@
'installable': True, 'installable': True,
'auto_install': False, 'auto_install': False,
'application': False, 'application': False,
"post_init_hook": "post_init_hook",
} }

4
website_coupon/controllers/main.py

@ -79,12 +79,12 @@ class WebsiteCoupon(http.Controller):
voucher_type = coupon.voucher.voucher_type voucher_type = coupon.voucher.voucher_type
voucher_val = coupon.voucher_val voucher_val = coupon.voucher_val
type = coupon.type 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: if coupon_product:
order = request.website.sale_get_order(force_create=1) order = request.website.sale_get_order(force_create=1)
flag_product = False flag_product = False
for line in order.order_line: for line in order.order_line:
if line.product_id.name == 'Gift Coupon': if line.product_id == coupon_product:
flag = False flag = False
break break
if flag and order.order_line: if flag and order.order_line:

1
website_coupon/data/product_data.xml

@ -2,6 +2,7 @@
<odoo noupdate="1"> <odoo noupdate="1">
<record model="product.product" id="discount_product"> <record model="product.product" id="discount_product">
<field name="name">Gift Coupon</field> <field name="name">Gift Coupon</field>
<field name="default_code">gift_coupon</field>
</record> </record>
</odoo> </odoo>

20
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)

1
website_coupon/models/__init__.py

@ -21,3 +21,4 @@
# #
############################################################################## ##############################################################################
import gift_voucher import gift_voucher
from . import res_company

16
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),
)

5
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

5
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

12
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")

13
website_coupon/wizards/res_config/sale_config_settings.xml

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_sales_config_inherit_sale" model="ir.ui.view">
<field name="name">sale settings</field>
<field name="model">sale.config.settings</field>
<field name="inherit_id" ref="sale.view_sales_config"/>
<field name="arch" type="xml">
<field name="deposit_product_id_setting" position="after">
<field name="gift_coupon_product_id"/>
</field>
</field>
</record>
</odoo>
Loading…
Cancel
Save