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 000000000000..f23fb28688bb --- /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 7dfbb84d9d6b..0180d28ad69b 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 08a9645f0c6e..766096f70785 100644 --- a/openedx/features/sdaia_features/course_progress/tasks.py +++ b/openedx/features/sdaia_features/course_progress/tasks.py @@ -60,9 +60,9 @@ def send_user_course_progress_email(current_progress, progress_last_email_sent_a site_domain = configuration_helpers.get_value_for_org( course.org, 'site_domain', - settings.LMS_HOST + settings.LMS_BASE ) - 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) @@ -112,9 +115,9 @@ def send_user_course_completion_email(user_id, course_key): site_domain = configuration_helpers.get_value_for_org( course.org, 'site_domain', - settings.LMS_HOST + settings.LMS_BASE ) - 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