threadparticipant.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from django.db import models
  2. from misago.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(is_owner=False)
  6. self.remove_participant(thread, user)
  7. ThreadParticipant.objects.create(thread=thread, user=user, is_owner=True)
  8. def add_participants(self, thread, users):
  9. bulk = []
  10. for user in users:
  11. bulk.append(ThreadParticipant(thread=thread, user=user, is_owner=False))
  12. ThreadParticipant.objects.bulk_create(bulk)
  13. def remove_participant(self, thread, user):
  14. ThreadParticipant.objects.filter(thread=thread, user=user).delete()
  15. class ThreadParticipant(models.Model):
  16. thread = models.ForeignKey(
  17. 'misago_threads.Thread',
  18. on_delete=models.CASCADE,
  19. )
  20. user = models.ForeignKey(
  21. settings.AUTH_USER_MODEL,
  22. on_delete=models.CASCADE,
  23. )
  24. is_owner = models.BooleanField(default=False)
  25. objects = ThreadParticipantManager()