From 2d1c973034a137d1bb49780cc7b64146bbebb11c Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Tue, 20 Sep 2022 19:45:24 +0000 Subject: [PATCH] [IMP] delivery_hibou: improve dynamic calls --- delivery_hibou/models/delivery.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/delivery_hibou/models/delivery.py b/delivery_hibou/models/delivery.py index 220eaae3..20f98e44 100644 --- a/delivery_hibou/models/delivery.py +++ b/delivery_hibou/models/delivery.py @@ -294,16 +294,16 @@ class DeliveryCarrier(models.Model): p.package_type_id.package_carrier_type in (False, '', 'none', carrier.delivery_type)) if packages and not carrier_packages: continue - if hasattr(carrier, '%s_rate_shipment_multi' % self.delivery_type): + attr = getattr(carrier, '%s_rate_shipment_multi' % self.delivery_type, None) + if attr: try: - res += getattr(carrier, '%s_rate_shipment_multi' % carrier.delivery_type)(order=order, - picking=picking, - packages=carrier_packages) + res += attr(order=order, picking=picking, packages=carrier_packages) except TypeError: # TODO remove catch if after Odoo 14 # This is intended to find ones that don't support packages= kwarg - res += getattr(carrier, '%s_rate_shipment_multi' % carrier.delivery_type)(order=order, - picking=picking) + res2 = attr(order=order, picking=picking) + if res2: + res += res2 return res @@ -314,11 +314,12 @@ class DeliveryCarrier(models.Model): :param packages: Optional recordset of packages (should be for this carrier) ''' self.ensure_one() - if hasattr(self, '%s_cancel_shipment' % self.delivery_type): + attr = getattr(self, '%s_cancel_shipment' % self.delivery_type, None) + if attr: # No good way to tell if this method takes the kwarg for packages if packages: try: - return getattr(self, '%s_cancel_shipment' % self.delivery_type)(pickings, packages=packages) + return attr(pickings, packages=packages) except TypeError: # we won't be able to cancel the packages properly # here we will TRY to make a good call here where we put the package references into the picking @@ -329,7 +330,7 @@ class DeliveryCarrier(models.Model): 'carrier_tracking_ref': tracking_ref, }) - return getattr(self, '%s_cancel_shipment' % self.delivery_type)(pickings) + return attr(pickings) class ChooseDeliveryPackage(models.TransientModel):