|
| 1 | +""" |
| 2 | +Email notifications for discussion moderation actions. |
| 3 | +""" |
| 4 | +import logging |
| 5 | + |
| 6 | +from django.conf import settings |
| 7 | +from django.contrib.auth import get_user_model |
| 8 | + |
| 9 | +log = logging.getLogger(__name__) |
| 10 | +User = get_user_model() |
| 11 | + |
| 12 | +# Try to import ACE at module level for easier testing |
| 13 | +try: |
| 14 | + from edx_ace import ace |
| 15 | + from edx_ace.recipient import Recipient |
| 16 | + from edx_ace.message import Message |
| 17 | + ACE_AVAILABLE = True |
| 18 | +except ImportError: |
| 19 | + ace = None |
| 20 | + Recipient = None |
| 21 | + Message = None |
| 22 | + ACE_AVAILABLE = False |
| 23 | + |
| 24 | + |
| 25 | +def send_ban_escalation_email( |
| 26 | + banned_user_id, |
| 27 | + moderator_id, |
| 28 | + course_id, |
| 29 | + scope, |
| 30 | + reason, |
| 31 | + threads_deleted, |
| 32 | + comments_deleted |
| 33 | +): |
| 34 | + """ |
| 35 | + Send email to partner-support when user is banned. |
| 36 | +
|
| 37 | + Uses ACE (Automated Communications Engine) for templated emails if available, |
| 38 | + otherwise falls back to Django's email system. |
| 39 | +
|
| 40 | + Args: |
| 41 | + banned_user_id: ID of the banned user |
| 42 | + moderator_id: ID of the moderator who applied the ban |
| 43 | + course_id: Course ID where ban was applied |
| 44 | + scope: 'course' or 'organization' |
| 45 | + reason: Reason for the ban |
| 46 | + threads_deleted: Number of threads deleted |
| 47 | + comments_deleted: Number of comments deleted |
| 48 | + """ |
| 49 | + # Check if email notifications are enabled |
| 50 | + if not getattr(settings, 'DISCUSSION_MODERATION_BAN_EMAIL_ENABLED', True): |
| 51 | + log.info( |
| 52 | + "Ban email notifications disabled by settings. " |
| 53 | + "User %s banned in course %s (scope: %s)", |
| 54 | + banned_user_id, course_id, scope |
| 55 | + ) |
| 56 | + return |
| 57 | + |
| 58 | + try: |
| 59 | + banned_user = User.objects.get(id=banned_user_id) |
| 60 | + moderator = User.objects.get(id=moderator_id) |
| 61 | + |
| 62 | + # Get escalation email from settings |
| 63 | + escalation_email = getattr( |
| 64 | + settings, |
| 65 | + 'DISCUSSION_MODERATION_ESCALATION_EMAIL', |
| 66 | + 'partner-support@edx.org' |
| 67 | + ) |
| 68 | + |
| 69 | + # Try using ACE first (preferred method for edX) |
| 70 | + if ACE_AVAILABLE and ace is not None: |
| 71 | + message = Message( |
| 72 | + app_label='discussion', |
| 73 | + name='ban_escalation', |
| 74 | + recipient=Recipient(lms_user_id=None, email_address=escalation_email), |
| 75 | + context={ |
| 76 | + 'banned_username': banned_user.username, |
| 77 | + 'banned_email': banned_user.email, |
| 78 | + 'banned_user_id': banned_user_id, |
| 79 | + 'moderator_username': moderator.username, |
| 80 | + 'moderator_email': moderator.email, |
| 81 | + 'moderator_id': moderator_id, |
| 82 | + 'course_id': str(course_id), |
| 83 | + 'scope': scope, |
| 84 | + 'reason': reason or 'No reason provided', |
| 85 | + 'threads_deleted': threads_deleted, |
| 86 | + 'comments_deleted': comments_deleted, |
| 87 | + 'total_deleted': threads_deleted + comments_deleted, |
| 88 | + } |
| 89 | + ) |
| 90 | + |
| 91 | + ace.send(message) |
| 92 | + log.info( |
| 93 | + "Ban escalation email sent via ACE to %s for user %s in course %s", |
| 94 | + escalation_email, banned_user.username, course_id |
| 95 | + ) |
| 96 | + |
| 97 | + else: |
| 98 | + # Fallback to Django's email system if ACE is not available |
| 99 | + from django.core.mail import send_mail |
| 100 | + from django.template.loader import render_to_string |
| 101 | + from django.template import TemplateDoesNotExist |
| 102 | + |
| 103 | + context = { |
| 104 | + 'banned_username': banned_user.username, |
| 105 | + 'banned_email': banned_user.email, |
| 106 | + 'banned_user_id': banned_user_id, |
| 107 | + 'moderator_username': moderator.username, |
| 108 | + 'moderator_email': moderator.email, |
| 109 | + 'moderator_id': moderator_id, |
| 110 | + 'course_id': str(course_id), |
| 111 | + 'scope': scope, |
| 112 | + 'reason': reason or 'No reason provided', |
| 113 | + 'threads_deleted': threads_deleted, |
| 114 | + 'comments_deleted': comments_deleted, |
| 115 | + 'total_deleted': threads_deleted + comments_deleted, |
| 116 | + } |
| 117 | + |
| 118 | + # Try to render template, fall back to plain text if template doesn't exist |
| 119 | + try: |
| 120 | + email_body = render_to_string( |
| 121 | + 'discussion/ban_escalation_email.txt', |
| 122 | + context |
| 123 | + ) |
| 124 | + except TemplateDoesNotExist: |
| 125 | + # Plain text fallback |
| 126 | + banned_user_info = "{} ({})".format(banned_user.username, banned_user.email) |
| 127 | + moderator_info = "{} ({})".format(moderator.username, moderator.email) |
| 128 | + email_body = """ |
| 129 | +A user has been banned from discussions: |
| 130 | +
|
| 131 | +Banned User: {} |
| 132 | +Moderator: {} |
| 133 | +Course: {} |
| 134 | +Scope: {} |
| 135 | +Reason: {} |
| 136 | +Content Deleted: {} threads, {} comments |
| 137 | +
|
| 138 | +Please review this moderation action and follow up as needed. |
| 139 | +""".format( |
| 140 | + banned_user_info, |
| 141 | + moderator_info, |
| 142 | + course_id, |
| 143 | + scope, |
| 144 | + reason or 'No reason provided', |
| 145 | + threads_deleted, |
| 146 | + comments_deleted |
| 147 | + ) |
| 148 | + |
| 149 | + subject = f'Discussion Ban Alert: {banned_user.username} in {course_id}' |
| 150 | + from_email = getattr(settings, 'DEFAULT_FROM_EMAIL', 'no-reply@example.com') |
| 151 | + |
| 152 | + send_mail( |
| 153 | + subject=subject, |
| 154 | + message=email_body, |
| 155 | + from_email=from_email, |
| 156 | + recipient_list=[escalation_email], |
| 157 | + fail_silently=False, |
| 158 | + ) |
| 159 | + |
| 160 | + log.info( |
| 161 | + "Ban escalation email sent via Django mail to %s for user %s in course %s", |
| 162 | + escalation_email, banned_user.username, course_id |
| 163 | + ) |
| 164 | + |
| 165 | + except User.DoesNotExist as e: |
| 166 | + log.error("Failed to send ban escalation email: User not found - %s", str(e)) |
| 167 | + raise |
| 168 | + except Exception as exc: |
| 169 | + log.error("Failed to send ban escalation email: %s", str(exc), exc_info=True) |
| 170 | + raise |
0 commit comments