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.
63 lines
2.2 KiB
63 lines
2.2 KiB
from django.core.management.base import BaseCommand
|
|
from django.db import transaction
|
|
from django.db.models import Exists, OuterRef
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from apps.chat.models import RoomMessage
|
|
from apps.course.models import Course
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = _('Ensure every course has a group chat room')
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
'--dry-run',
|
|
action='store_true',
|
|
dest='dry_run',
|
|
help=_('Show courses missing group rooms without creating them'),
|
|
)
|
|
|
|
def handle(self, *args, **options):
|
|
dry_run = options['dry_run']
|
|
|
|
group_room_exists = RoomMessage.objects.filter(
|
|
course=OuterRef('pk'),
|
|
room_type=RoomMessage.RoomTypeChoices.GROUP,
|
|
)
|
|
|
|
courses_without_group_room = Course.objects.prefetch_related('professors').annotate(
|
|
has_group_room=Exists(group_room_exists)
|
|
).filter(
|
|
has_group_room=False
|
|
)
|
|
|
|
missing_count = courses_without_group_room.count()
|
|
|
|
if missing_count == 0:
|
|
self.stdout.write(self.style.SUCCESS(_('All courses already have a group room.')))
|
|
return
|
|
|
|
self.stdout.write(self.style.WARNING(_(f'Found {missing_count} course(s) without a group room.')))
|
|
|
|
for course in courses_without_group_room:
|
|
self.stdout.write(f'- Course #{course.id}: {course.title}')
|
|
|
|
if dry_run:
|
|
self.stdout.write(self.style.WARNING(_('Dry run completed. No rooms were created.')))
|
|
return
|
|
|
|
created_count = 0
|
|
with transaction.atomic():
|
|
for course in courses_without_group_room:
|
|
RoomMessage.objects.create(
|
|
name=f"{course.title} - Group",
|
|
description=f"Group chat for course: {course.title}",
|
|
initiator=course.professors.first(),
|
|
course=course,
|
|
room_type=RoomMessage.RoomTypeChoices.GROUP,
|
|
is_locked=course.is_group_chat_locked,
|
|
)
|
|
created_count += 1
|
|
|
|
self.stdout.write(self.style.SUCCESS(_(f'Created {created_count} missing group room(s).')))
|