From 3260c6c801105fc3203b492e4bd7fdef9ea68feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 1 Jun 2015 15:09:37 +0200 Subject: [PATCH] [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. --- .../models/__init__.py | 4 +- .../models/payment_mode.py | 48 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/account_banking_payment_export/models/__init__.py b/account_banking_payment_export/models/__init__.py index 58f25e448..797c1ddf3 100644 --- a/account_banking_payment_export/models/__init__.py +++ b/account_banking_payment_export/models/__init__.py @@ -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 diff --git a/account_banking_payment_export/models/payment_mode.py b/account_banking_payment_export/models/payment_mode.py index d8333339a..1e41fc98b 100644 --- a/account_banking_payment_export/models/payment_mode.py +++ b/account_banking_payment_export/models/payment_mode.py @@ -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.