-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating the Certification Page for course based trainings (#2223)
## What This is a small PR making a tiny change in the queryset for the trainings. The logic is updated to also include custom logic for courses as their expiry is determined in slightly different way than the regular one. ## Why A TrainingType can have multiple courses attached to it, each being a unique version in itself. Each course version should have it's own valid duration - and hence it's own expiry date. Whenever a user takes an on-platform course, it is the latest version of the course available at that time, but with time new versions can come in, and at some point, the version of the course that this training was attached to, will be deprecated. At that point all the trainings associated with this course version should be renewed. Thus, admin panel provides an option to set an expiry date to the associated trainings when deprecating a course version. That is how the expiry date for the trainings is decided for course version, which is different from the way it is determined for other types of trainings. Thus, this PR adds onto the logic of querysets so that all the courses are accurately identified and put under active / expired versions. ### This PR does not disturb the regular flow - it only add onto the logic to also fetch the course based trainings (A feature in the upcoming Trainings part-2 PR)
- Loading branch information
Showing
5 changed files
with
97 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,71 @@ | ||
from django.db.models import DateTimeField, ExpressionWrapper, QuerySet, F, Q | ||
from django.db.models import (DateTimeField, ExpressionWrapper, QuerySet, F, Q, | ||
OuterRef, Subquery, Case, When) | ||
from django.utils import timezone | ||
|
||
from user.enums import TrainingStatus | ||
|
||
from user.enums import TrainingStatus, RequiredField | ||
from training.models import Course | ||
|
||
class TrainingQuerySet(QuerySet): | ||
def get_review(self): | ||
return self.filter(status=TrainingStatus.REVIEW) | ||
""" | ||
Get the document-based or URL-based training objects for the user, that are in the status REVIEW. | ||
""" | ||
return self.filter( | ||
Q(status=TrainingStatus.REVIEW), | ||
Q(training_type__required_field=RequiredField.DOCUMENT) | ||
| Q(training_type__required_field=RequiredField.URL) | ||
) | ||
|
||
def get_in_progress(self): | ||
""" | ||
Get the on-platform training objects for the user, that are in the status IN-PROGRESS. | ||
""" | ||
return self.filter( | ||
Q(status=TrainingStatus.IN_PROGRESS), | ||
Q(training_type__required_field=RequiredField.PLATFORM) | ||
) | ||
|
||
def get_valid(self): | ||
""" | ||
Get all the training objects that are in the status ACCEPTED and have not expired. | ||
""" | ||
return self.filter( | ||
Q(status=TrainingStatus.ACCEPTED), | ||
Q(training_type__valid_duration__isnull=True) | ||
| Q(process_datetime__gte=timezone.now() - F('training_type__valid_duration')), | ||
| Q(process_datetime__gte=timezone.now() - Case( | ||
When(training_type__required_field=RequiredField.PLATFORM, then=F('course__valid_duration')), | ||
default=F('training_type__valid_duration') | ||
)), | ||
).annotate( | ||
valid_datetime=ExpressionWrapper( | ||
F('process_datetime') + F('training_type__valid_duration'), output_field=DateTimeField() | ||
F('process_datetime') + Case( | ||
When(training_type__required_field=RequiredField.PLATFORM, then=F('course__valid_duration')), | ||
default=F('training_type__valid_duration') | ||
), output_field=DateTimeField() | ||
) | ||
) | ||
|
||
def get_expired(self): | ||
""" | ||
Get all the training objects that are in the status ACCEPTED and have expired | ||
""" | ||
return self.filter( | ||
status=TrainingStatus.ACCEPTED, process_datetime__lt=timezone.now() - F('training_type__valid_duration') | ||
Q(status=TrainingStatus.ACCEPTED), | ||
Q(process_datetime__lt=timezone.now() - Case( | ||
When(training_type__required_field=RequiredField.PLATFORM, then=F('course__valid_duration')), | ||
default=F('training_type__valid_duration') | ||
)), | ||
).annotate( | ||
valid_datetime=ExpressionWrapper( | ||
F('process_datetime') + F('training_type__valid_duration'), output_field=DateTimeField() | ||
F('process_datetime') + Case( | ||
When(training_type__required_field=RequiredField.PLATFORM, then=F('course__valid_duration')), | ||
default=F('training_type__valid_duration') | ||
), output_field=DateTimeField() | ||
) | ||
) | ||
|
||
def get_rejected(self): | ||
""" | ||
Get all the training objects that are in the status REJECTED. | ||
""" | ||
return self.filter(status=TrainingStatus.REJECTED) |
28 changes: 28 additions & 0 deletions
28
physionet-django/user/migrations/0063_alter_training_status.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 4.2.14 on 2024-11-15 00:47 | ||
|
||
from django.db import migrations, models | ||
import user.enums | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("user", "0062_training_course"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="training", | ||
name="status", | ||
field=models.PositiveSmallIntegerField( | ||
choices=[ | ||
(0, "REVIEW"), | ||
(1, "WITHDRAWN"), | ||
(2, "REJECTED"), | ||
(3, "ACCEPTED"), | ||
(4, "IN_PROGRESS"), | ||
], | ||
default=user.enums.TrainingStatus["REVIEW"], | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters