fix(signals): course tracking fix

This commit is contained in:
sankalp committed 2025-04-22 04:57:13 +05:45
1 parent 7715c6b72a
commit 9be8764716
1 file changed
+27 -28
+27 -28
View File
@@ -103,59 +103,58 @@ def create_course_tracking(sender, instance, created, **kwargs):
Signal handler to create CourseTracking when a new student is created Signal handler to create CourseTracking when a new student is created
or when a course is assigned to an existing student or when a course is assigned to an existing student
""" """
# Skip if signal is triggered due to our own update
if getattr(instance, '_skip_course_tracking_signal', False):
return
# Check if student has a course assigned # Check if student has a course assigned
if instance.course: if not instance.course:
try: return
# Check if course tracking already exists
# Check if course tracking already exists (outside of any transaction)
existing_tracking = CourseTracking.objects.filter( existing_tracking = CourseTracking.objects.filter(
student=instance, course=instance.course student=instance, course=instance.course
).first() ).first()
if existing_tracking: if existing_tracking:
print(f"Course tracking already exists for {instance.name}") return # Skip if already exists
return
try:
# Calculate dates based on course duration # Calculate dates based on course duration
start_date = instance.joining_date or date.today() start_date = instance.joining_date or date.today()
# Calculate expected end date based on course duration type # Calculate expected end date based on course duration type
if instance.course.duration_type == "Year":
# For year-based courses, each year is 365 days
years = instance.course.duration
duration_days = years * 365
else: # Semester based
# For semester-based courses:
# - Each year has 2 semesters
# - Total years is the same as course duration
years = instance.course.duration years = instance.course.duration
duration_days = years * 365 # Use full years for total duration duration_days = years * 365 # Use full years for total duration
expected_end_date = start_date + timedelta(days=duration_days) expected_end_date = start_date + timedelta(days=duration_days)
# Create course tracking without nested transaction # Set period dates based on duration type
course_tracking = CourseTracking.objects.create( period_start_date = start_date
if instance.course.duration_type == "Semester":
period_end_date = start_date + timedelta(days=180) # 6 months
else:
period_end_date = start_date + timedelta(days=365) # 1 year
# Create a new CourseTracking instance
CourseTracking.objects.create(
student=instance, student=instance,
course=instance.course, course=instance.course,
start_date=start_date, start_date=start_date,
expected_end_date=expected_end_date, expected_end_date=expected_end_date,
current_period=instance.current_period, current_period=instance.current_period,
period_start_date=start_date, period_start_date=period_start_date,
period_end_date=start_date period_end_date=period_end_date,
+ timedelta(days=180), # 6 months for first semester progress_status="In Progress"
) )
# The model's save() method will handle the percentage calculation automatically # Log success but don't trigger any more signals
# No need to manually call update_completion_percentage() logger.info(f"Created course tracking for student: {instance.name}")
except Exception as e: except Exception as e:
print( # Log the error properly
f"Error creating course tracking for student {instance.name}: {str(e)}" error_msg = f"Error creating course tracking for student {instance.name}: {str(e)}"
) logger.error(error_msg)
# Log the error but don't prevent student creation # Do not raise the exception - let the student save proceed
# You might want to add proper logging here instead of print statements
else:
print(f"No course assigned to student {instance.name}")
@receiver(post_save, sender=Student) @receiver(post_save, sender=Student)