Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addons/purchase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from . import models
from . import report
from . import populate
from .hooks import pre_init_hook
1 change: 1 addition & 0 deletions addons/purchase/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'demo': [
'data/purchase_demo.xml',
],
"pre_init_hook": "pre_init_hook",
'installable': True,
'application': True,
'assets': {
Expand Down
20 changes: 20 additions & 0 deletions addons/purchase/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging

from odoo.tools import column_exists

_logger = logging.getLogger(__name__)


def pre_init_hook(cr):
pre_create_purchase_method(cr)


def pre_create_purchase_method(cr):
"""Create the purchase_method field in product.template if it does not exist (added in "purchase" dependency).
Also, set the default value to 'purchase' for existing products by query instead of computing it with Python.
"""
if not column_exists(cr, "product_template", "purchase_method"):
cr.execute("ALTER TABLE product_template ADD COLUMN purchase_method character varying;")
_logger.info("Column 'purchase_method' added to product_template table.")
cr.execute("UPDATE product_template AS pt SET purchase_method = 'purchase';")
_logger.info("Setting purchase_method to 'purchase' for %s products", cr.rowcount)