Skip to content

Commit

Permalink
fix: lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulkark committed Mar 4, 2024
1 parent c76bb90 commit 2725429
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
27 changes: 18 additions & 9 deletions learning_paths/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions learning_paths/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@
except ImportError:
CourseEnrollment = None

__all__ = [
"get_course_keys_with_outlines",
"CourseEnrollment"
]
__all__ = ["get_course_keys_with_outlines", "CourseEnrollment"]
18 changes: 9 additions & 9 deletions learning_paths/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
17 changes: 10 additions & 7 deletions learning_paths/signals.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,23 +20,22 @@ 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
LearningPathCourseEnrollment.objects.create(
learning_path_enrollment=learning_path_enrollment,
course_key=course_key,
status=LearningPathCourseEnrollment.ACTIVE,
course_enrollment=instance
course_enrollment=instance,
)

0 comments on commit 2725429

Please sign in to comment.