You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.4 KiB
42 lines
1.4 KiB
from django.core.management.base import BaseCommand
|
|
|
|
from apps.course.models.course import Course
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Recalculate lessons_count for all courses based on active assigned course lessons."
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
'--dry-run',
|
|
action='store_true',
|
|
help='Preview updates without saving changes.',
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
dry_run = options['dry_run']
|
|
updated_courses = 0
|
|
|
|
for course in Course.objects.all().order_by('id'):
|
|
actual_count = course.lessons.filter(is_active=True).count()
|
|
|
|
if course.lessons_count != actual_count:
|
|
updated_courses += 1
|
|
self.stdout.write(
|
|
f"Course {course.id} ({course.slug}): {course.lessons_count} -> {actual_count}"
|
|
)
|
|
if not dry_run:
|
|
Course.objects.filter(pk=course.pk).update(lessons_count=actual_count)
|
|
|
|
if dry_run:
|
|
self.stdout.write(
|
|
self.style.WARNING(
|
|
f"Dry run complete. {updated_courses} course(s) would be updated."
|
|
)
|
|
)
|
|
else:
|
|
self.stdout.write(
|
|
self.style.SUCCESS(
|
|
f"Sync complete. {updated_courses} course(s) updated."
|
|
)
|
|
)
|