From 4d9e276f2b58d540473acfe84c4bb407abb6138a Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Mon, 8 Sep 2025 04:56:09 +0300 Subject: [PATCH] Features: 1) Add handling for multiple AMO CRM providers in gateway initialization; 2) Suppress CRMException in CRM-related signal processing; Fixes: 1) Correct provider name from "amo" to "AmoCRM"; Extra: 1) Remove unused `time` import and redundant token caching logic; 2) Add missing CRMException import to signals; 3) Minor code cleanup and readability improvements; --- core/crm/amo/gateway.py | 10 ++++------ core/signals.py | 5 ++++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/core/crm/amo/gateway.py b/core/crm/amo/gateway.py index eb2de7b9..19ee2585 100644 --- a/core/crm/amo/gateway.py +++ b/core/crm/amo/gateway.py @@ -1,5 +1,4 @@ import logging -import time import traceback from typing import Optional @@ -18,10 +17,13 @@ class AmoCRM: def __init__(self): try: - self.instance = CustomerRelationshipManagementProvider.objects.get(name="amo") + self.instance = CustomerRelationshipManagementProvider.objects.get(name="AmoCRM") except CustomerRelationshipManagementProvider.DoesNotExist as dne: logger.warning("AMO CRM provider not found") raise CRMException("AMO CRM provider not found") from dne + except CustomerRelationshipManagementProvider.MultipleObjectsReturned as mre: + logger.warning("Multiple AMO CRM providers found") + raise CRMException("Multiple AMO CRM providers found") from mre self.base = f"https://{self.instance.integration_url}" @@ -46,10 +48,6 @@ class AmoCRM: raise CRMException("AMO CRM provider not configured") def _token(self) -> str: - cached = getattr(self, "_cached_token", None) - expiry = getattr(self, "_cached_expiry", 0) - if cached and time.time() < expiry - 15: - return cached payload = { "client_id": self.client_id, "client_secret": self.client_secret, diff --git a/core/signals.py b/core/signals.py index 4cd6a12f..9bf573df 100644 --- a/core/signals.py +++ b/core/signals.py @@ -1,4 +1,5 @@ import logging +from contextlib import suppress from datetime import timedelta from django.db import IntegrityError @@ -11,6 +12,7 @@ from django.utils.translation import gettext_lazy as _ from sentry_sdk import capture_exception from core.crm import any_crm_integrations +from core.crm.exceptions import CRMException from core.models import Category, Order, Product, PromoCode, Wishlist, DigitalAssetDownload from core.utils import ( generate_human_readable_id, @@ -69,7 +71,8 @@ def process_order_changes(instance, created, **_kwargs): instance.attributes = {} if any_crm_integrations(): - instance.trigger_crm() + with suppress(CRMException): + instance.trigger_crm() if not created: if instance.status != "PENDING" and instance.user: