Browse Source

[FIX] avoid monkey patch and error on foreign key constraint

pull/182/head
Cédric Pigeon 8 years ago
parent
commit
e145f651cd
  1. 1
      pos_remove_pos_category/README.rst
  2. 5
      pos_remove_pos_category/__manifest__.py
  3. 50
      pos_remove_pos_category/models/product.py

1
pos_remove_pos_category/README.rst

@ -48,6 +48,7 @@ Contributors
* Sylvain Calador <sylvain.calador@akretion.com>
* Simone Orsi <simone.orsi@camptocamp.com>
* Cédric Pigeon <cedric.pigeon@acsone.eu>
Maintainer
----------

5
pos_remove_pos_category/__manifest__.py

@ -3,8 +3,9 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
'name': 'POS Remove POS Category',
'version': '10.0.0.1.0',
'author': 'Akretion, Camptocamp SA, Odoo Community Association (OCA)',
'version': '10.0.2.0.0',
'author': 'Akretion, Camptocamp SA, ACSONE SA/NV, '
'Odoo Community Association (OCA)',
'category': 'Sales Management',
'depends': [
'point_of_sale',

50
pos_remove_pos_category/models/product.py

@ -2,7 +2,6 @@
# Copyright (C) 2015-TODAY Akretion (<http://www.akretion.com>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import sys
from odoo import models, fields, api, tools
@ -10,7 +9,13 @@ class ProductTemplate(models.Model):
_inherit = 'product.template'
pos_categ_id = fields.Many2one('product.category',
store=True, related='categ_id')
store=False,
related='categ_id',
search='_search_pos_categ_id')
@api.multi
def _search_pos_categ_id(self, operator, value):
return [('categ_id', operator, value)]
@api.model
def create(self, vals):
@ -50,44 +55,3 @@ class ProductCategory(models.Model):
def _set_image(self):
return self.write(
{'image': tools.image_resize_image_big(self.image_medium)})
_auto_end_original = models.BaseModel._auto_end
@api.model
def _auto_end(self):
""" Create the foreign keys recorded by _auto_init.
(pos_remove_pos_category monkey patching)
"""
module = self._context['module']
foreign_keys = []
patched = 'odoo.addons.pos_remove_pos_category' in sys.modules
for fk in self._foreign_keys:
t = fk[0]
k = fk[1]
if patched and (t, k) == ('product_template', 'pos_categ_id'):
if module == 'pos_remove_pos_category':
self._cr.execute('''
ALTER TABLE product_template
DROP CONSTRAINT IF EXISTS
product_template_pos_categ_id_fkey
''')
self._cr.execute('''
UPDATE product_template
SET pos_categ_id = categ_id;
''')
self._cr.execute('''
ALTER TABLE product_template ADD CONSTRAINT
"product_template_pos_categ_id_fkey"
FOREIGN KEY (pos_categ_id)
REFERENCES product_category(id) ON DELETE SET NULL;
''')
continue
foreign_keys.append(fk)
self._foreign_keys = foreign_keys
return _auto_end_original
models.BaseModel._auto_end = _auto_end
Loading…
Cancel
Save