From 3bead2cbea215235904eca7f5c510bcbeaf03e02 Mon Sep 17 00:00:00 2001 From: "Antoine Vandevenne (anv)" Date: Fri, 16 Sep 2022 08:03:21 +0000 Subject: [PATCH] [CLN] payment: code cleanup This commit removes unused imports and renames some confusing variable names and helper texts that were changed with commit f7b8f075 when renaming the term "acquirer" to "provider". closes odoo/odoo#100348 Signed-off-by: Antoine Vandevenne (anv) --- addons/payment/controllers/portal.py | 11 +++--- addons/payment/models/payment_provider.py | 43 ++++++++++++++--------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/addons/payment/controllers/portal.py b/addons/payment/controllers/portal.py index 60bff3b3377..daa70debc17 100644 --- a/addons/payment/controllers/portal.py +++ b/addons/payment/controllers/portal.py @@ -5,7 +5,6 @@ import werkzeug from odoo import _, http from odoo.exceptions import AccessError, UserError, ValidationError -from odoo.fields import Command from odoo.http import request from odoo.addons.payment import utils as payment_utils @@ -17,8 +16,8 @@ class PaymentPortal(portal.CustomerPortal): """ This controller contains the foundations for online payments through the portal. - It allows to complete a full payment flow without the need of going though a document-based flow - made available by another module's controller. + It allows to complete a full payment flow without the need of going through a document-based + flow made available by another module's controller. Such controllers should extend this one to gain access to the _create_transaction static method that implements the creation of a transaction before its processing, or to override specific @@ -74,7 +73,7 @@ class PaymentPortal(portal.CustomerPortal): # Raise an HTTP 404 if a partner is provided with an invalid access token if partner_id: if not payment_utils.check_access_token(access_token, partner_id, amount, currency_id): - raise werkzeug.exceptions.NotFound # Don't leak info about the existence of an id + raise werkzeug.exceptions.NotFound() # Don't leak information about ids. user_sudo = request.env.user logged_in = not user_sudo._is_public() @@ -105,7 +104,7 @@ class PaymentPortal(portal.CustomerPortal): # Make sure that the currency exists and is active currency = request.env['res.currency'].browse(currency_id).exists() if not currency or not currency.active: - raise werkzeug.exceptions.NotFound # The currency must exist and be active + raise werkzeug.exceptions.NotFound() # The currency must exist and be active. # Select all providers and tokens that match the constraints providers_sudo = request.env['payment.provider'].sudo()._get_compatible_providers( @@ -371,7 +370,7 @@ class PaymentPortal(portal.CustomerPortal): if not payment_utils.check_access_token( access_token, tx_sudo.partner_id.id, tx_sudo.amount, tx_sudo.currency_id.id ): - raise werkzeug.exceptions.NotFound # Don't leak info about existence of an id + raise werkzeug.exceptions.NotFound() # Don't leak information about ids. # Stop monitoring the transaction now that it reached a final state. PaymentPostProcessing.remove_transactions(tx_sudo) diff --git a/addons/payment/models/payment_provider.py b/addons/payment/models/payment_provider.py index 97a2079868f..c837a7187a6 100644 --- a/addons/payment/models/payment_provider.py +++ b/addons/payment/models/payment_provider.py @@ -4,7 +4,7 @@ import logging from psycopg2 import sql -from odoo import _, api, fields, models, SUPERUSER_ID +from odoo import _, api, fields, models from odoo.exceptions import UserError, ValidationError from odoo.osv import expression @@ -23,8 +23,12 @@ class PaymentProvider(models.Model): name = fields.Char(string="Name", required=True, translate=True) sequence = fields.Integer(string="Sequence", help="Define the display order") code = fields.Selection( - string="Code", help="The Payment Service Code to use with this provider", - selection=[('none', "No Provider Set")], default='none', required=True) + string="Code", + help="The technical code of this payment provider.", + selection=[('none', "No Provider Set")], + default='none', + required=True, + ) state = fields.Selection( string="State", help="In test mode, a fake payment is processed through a test payment interface.\n" @@ -305,23 +309,23 @@ class PaymentProvider(models.Model): def _check_required_if_provider(self): """ Check that provider-specific required fields have been filled. - The fields that have the `required_if_provider=""` attribute are made required - for all payment.provider records with the `code` field equal to and with the + The fields that have the `required_if_provider=''` attribute are made required + for all `payment.provider` records with the `code` field equal to and with the `state` field equal to 'enabled' or 'test'. Provider-specific views should make the form fields required under the same conditions. :return: None - :raise ValidationError: if a provider-specific required field is empty + :raise ValidationError: If a provider-specific required field is empty. """ field_names = [] enabled_providers = self.filtered(lambda p: p.state in ['enabled', 'test']) - for name, field in self._fields.items(): - required_provider = getattr(field, 'required_if_provider', None) - if required_provider and any( - required_provider == provider.code and not provider[name] + for field_name, field in self._fields.items(): + required_for_provider_code = getattr(field, 'required_if_provider', None) + if required_for_provider_code and any( + required_for_provider_code == provider.code and not provider[field_name] for provider in enabled_providers ): - ir_field = self.env['ir.model.fields']._get(self._name, name) + ir_field = self.env['ir.model.fields']._get(self._name, field_name) field_names.append(ir_field.field_description) if field_names: raise ValidationError( @@ -565,10 +569,12 @@ class PaymentProvider(models.Model): WHERE state NOT IN ('test', 'disabled') """) - def _neutralize_fields(self, provider, fields): - """ Helper to neutralize API keys for a specific provider - :param str provider: name of provider - :param list fields: list of fields to nullify + def _neutralize_fields(self, provider_code, field_names): + """ Helper to neutralize API keys for a specific provider. + + :param str provider_code: The code of the provider. + :param list field_names: The names of the fields to nullify. + :return: None """ self.flush_model() self.invalidate_model() @@ -576,5 +582,8 @@ class PaymentProvider(models.Model): UPDATE payment_provider SET ({fields}) = ROW({vals}) WHERE code = %s - """).format(fields=sql.SQL(','.join(fields)), vals=sql.SQL(', '.join(['NULL'] * len(fields)))) - self.env.cr.execute(query, (provider, )) + """).format( + fields=sql.SQL(','.join(field_names)), + vals=sql.SQL(', '.join(['NULL'] * len(field_names))), + ) + self.env.cr.execute(query, (provider_code, ))