Files
edx-platform/lms/djangoapps/program_enrollments/tasks.py
Kyle McCormick 358f989131 Create Python API for program_enrollments: Part III
This is the third in a series of commits to create
a Python API for the LMS program_enrollments app.
It does the following:
* Creates api/ folder.
* Moves link_program_enrollments.py to api/linking.py
* Creates api/reading.py for enrollment-fetching
  functions.
* Updates rest of app to use api/reading.py when
  it was going directly through the models before.
* Other misc. cleanup (isorting, unicode_literals,
  line breaks, etc).

Still to do:
* Create api/writing.py and update app to use it instead
  of going directly through models.
* Create api/reset.py and api/expire.py, which the management
  commands call out to.

EDUCATOR-4321
2019-09-12 14:54:03 -04:00

64 lines
2.4 KiB
Python

""" Tasks for program enrollments """
from __future__ import absolute_import, unicode_literals
import logging
from datetime import timedelta
from celery import task
from celery_utils.logged_task import LoggedTask
from django.utils import timezone
from lms.djangoapps.program_enrollments.models import ProgramCourseEnrollment, ProgramEnrollment
log = logging.getLogger(__name__)
@task(base=LoggedTask)
def expire_waiting_enrollments(expiration_days):
"""
Remove all ProgramEnrollments and related ProgramCourseEnrollments for
ProgramEnrollments not modified for <expiration_days>
"""
expiry_date = timezone.now() - timedelta(days=expiration_days)
program_enrollments = ProgramEnrollment.objects.filter(
user=None,
modified__lte=expiry_date
).prefetch_related('program_course_enrollments')
program_enrollment_ids = []
program_course_enrollment_ids = []
for program_enrollment in program_enrollments:
program_enrollment_ids.append(program_enrollment.id)
log.info(
'Found expired program_enrollment (id=%s) for program_uuid=%s',
program_enrollment.id,
program_enrollment.program_uuid,
)
for course_enrollment in program_enrollment.program_course_enrollments.all():
program_course_enrollment_ids.append(course_enrollment.id)
log.info(
'Found expired program_course_enrollment (id=%s) for program_uuid=%s, course_key=%s',
course_enrollment.id,
program_enrollment.program_uuid,
course_enrollment.course_key,
)
deleted_enrollments = program_enrollments.delete()
log.info('Removed %s expired records: %s', deleted_enrollments[0], deleted_enrollments[1])
deleted_hist_program_enroll = ProgramEnrollment.historical_records.filter( # pylint: disable=no-member
id__in=program_enrollment_ids
).delete()
deleted_hist_course_enroll = ProgramCourseEnrollment.historical_records.filter( # pylint: disable=no-member
id__in=program_course_enrollment_ids
).delete()
log.info(
'Removed %s historical program_enrollment records with id in %s',
deleted_hist_program_enroll[0], program_enrollment_ids
)
log.info(
'Removed %s historical program_course_enrollment records with id in %s',
deleted_hist_course_enroll[0], program_course_enrollment_ids
)