Skip to content

Commit

Permalink
feat: added events to bulk email tool (#35147)
Browse files Browse the repository at this point in the history
* feat: added events to bulk email tool
  • Loading branch information
AhtishamShahid authored Aug 8, 2024
1 parent d1bf43d commit 63f327b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lms/djangoapps/bulk_email/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ class BulkEmailConfig(AppConfig):
Application Configuration for bulk_email.
"""
name = 'lms.djangoapps.bulk_email'

def ready(self):
import lms.djangoapps.bulk_email.signals # lint-amnesty, pylint: disable=unused-import
from edx_ace.signals import ACE_MESSAGE_SENT # lint-amnesty, pylint: disable=unused-import
30 changes: 28 additions & 2 deletions lms/djangoapps/bulk_email/signals.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""
Signal handlers for the bulk_email app
"""


from django.contrib.auth import get_user_model
from django.dispatch import receiver
from eventtracking import tracker

from common.djangoapps.student.models import CourseEnrollment
from openedx.core.djangoapps.user_api.accounts.signals import USER_RETIRE_MAILINGS
from edx_ace.signals import ACE_MESSAGE_SENT

from .models import Optout

Expand All @@ -24,3 +25,28 @@ def force_optout_all(sender, **kwargs): # lint-amnesty, pylint: disable=unused-

for enrollment in CourseEnrollment.objects.filter(user=user):
Optout.objects.get_or_create(user=user, course_id=enrollment.course.id)


@receiver(ACE_MESSAGE_SENT)
def ace_email_sent_handler(sender, **kwargs):
"""
When an email is sent using ACE, this method will create an event to detect ace email success status
"""
# Fetch the message object from kwargs, defaulting to None if not present
message = kwargs.get('message', None)

user_model = get_user_model()
try:
user_id = user_model.objects.get(email=message.recipient.email_address).id
except user_model.DoesNotExist:
user_id = None
course_email = message.context.get('course_email', None)
course_id = course_email.course_id if course_email else None
tracker.emit(
'edx.bulk_email.sent',
{
'message_type': message.name,
'course_id': course_id,
'user_id': user_id,
}
)
10 changes: 9 additions & 1 deletion lms/djangoapps/bulk_email/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from django.utils.translation import gettext as _
from django.utils.translation import override as override_language
from edx_django_utils.monitoring import set_code_owner_attribute
from eventtracking import tracker
from markupsafe import escape

from common.djangoapps.util.date_utils import get_default_time_display
Expand Down Expand Up @@ -467,7 +468,14 @@ def _send_course_email(entry_id, email_id, to_list, global_email_context, subtas
"send."
)
raise exc

tracker.emit(
'edx.bulk_email.created',
{
'course_id': str(course_email.course_id),
'to_list': to_list,
'total_recipients': total_recipients,
}
)
# Exclude optouts (if not a retry):
# Note that we don't have to do the optout logic at all if this is a retry,
# because we have presumably already performed the optout logic on the first
Expand Down
9 changes: 9 additions & 0 deletions lms/djangoapps/bulk_email/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.http import Http404
from eventtracking import tracker
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey

Expand Down Expand Up @@ -60,4 +61,12 @@ def opt_out_email_updates(request, token, course_id):
course_id,
)

tracker.emit(
'edx.bulk_email.opt_out',
{
'course_id': course_id,
'user_id': user.id,
}
)

return render_to_response('bulk_email/unsubscribe_success.html', context)
2 changes: 1 addition & 1 deletion requirements/edx/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ drf-yasg==1.21.7
# via
# django-user-tasks
# edx-api-doc-tools
edx-ace==1.11.0
edx-ace==1.11.1
# via -r requirements/edx/kernel.in
edx-api-doc-tools==1.8.0
# via
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ drf-yasg==1.21.7
# -r requirements/edx/testing.txt
# django-user-tasks
# edx-api-doc-tools
edx-ace==1.11.0
edx-ace==1.11.1
# via
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ drf-yasg==1.21.7
# -r requirements/edx/base.txt
# django-user-tasks
# edx-api-doc-tools
edx-ace==1.11.0
edx-ace==1.11.1
# via -r requirements/edx/base.txt
edx-api-doc-tools==1.8.0
# via
Expand Down
2 changes: 1 addition & 1 deletion requirements/edx/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ drf-yasg==1.21.7
# -r requirements/edx/base.txt
# django-user-tasks
# edx-api-doc-tools
edx-ace==1.11.0
edx-ace==1.11.1
# via -r requirements/edx/base.txt
edx-api-doc-tools==1.8.0
# via
Expand Down

0 comments on commit 63f327b

Please sign in to comment.