[FIX] let the not null constraint on payment_mode.type be created correctly

This is a hack: in an ideal world, Odoo would set such
constraint after module install, not before.
This commit is contained in:
Stéphane Bidoul
2015-06-01 15:09:37 +02:00
parent c07d0522c0
commit 3260c6c801
2 changed files with 50 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from . import account_payment
from . import payment_mode
# important: import payment_mode_type before payment_mode
# to let the _auto_init work properly
from . import payment_mode_type
from . import payment_mode
from . import account_move_line
from . import account_invoice

View File

@@ -23,7 +23,7 @@
#
##############################################################################
from openerp import models, fields, api
from openerp import models, fields, api, SUPERUSER_ID
class PaymentMode(models.Model):
@@ -32,6 +32,52 @@ class PaymentMode(models.Model):
"""
_inherit = "payment.mode"
def _get_manual_bank_transfer(self, cr, uid, context=None):
""" hack: pre-create the manual bank transfer that is also
defined in the data directory, so we have an id in to use
in _auto_init """
model_data = self.pool['ir.model.data']
try:
_, res = model_data.get_object_reference(
cr, uid,
'account_banking_payment_export',
'manual_bank_tranfer')
except ValueError:
payment_mode_type = self.pool['payment.mode.type']
res = payment_mode_type.create(
cr, uid,
{'name': 'Manual Bank Transfer',
'code': 'BANKMAN'})
model_data.create(
cr, uid,
{'module': 'account_banking_payment_export',
'model': 'payment.mode.type',
'name': 'manual_bank_tranfer',
'res_id': res,
'noupdate': False})
return res
def _auto_init(self, cr, context=None):
""" hack: pre-create and initialize the type column so that the
constraint setting will not fail, this is a hack, made necessary
because Odoo tries to set the not-null constraint before
applying default values """
self._field_create(cr, context=context)
column_data = self._select_column_data(cr)
if 'type' not in column_data:
default_type = self._get_manual_bank_transfer(
cr, SUPERUSER_ID, context=context)
if default_type:
cr.execute('ALTER TABLE "{table}" ADD COLUMN "type" INTEGER'.
format(table=self._table))
cr.execute('UPDATE "{table}" SET type=%s'.
format(table=self._table),
(default_type,))
else:
_logger.warning("manual bank transfer payment mode "
"type not found")
return super(PaymentMode, self)._auto_init(cr, context=context)
def suitable_bank_types(self, cr, uid, payment_mode_id=None, context=None):
""" Reinstates functional code for suitable bank type filtering.
Current code in account_payment is disfunctional.