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
+50 -51
+50 -51
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
existing_tracking = CourseTracking.objects.filter( # Check if course tracking already exists (outside of any transaction)
student=instance, course=instance.course existing_tracking = CourseTracking.objects.filter(
).first() student=instance, course=instance.course
).first()
if existing_tracking:
return # Skip if already exists
try:
# Calculate dates based on course duration
start_date = instance.joining_date or date.today()
if existing_tracking: # Calculate expected end date based on course duration type
print(f"Course tracking already exists for {instance.name}") years = instance.course.duration
return duration_days = years * 365 # Use full years for total duration
expected_end_date = start_date + timedelta(days=duration_days)
# Calculate dates based on course duration
start_date = instance.joining_date or date.today() # Set period dates based on duration type
period_start_date = start_date
# Calculate expected end date based on course duration type if instance.course.duration_type == "Semester":
if instance.course.duration_type == "Year": period_end_date = start_date + timedelta(days=180) # 6 months
# For year-based courses, each year is 365 days else:
years = instance.course.duration period_end_date = start_date + timedelta(days=365) # 1 year
duration_days = years * 365
else: # Semester based # Create a new CourseTracking instance
# For semester-based courses: CourseTracking.objects.create(
# - Each year has 2 semesters student=instance,
# - Total years is the same as course duration course=instance.course,
years = instance.course.duration start_date=start_date,
duration_days = years * 365 # Use full years for total duration expected_end_date=expected_end_date,
current_period=instance.current_period,
expected_end_date = start_date + timedelta(days=duration_days) period_start_date=period_start_date,
period_end_date=period_end_date,
# Create course tracking without nested transaction progress_status="In Progress"
course_tracking = CourseTracking.objects.create( )
student=instance,
course=instance.course, # Log success but don't trigger any more signals
start_date=start_date, logger.info(f"Created course tracking for student: {instance.name}")
expected_end_date=expected_end_date,
current_period=instance.current_period, except Exception as e:
period_start_date=start_date, # Log the error properly
period_end_date=start_date error_msg = f"Error creating course tracking for student {instance.name}: {str(e)}"
+ timedelta(days=180), # 6 months for first semester logger.error(error_msg)
) # Do not raise the exception - let the student save proceed
# The model's save() method will handle the percentage calculation automatically
# No need to manually call update_completion_percentage()
except Exception as e:
print(
f"Error creating course tracking for student {instance.name}: {str(e)}"
)
# Log the error but don't prevent student creation
# 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)