threadparticipant.py 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. from django.db import models
  2. from ...conf import settings
  3. class ThreadParticipantManager(models.Manager):
  4. def set_owner(self, thread, user):
  5. ThreadParticipant.objects.filter(thread=thread, is_owner=True).update(
  6. is_owner=False
  7. )
  8. self.remove_participant(thread, user)
  9. ThreadParticipant.objects.create(thread=thread, user=user, is_owner=True)
  10. def add_participants(self, thread, users):
  11. bulk = []
  12. for user in users:
  13. bulk.append(ThreadParticipant(thread=thread, user=user, is_owner=False))
  14. ThreadParticipant.objects.bulk_create(bulk)
  15. def remove_participant(self, thread, user):
  16. ThreadParticipant.objects.filter(thread=thread, user=user).delete()
  17. class ThreadParticipant(models.Model):
  18. thread = models.ForeignKey("misago_threads.Thread", on_delete=models.CASCADE)
  19. user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  20. is_owner = models.BooleanField(default=False)
  21. objects = ThreadParticipantManager()