Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Apply changes after review

Maciej Korycinski0713acb0f004
diff --git a/saleor/plugins/sendgrid/plugin.py b/saleor/plugins/sendgrid/plugin.py
index 9131cefd93..0d36151336 100644
--- a/saleor/plugins/sendgrid/plugin.py
+++ b/saleor/plugins/sendgrid/plugin.py
@@ -28,66 +28,64 @@ from .tasks import (
logger = logging.getLogger(__name__)
-def get_event_to_task_map():
- return {
- UserNotifyEvent.ACCOUNT_CONFIRMATION: send_account_confirmation_email_task,
- UserNotifyEvent.ACCOUNT_SET_CUSTOMER_PASSWORD: (
- send_set_user_password_email_task
- ),
- UserNotifyEvent.ACCOUNT_DELETE: send_account_delete_confirmation_email_task,
- UserNotifyEvent.ACCOUNT_CHANGE_EMAIL_CONFIRM: (
- send_user_change_email_notification_task
- ),
- UserNotifyEvent.ACCOUNT_CHANGE_EMAIL_REQUEST: (
- send_request_email_change_email_task
- ),
- UserNotifyEvent.ACCOUNT_PASSWORD_RESET: send_password_reset_email_task,
- UserNotifyEvent.INVOICE_READY: send_invoice_email_task,
- UserNotifyEvent.ORDER_CONFIRMATION: send_order_confirmation_email_task,
- UserNotifyEvent.ORDER_CONFIRMED: send_order_confirmed_email_task,
- UserNotifyEvent.ORDER_FULFILLMENT_CONFIRMATION: (
- send_fulfillment_confirmation_email_task
- ),
- UserNotifyEvent.ORDER_FULFILLMENT_UPDATE: send_fulfillment_update_email_task,
- UserNotifyEvent.ORDER_PAYMENT_CONFIRMATION: (
- send_payment_confirmation_email_task
- ),
- UserNotifyEvent.ORDER_CANCELED: send_order_canceled_email_task,
- UserNotifyEvent.ORDER_REFUND_CONFIRMATION: send_order_refund_email_task,
- }
-
-
-def get_dynamic_template_map():
- return {
- UserNotifyEvent.ACCOUNT_CONFIRMATION: "account_confirmation_template_id",
- UserNotifyEvent.ACCOUNT_SET_CUSTOMER_PASSWORD: (
- "account_set_customer_password_template_id"
- ),
- UserNotifyEvent.ACCOUNT_DELETE: "account_delete_template_id",
- UserNotifyEvent.ACCOUNT_CHANGE_EMAIL_CONFIRM: (
- "account_change_email_confirm_template_id"
- ),
- UserNotifyEvent.ACCOUNT_CHANGE_EMAIL_REQUEST: (
- "account_change_email_request_template_id"
- ),
- UserNotifyEvent.ACCOUNT_PASSWORD_RESET: "account_password_reset_template_id",
- UserNotifyEvent.INVOICE_READY: "invoice_ready_template_id",
- UserNotifyEvent.ORDER_CONFIRMATION: "order_confirmation_template_id",
- UserNotifyEvent.ORDER_CONFIRMED: "order_confirmed_template_id",
- UserNotifyEvent.ORDER_FULFILLMENT_CONFIRMATION: (
- "order_fulfillment_confirmation_template_id"
- ),
- UserNotifyEvent.ORDER_FULFILLMENT_UPDATE: (
- "order_fulfillment_update_template_id"
- ),
- UserNotifyEvent.ORDER_PAYMENT_CONFIRMATION: (
- "order_payment_confirmation_template_id"
- ),
- UserNotifyEvent.ORDER_CANCELED: "order_canceled_template_id",
- UserNotifyEvent.ORDER_REFUND_CONFIRMATION: (
- "order_refund_confirmation_template_id"
- ),
- }
+EVENT_MAP = {
+ UserNotifyEvent.ACCOUNT_CONFIRMATION: (
+ send_account_confirmation_email_task,
+ "account_confirmation_template_id",
+ ),
+ UserNotifyEvent.ACCOUNT_SET_CUSTOMER_PASSWORD: (
+ send_set_user_password_email_task,
+ "account_set_customer_password_template_id",
+ ),
+ UserNotifyEvent.ACCOUNT_DELETE: (
+ send_account_delete_confirmation_email_task,
+ "account_delete_template_id",
+ ),
+ UserNotifyEvent.ACCOUNT_CHANGE_EMAIL_CONFIRM: (
+ send_user_change_email_notification_task,
+ "account_change_email_confirm_template_id",
+ ),
+ UserNotifyEvent.ACCOUNT_CHANGE_EMAIL_REQUEST: (
+ send_request_email_change_email_task,
+ "account_change_email_request_template_id",
+ ),
+ UserNotifyEvent.ACCOUNT_PASSWORD_RESET: (
+ send_password_reset_email_task,
+ "account_password_reset_template_id",
+ ),
+ UserNotifyEvent.INVOICE_READY: (
+ send_invoice_email_task,
+ "invoice_ready_template_id",
+ ),
+ UserNotifyEvent.ORDER_CONFIRMATION: (
+ send_order_confirmation_email_task,
+ "order_confirmation_template_id",
+ ),
+ UserNotifyEvent.ORDER_CONFIRMED: (
+ send_order_confirmed_email_task,
+ "order_confirmed_template_id",
+ ),
+ UserNotifyEvent.ORDER_FULFILLMENT_CONFIRMATION: (
+ send_fulfillment_confirmation_email_task,
+ "order_fulfillment_confirmation_template_id",
+ ),
+ UserNotifyEvent.ORDER_FULFILLMENT_UPDATE: (
+ send_fulfillment_update_email_task,
+ "order_fulfillment_update_template_id",
+ ),
+ UserNotifyEvent.ORDER_PAYMENT_CONFIRMATION: (
+ send_payment_confirmation_email_task,
+ "order_payment_confirmation_template_id",
+ ),
+ UserNotifyEvent.ORDER_CANCELED: (
+ send_order_canceled_email_task,
+ "order_canceled_template_id",
+ ),
+ UserNotifyEvent.ORDER_REFUND_CONFIRMATION: (
+ send_order_refund_email_task,
+ "order_refund_confirmation_template_id",
+ ),
+}
class SendgridEmailPlugin(BasePlugin):
@@ -199,9 +197,7 @@ class SendgridEmailPlugin(BasePlugin):
},
"api_key": {
"type": ConfigurationTypeField.SECRET,
- "help_text": (
- "This allows your application to authenticate to our API and send mail."
- ),
+ "help_text": "Your Sendgrid API key.",
"label": "API key",
},
}
@@ -219,20 +215,19 @@ class SendgridEmailPlugin(BasePlugin):
if event not in UserNotifyEvent.CHOICES:
return previous_value
- event_to_task_map = get_event_to_task_map()
- if event not in event_to_task_map:
+ if event not in EVENT_MAP:
logger.warning(f"Missing handler for event {event}")
return previous_value
configuration = {item["name"]: item["value"] for item in self.configuration}
- event_to_template_fields = get_dynamic_template_map()
- template_id = configuration.get(event_to_template_fields.get(event))
+ event_task, event_template = EVENT_MAP.get(event) # type: ignore
+ template_id = configuration.get(event_template)
if not template_id:
# the empty fields means that we should not send an email for this event.
return previous_value
- event_to_task_map[event].delay(payload, asdict(self.config))
+ event_task.delay(payload, asdict(self.config))
@classmethod
def validate_plugin_configuration(cls, plugin_configuration: "PluginConfiguration"):
diff --git a/saleor/plugins/sendgrid/tasks.py b/saleor/plugins/sendgrid/tasks.py
index d6c5859c86..d489119f8f 100644
--- a/saleor/plugins/sendgrid/tasks.py
+++ b/saleor/plugins/sendgrid/tasks.py
@@ -11,6 +11,9 @@ from . import SendgridConfiguration
logger = logging.getLogger(__name__)
+CELERY_RETRY_BACKOFF = 60
+CELERY_RETRY_MAX = 5
+
def send_email(configuration: SendgridConfiguration, template_id, payload):
recipient_email = payload["recipient_email"]
@@ -24,8 +27,8 @@ def send_email(configuration: SendgridConfiguration, template_id, payload):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_account_confirmation_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -38,8 +41,8 @@ def send_account_confirmation_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_password_reset_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -54,8 +57,8 @@ def send_password_reset_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_request_email_change_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -76,8 +79,8 @@ def send_request_email_change_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_user_change_email_notification_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -99,8 +102,8 @@ def send_user_change_email_notification_task(payload: dict, configuration: dict)
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_account_delete_confirmation_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -113,8 +116,8 @@ def send_account_delete_confirmation_email_task(payload: dict, configuration: di
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_set_user_password_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -127,8 +130,8 @@ def send_set_user_password_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_invoice_email_task(payload: dict, configuration: dict):
"""Send an invoice to user of related order with URL to download it."""
@@ -152,8 +155,8 @@ def send_invoice_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_order_confirmation_email_task(payload: dict, configuration: dict):
"""Send order confirmation email."""
@@ -172,8 +175,8 @@ def send_order_confirmation_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_fulfillment_confirmation_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -198,8 +201,8 @@ def send_fulfillment_confirmation_email_task(payload: dict, configuration: dict)
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_fulfillment_update_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -212,8 +215,8 @@ def send_fulfillment_update_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_payment_confirmation_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -231,8 +234,8 @@ def send_payment_confirmation_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_order_canceled_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -250,8 +253,8 @@ def send_order_canceled_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_order_refund_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)
@@ -269,8 +272,8 @@ def send_order_refund_email_task(payload: dict, configuration: dict):
@app.task(
autoretry_for=(SendGridException,),
- retry_backoff=60,
- retry_kwargs={"max_retries": 5},
+ retry_backoff=CELERY_RETRY_BACKOFF,
+ retry_kwargs={"max_retries": CELERY_RETRY_MAX},
)
def send_order_confirmed_email_task(payload: dict, configuration: dict):
configuration = SendgridConfiguration(**configuration)