fix(signals): course tracking fix
This commit is contained in:
1 file changed
+43
-44
+43
-44
@@ -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(
|
|
||||||
student=instance, course=instance.course
|
|
||||||
).first()
|
|
||||||
|
|
||||||
if existing_tracking:
|
# Check if course tracking already exists (outside of any transaction)
|
||||||
print(f"Course tracking already exists for {instance.name}")
|
existing_tracking = CourseTracking.objects.filter(
|
||||||
return
|
student=instance, course=instance.course
|
||||||
|
).first()
|
||||||
|
|
||||||
# Calculate dates based on course duration
|
if existing_tracking:
|
||||||
start_date = instance.joining_date or date.today()
|
return # Skip if already exists
|
||||||
|
|
||||||
# Calculate expected end date based on course duration type
|
try:
|
||||||
if instance.course.duration_type == "Year":
|
# Calculate dates based on course duration
|
||||||
# For year-based courses, each year is 365 days
|
start_date = instance.joining_date or date.today()
|
||||||
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
|
|
||||||
duration_days = years * 365 # Use full years for total duration
|
|
||||||
|
|
||||||
expected_end_date = start_date + timedelta(days=duration_days)
|
# Calculate expected end date based on course duration type
|
||||||
|
years = instance.course.duration
|
||||||
|
duration_days = years * 365 # Use full years for total duration
|
||||||
|
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
|
||||||
student=instance,
|
if instance.course.duration_type == "Semester":
|
||||||
course=instance.course,
|
period_end_date = start_date + timedelta(days=180) # 6 months
|
||||||
start_date=start_date,
|
else:
|
||||||
expected_end_date=expected_end_date,
|
period_end_date = start_date + timedelta(days=365) # 1 year
|
||||||
current_period=instance.current_period,
|
|
||||||
period_start_date=start_date,
|
|
||||||
period_end_date=start_date
|
|
||||||
+ timedelta(days=180), # 6 months for first semester
|
|
||||||
)
|
|
||||||
|
|
||||||
# The model's save() method will handle the percentage calculation automatically
|
# Create a new CourseTracking instance
|
||||||
# No need to manually call update_completion_percentage()
|
CourseTracking.objects.create(
|
||||||
|
student=instance,
|
||||||
|
course=instance.course,
|
||||||
|
start_date=start_date,
|
||||||
|
expected_end_date=expected_end_date,
|
||||||
|
current_period=instance.current_period,
|
||||||
|
period_start_date=period_start_date,
|
||||||
|
period_end_date=period_end_date,
|
||||||
|
progress_status="In Progress"
|
||||||
|
)
|
||||||
|
|
||||||
except Exception as e:
|
# Log success but don't trigger any more signals
|
||||||
print(
|
logger.info(f"Created course tracking for student: {instance.name}")
|
||||||
f"Error creating course tracking for student {instance.name}: {str(e)}"
|
|
||||||
)
|
except Exception as e:
|
||||||
# Log the error but don't prevent student creation
|
# Log the error properly
|
||||||
# You might want to add proper logging here instead of print statements
|
error_msg = f"Error creating course tracking for student {instance.name}: {str(e)}"
|
||||||
else:
|
logger.error(error_msg)
|
||||||
print(f"No course assigned to student {instance.name}")
|
# Do not raise the exception - let the student save proceed
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Student)
|
@receiver(post_save, sender=Student)
|
||||||
|
|||||||
Reference in new issue
Block a user