threadparticipant.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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(
  6. thread=thread,
  7. is_owner=True
  8. ).update(is_owner=False)
  9. self.remove_participant(thread, user)
  10. ThreadParticipant.objects.create(
  11. thread=thread,
  12. user=user,
  13. is_owner=True
  14. )
  15. def add_participants(self, thread, users):
  16. bulk = []
  17. for user in users:
  18. bulk.append(ThreadParticipant(
  19. thread=thread,
  20. user=user,
  21. is_owner=False
  22. ))
  23. ThreadParticipant.objects.bulk_create(bulk)
  24. def remove_participant(self, thread, user):
  25. ThreadParticipant.objects.filter(
  26. thread=thread,
  27. user=user
  28. ).delete()
  29. class ThreadParticipant(models.Model):
  30. thread = models.ForeignKey('misago_threads.Thread')
  31. user = models.ForeignKey(settings.AUTH_USER_MODEL)
  32. is_owner = models.BooleanField(default=False)
  33. objects = ThreadParticipantManager()