3 changed files with 61 additions and 12 deletions
-
25apps/course/management/commands/assign_professors.py
-
9apps/course/serializers/course.py
-
39apps/course/signals.py
@ -0,0 +1,25 @@ |
|||
from django.core.management.base import BaseCommand |
|||
from apps.course.models import Course |
|||
from apps.account.models import ProfessorUser |
|||
import random |
|||
|
|||
class Command(BaseCommand): |
|||
help = 'Assign at least one professor to every course (if missing)' |
|||
|
|||
def handle(self, *args, **options): |
|||
# Gather all active professors |
|||
professors = list(ProfessorUser.objects.filter(is_active=True)) |
|||
if not professors: |
|||
self.stdout.write(self.style.ERROR('No active professors found.')) |
|||
return |
|||
|
|||
updated = 0 |
|||
for course in Course.objects.all(): |
|||
if course.professors.exists(): |
|||
continue # already has a professor |
|||
professor = random.choice(professors) |
|||
course.professors.add(professor) |
|||
updated += 1 |
|||
self.stdout.write(f'Added professor {professor.id} to course {course.id}') |
|||
|
|||
self.stdout.write(self.style.SUCCESS(f'Finished. Updated {updated} course(s).')) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue