|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import TYPE_CHECKING, Type |
| 7 | + |
| 8 | +from django.conf import settings |
| 9 | +from django.contrib.auth import get_user_model |
| 10 | +from django.core.mail import send_mail |
| 11 | +from django.template.loader import render_to_string |
| 12 | + |
| 13 | +from wagtail.signals import page_published, page_unpublished, post_page_move |
| 14 | +from wagtail_localize_smartling.signals import individual_translation_imported |
| 15 | + |
| 16 | +from bedrock.cms.utils import warm_page_path_cache |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from wagtail_localize.models import Translation |
| 20 | + from wagtail_localize_smartling.models import Job |
| 21 | + |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +def notify_of_imported_translation( |
| 27 | + sender: Type["Job"], |
| 28 | + instance: "Job", |
| 29 | + translation: "Translation", |
| 30 | + **kwargs, |
| 31 | +): |
| 32 | + """ |
| 33 | + Signal handler for receiving news that a translation has landed from |
| 34 | + Smartling. |
| 35 | +
|
| 36 | + For now, sends a notification email to all Admins |
| 37 | + """ |
| 38 | + UserModel = get_user_model() |
| 39 | + |
| 40 | + admin_emails = UserModel.objects.filter( |
| 41 | + is_superuser=True, |
| 42 | + is_active=True, |
| 43 | + ).values_list("email", flat=True) |
| 44 | + admin_emails = [email for email in admin_emails if email] # Safety check to ensure no empty email addresses are included |
| 45 | + |
| 46 | + if not admin_emails: |
| 47 | + logger.warning("Unable to send translation-imported email alerts: no admins in system") |
| 48 | + return |
| 49 | + |
| 50 | + email_subject = "New translations imported into Bedrock CMS" |
| 51 | + |
| 52 | + job_name = instance.name |
| 53 | + translation_source_name = str(instance.translation_source.get_source_instance()) |
| 54 | + |
| 55 | + smartling_cms_dashboard_url = f"{settings.WAGTAILADMIN_BASE_URL}/cms-admin/smartling-jobs/inspect/{instance.pk}/" |
| 56 | + |
| 57 | + email_body = render_to_string( |
| 58 | + template_name="cms/email/notifications/individual_translation_imported__body.txt", |
| 59 | + context={ |
| 60 | + "job_name": job_name, |
| 61 | + "translation_source_name": translation_source_name, |
| 62 | + "translation_target_language_code": translation.target_locale.language_code, |
| 63 | + "smartling_cms_dashboard_url": smartling_cms_dashboard_url, |
| 64 | + }, |
| 65 | + ) |
| 66 | + |
| 67 | + send_mail( |
| 68 | + subject=email_subject, |
| 69 | + message=email_body, |
| 70 | + from_email=settings.DEFAULT_FROM_EMAIL, |
| 71 | + recipient_list=admin_emails, |
| 72 | + ) |
| 73 | + logger.info(f"Translation-imported notification sent to {len(admin_emails)} admins") |
| 74 | + |
| 75 | + |
| 76 | +individual_translation_imported.connect(notify_of_imported_translation, weak=False) |
| 77 | + |
| 78 | + |
| 79 | +def trigger_cache_warming(sender, **kwargs): |
| 80 | + # Run after the post-migrate signal is sent for the `cms` app |
| 81 | + warm_page_path_cache() |
| 82 | + |
| 83 | + |
| 84 | +def rebuild_path_cache_after_page_move(sender, **kwargs): |
| 85 | + # Check if a page has moved up or down within the tree |
| 86 | + # (rather than just being reordered). If it has really moved |
| 87 | + # then we should update the cache |
| 88 | + if kwargs["url_path_before"] == kwargs["url_path_after"]: |
| 89 | + # No URLs are changing - nothing to do here! |
| 90 | + return |
| 91 | + |
| 92 | + # The page is moving, so we need to rebuild the entire pre-empting-lookup cache |
| 93 | + warm_page_path_cache() |
| 94 | + |
| 95 | + |
| 96 | +post_page_move.connect(rebuild_path_cache_after_page_move) |
| 97 | + |
| 98 | +page_published.connect(trigger_cache_warming) |
| 99 | +page_unpublished.connect(trigger_cache_warming) |
0 commit comments