From c4203dba28bf82cfe32783dfda1acdc84aa6d629 Mon Sep 17 00:00:00 2001 From: Muhammad Faraz Maqsood Date: Wed, 6 Mar 2024 01:39:44 +0500 Subject: [PATCH] fix: send course completion email only one time --- ...ionemailhistory_is_completion_email_sent.py | 18 ++++++++++++++++++ .../sdaia_features/course_progress/models.py | 1 + .../sdaia_features/course_progress/tasks.py | 9 +++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 openedx/features/sdaia_features/course_progress/migrations/0002_coursecompletionemailhistory_is_completion_email_sent.py diff --git a/openedx/features/sdaia_features/course_progress/migrations/0002_coursecompletionemailhistory_is_completion_email_sent.py b/openedx/features/sdaia_features/course_progress/migrations/0002_coursecompletionemailhistory_is_completion_email_sent.py new file mode 100644 index 00000000000..f23fb28688b --- /dev/null +++ b/openedx/features/sdaia_features/course_progress/migrations/0002_coursecompletionemailhistory_is_completion_email_sent.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.20 on 2024-03-05 12:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('course_progress', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='coursecompletionemailhistory', + name='is_completion_email_sent', + field=models.BooleanField(default=False), + ), + ] diff --git a/openedx/features/sdaia_features/course_progress/models.py b/openedx/features/sdaia_features/course_progress/models.py index 7dfbb84d9d6..0180d28ad69 100644 --- a/openedx/features/sdaia_features/course_progress/models.py +++ b/openedx/features/sdaia_features/course_progress/models.py @@ -14,3 +14,4 @@ class CourseCompletionEmailHistory(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) course_key = CourseKeyField(max_length=255, db_index=True) last_progress_email_sent = models.IntegerField(default=0) + is_completion_email_sent = models.BooleanField(default=False) diff --git a/openedx/features/sdaia_features/course_progress/tasks.py b/openedx/features/sdaia_features/course_progress/tasks.py index 08a9645f0c6..08b48aa8c3c 100644 --- a/openedx/features/sdaia_features/course_progress/tasks.py +++ b/openedx/features/sdaia_features/course_progress/tasks.py @@ -62,7 +62,7 @@ def send_user_course_progress_email(current_progress, progress_last_email_sent_a 'site_domain', settings.LMS_HOST ) - site = Site.objects.get(domain=site_domain) + site = Site.objects.filter(domain=site_domain).first() or Site.objects.first() or Site.objects.get_current() message_context = get_base_template_context(site) course_home_url = get_learning_mfe_home_url(course_key=course_key, url_fragment='home') platform_name = configuration_helpers.get_value_for_org( @@ -104,6 +104,9 @@ def send_user_course_progress_email(current_progress, progress_last_email_sent_a def send_user_course_completion_email(user_id, course_key): course_id = CourseKey.from_string(course_key) user = User.objects.get(id=user_id) + user_completion_progress_email_history, _ = CourseCompletionEmailHistory.objects.get_or_create(user=user, course_key=course_key) + if user_completion_progress_email_history.is_completion_email_sent: + return collected_block_structure = get_block_structure_manager(course_id).get_collected() course_grade = CourseGradeFactory().read(user, collected_block_structure=collected_block_structure) passing_grade = int(course_grade.percent * 100) @@ -114,7 +117,7 @@ def send_user_course_completion_email(user_id, course_key): 'site_domain', settings.LMS_HOST ) - site = Site.objects.get(domain=site_domain) + site = Site.objects.filter(domain=site_domain).first() or Site.objects.first() or Site.objects.get_current() message_context = get_base_template_context(site) course_progress_url = get_learning_mfe_home_url(course_key=course_key, url_fragment='progress') platform_name = configuration_helpers.get_value_for_org( @@ -140,6 +143,8 @@ def send_user_course_completion_email(user_id, course_key): user_context={'full_name': user.profile.name} ) ace.send(msg) + user_completion_progress_email_history.is_completion_email_sent = True + user_completion_progress_email_history.save() logger.info('course completion email sent to user:') return True except Exception as e: # pylint: disable=broad-except