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.
34 lines
1.3 KiB
34 lines
1.3 KiB
from django.db.models.signals import post_save
|
|
from django.dispatch import receiver
|
|
|
|
from apps.course.models import Course
|
|
from apps.chat.models import RoomMessage
|
|
|
|
|
|
@receiver(post_save, sender=Course)
|
|
def handle_room_message_for_course(sender, instance, created, **kwargs):
|
|
if created: # فقط برای موارد جدید اجرا شود
|
|
RoomMessage.objects.create(
|
|
name=f"{instance.title} - Group",
|
|
description=f"Group chat for course: {instance.title}",
|
|
initiator=instance.professor, # استاد بهعنوان سازنده اتاق
|
|
course=instance,
|
|
room_type=RoomMessage.RoomTypeChoices.GROUP
|
|
)
|
|
else: # این بخش در زمان آپدیت دوره اجرا میشود
|
|
# Find the existing group room for this course and update its details
|
|
RoomMessage.objects.filter(
|
|
course=instance,
|
|
room_type=RoomMessage.RoomTypeChoices.GROUP
|
|
).update(
|
|
name=f"{instance.title} - Group",
|
|
description=f"Group chat for course: {instance.title}",
|
|
initiator=instance.professor
|
|
)
|
|
|
|
|
|
@receiver(post_save, sender=Course)
|
|
def ensure_professor_role(sender, instance, **kwargs):
|
|
professor = getattr(instance, 'professor', None)
|
|
if professor:
|
|
professor.ensure_professor_profile()
|