Commit f11389ca by Clinton Blackburn Committed by Clinton Blackburn

Added management command to back-populate missing audit seats

LEARNER-2876
parent e5490b2e
import logging
from django.core.management import BaseCommand
from course_discovery.apps.publisher.models import CourseRun, Seat
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Adds audit seats to credit/verified course runs'
def add_arguments(self, parser):
parser.add_argument('--commit',
action='store_true',
dest='commit',
default=False,
help='Commit the new seats to the database')
def handle(self, *args, **options):
course_runs = CourseRun.objects.filter(seats__type__in=(Seat.CREDIT, Seat.VERIFIED,)).exclude(
seats__type=Seat.AUDIT)
if options['commit']:
for course_run in course_runs:
seat = course_run.seats.create(type=Seat.AUDIT, price=0, upgrade_deadline=None)
course_run_id = course_run.lms_course_id or course_run.id
logger.info('Created audit seat [%d] for course run [%s]', seat.id, course_run_id)
else:
logger.info('The following [%d] course runs lack audit seats...', course_runs.count())
for course_run in course_runs:
logger.info('\t%s', course_run.lms_course_id or course_run.id)
import pytest
from django.core.management import call_command
from course_discovery.apps.publisher.models import Seat
from course_discovery.apps.publisher.tests.factories import SeatFactory
@pytest.mark.django_db
class TestAddAuditSeats:
def test_no_commit(self):
seats = SeatFactory.create_batch(3, type=Seat.VERIFIED)
call_command('add_audit_seats_to_verified_course_runs')
assert Seat.objects.count() == len(seats)
@pytest.mark.parametrize('seat_type', (Seat.CREDIT, Seat.VERIFIED,))
def test_commit(self, seat_type):
SeatFactory(type=Seat.AUDIT)
SeatFactory(type=Seat.NO_ID_PROFESSIONAL)
SeatFactory(type=Seat.PROFESSIONAL)
seat = SeatFactory(type=seat_type)
call_command('add_audit_seats_to_verified_course_runs', '--commit')
assert seat.course_run.seats.count() == 2
assert seat.course_run.seats.filter(type=Seat.AUDIT, price=0).exists()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment