diff --git a/learning_paths/admin.py b/learning_paths/admin.py index 590d235..5f81b03 100644 --- a/learning_paths/admin.py +++ b/learning_paths/admin.py @@ -122,20 +122,26 @@ class LearningPathAdmin(admin.ModelAdmin): def save_related(self, request, form, formsets, change): """Save related objects and enroll users in the learning path.""" super().save_related(request, form, formsets, change) - enroll_all_in_courses = form.cleaned_data.get('enroll_all_in_courses', False) + enroll_all_in_courses = form.cleaned_data.get("enroll_all_in_courses", False) with transaction.atomic(): for user in form.cleaned_data["usernames"]: - learning_path_enrollment, created = LearningPathEnrollment.objects.get_or_create( - user=user, learning_path=form.instance + learning_path_enrollment, created = ( + LearningPathEnrollment.objects.get_or_create( + user=user, learning_path=form.instance + ) ) if enroll_all_in_courses: - courses = LearningPathStep.objects.filter(learning_path=form.instance).values_list('course_key', flat=True) + courses = LearningPathStep.objects.filter( + learning_path=form.instance + ).values_list("course_key", flat=True) for course_key in courses: LearningPathCourseEnrollment.objects.create( course_key=course_key, learning_path_enrollment=learning_path_enrollment, - course_enrollment=CourseEnrollment.objects.get_or_create(user=user, course_id=course_key)[0], - status=LearningPathCourseEnrollment.ACTIVE + course_enrollment=CourseEnrollment.objects.get_or_create( + user=user, course_id=course_key + )[0], + status=LearningPathCourseEnrollment.ACTIVE, ) @@ -158,11 +164,14 @@ class EnrolledUsersAdmin(admin.ModelAdmin): "learning_path__display_name", ] + class LearningPathCourseEnrollmentAdmin(admin.ModelAdmin): - """ Admin for Learning Path Course enrollments.""" + """Admin for Learning Path Course enrollments.""" + model = LearningPathCourseEnrollment - list_display = ['learning_path_enrollment', 'course_key', 'status'] - list_filter = ['status'] + list_display = ["learning_path_enrollment", "course_key", "status"] + list_filter = ["status"] + admin.site.register(LearningPath, LearningPathAdmin) admin.site.register(Skill, SkillAdmin) diff --git a/learning_paths/compat.py b/learning_paths/compat.py index 609c833..9e28e2c 100644 --- a/learning_paths/compat.py +++ b/learning_paths/compat.py @@ -15,7 +15,4 @@ except ImportError: CourseEnrollment = None -__all__ = [ - "get_course_keys_with_outlines", - "CourseEnrollment" -] +__all__ = ["get_course_keys_with_outlines", "CourseEnrollment"] diff --git a/learning_paths/models.py b/learning_paths/models.py index 21354b6..9e0e212 100644 --- a/learning_paths/models.py +++ b/learning_paths/models.py @@ -11,7 +11,6 @@ from model_utils.models import TimeStampedModel from opaque_keys.edx.django.models import CourseKeyField from .compat import CourseEnrollment -# from common.djangoapps.student.models import CourseEnrollment User = auth.get_user_model() @@ -193,18 +192,19 @@ class LearningPathCourseEnrollment(TimeStampedModel): """ Model to track course enrollments that are part of a learning path """ - ACTIVE = 'active' - INACTIVE = 'inactive' - STATUS_CHOICES = ( - (ACTIVE, 'Active'), - (INACTIVE, 'Inactive') - ) + + ACTIVE = "active" + INACTIVE = "inactive" + STATUS_CHOICES = ((ACTIVE, "Active"), (INACTIVE, "Inactive")) + class Meta: """Model options.""" - unique_together = ('learning_path_enrollment', 'course_key') + unique_together = ("learning_path_enrollment", "course_key") - learning_path_enrollment = models.ForeignKey(LearningPathEnrollment, on_delete=models.CASCADE) + learning_path_enrollment = models.ForeignKey( + LearningPathEnrollment, on_delete=models.CASCADE + ) course_key = CourseKeyField(max_length=255) status = models.CharField(max_length=9, choices=STATUS_CHOICES) course_enrollment = models.ForeignKey(CourseEnrollment, on_delete=models.CASCADE) diff --git a/learning_paths/signals.py b/learning_paths/signals.py index 8e13fd1..4deff3f 100644 --- a/learning_paths/signals.py +++ b/learning_paths/signals.py @@ -1,12 +1,16 @@ - import logging from django.db.models.signals import post_save from django.dispatch import receiver from .compat import CourseEnrollment -from .models import LearningPathCourseEnrollment, LearningPathStep, LearningPathEnrollment +from .models import ( + LearningPathCourseEnrollment, + LearningPathStep, + LearningPathEnrollment, +) logger = logging.getLogger(__name__) + @receiver(post_save, sender=CourseEnrollment) def create_learningpath_course_enrollment(sender, instance, created, **kwargs): if created: # Only react to new enrollments @@ -16,17 +20,16 @@ def create_learningpath_course_enrollment(sender, instance, created, **kwargs): try: learning_path_step = LearningPathStep.objects.get(course_key=course_key) except LearningPathStep.DoesNotExist: - logger.info('Course is not part of any Learning Path. Ignoring.') + logger.info("Course is not part of any Learning Path. Ignoring.") return # Find the associated LearningPathEnrollment try: learning_path_enrollment = LearningPathEnrollment.objects.get( - learning_path=learning_path_step.learning_path, - user=instance.user + learning_path=learning_path_step.learning_path, user=instance.user ) except LearningPathEnrollment.DoesNotExist: - logger.warning('User is not enrolled in the relevant Learning Path.') + logger.warning("User is not enrolled in the relevant Learning Path.") return # Create the LearningPathCourseEnrollment object @@ -34,5 +37,5 @@ def create_learningpath_course_enrollment(sender, instance, created, **kwargs): learning_path_enrollment=learning_path_enrollment, course_key=course_key, status=LearningPathCourseEnrollment.ACTIVE, - course_enrollment=instance + course_enrollment=instance, )