Skip to content

Commit

Permalink
fix: send course completion email only one time
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Faraz Maqsood authored and Muhammad Faraz Maqsood committed Mar 5, 2024
1 parent c24a13d commit c4203db
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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),
),
]
1 change: 1 addition & 0 deletions openedx/features/sdaia_features/course_progress/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 7 additions & 2 deletions openedx/features/sdaia_features/course_progress/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit c4203db

Please sign in to comment.