-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(core) create a management command to generate certificates
Create a management command which aims to be called by a cron task to generate certificate at a regular interval.
- Loading branch information
Showing
4 changed files
with
201 additions
and
10 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
90 changes: 90 additions & 0 deletions
90
src/backend/joanie/core/management/commands/create_certificates.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,90 @@ | ||
"""Management command to create all available certificates that are not yet existing.""" | ||
import logging | ||
|
||
from django.core.management import BaseCommand | ||
|
||
from joanie.core import enums, models | ||
|
||
logger = logging.getLogger("joanie.core.create_certificates") | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Browse all courses and their products then create certificate if needed.""" | ||
|
||
help = "Browse all courses and their products then create certificate if needed." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"-c", | ||
"--course", | ||
help="Provide a course code to restrict check to this course", | ||
) | ||
parser.add_argument( | ||
"-p", | ||
"--product", | ||
help="Provide a product uuid to restrict check to this product", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Retrieve all courses with certifying products then for each product check state | ||
of order and their enrollments, if all enrollments has been passed with success | ||
we are able to create the related certificate. | ||
""" | ||
# TODO: Improve db queries by using select_related and prefetch_related methods | ||
# 1. First retrieve all courses with certifying products | ||
course = options["course"] | ||
product = options["product"] | ||
filters = {"type__in": enums.PRODUCT_TYPE_CERTIFICATE_ALLOWED} | ||
|
||
if course: | ||
filters.update({"courses__code": course}) | ||
|
||
if product: | ||
filters.update({"uid": product}) | ||
|
||
products = models.Product.objects.filter(**filters) | ||
|
||
for product in products: | ||
|
||
# Retrieve all active orders related to the course | ||
orders = [ | ||
order | ||
for order in product.orders.filter( | ||
is_canceled=False, certificate__isnull=True | ||
) | ||
if order.state == enums.ORDER_STATE_VALIDATED | ||
] | ||
|
||
certificate_generated = 0 | ||
for order in orders: | ||
graded_courses = order.target_courses.filter( | ||
order_relations__is_graded=True | ||
).order_by("order_relations__position") | ||
|
||
course_runs = models.CourseRun.objects.filter( | ||
course__in=graded_courses, is_gradable=True | ||
) | ||
|
||
enrollments = order.enrollments.filter( | ||
course_run__in=course_runs, is_active=True | ||
) | ||
|
||
if len(enrollments) < len(graded_courses): | ||
break | ||
|
||
passed_enrollments = 0 | ||
for enrollment in enrollments: | ||
if enrollment.is_passed: | ||
passed_enrollments += 1 | ||
|
||
if passed_enrollments == len(graded_courses): | ||
order.create_certificate() | ||
certificate_generated += 1 | ||
|
||
logger.info( | ||
f'{certificate_generated} certificate(s) for "{product.title}" has been generated.' | ||
) | ||
self.stdout.write( | ||
f'{certificate_generated} certificate(s) for "{product.title}" has been generated.' | ||
) |
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