threadparticipant.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from django.db import models
  2. from misago.conf import settings
  3. class ThreadParticipantManager(models.Manager):
  4. def remove_participant(self, thread, user):
  5. ThreadParticipant.objects.filter(thread=thread, user=user).delete()
  6. def set_owner(self, thread, user):
  7. # remove existing owner
  8. ThreadParticipant.objects.filter(
  9. thread=thread,
  10. is_owner=True
  11. ).update(is_owner=False)
  12. # add (or re-add) user as thread owner
  13. self.remove_participant(thread, user)
  14. ThreadParticipant.objects.create(
  15. thread=thread,
  16. user=user,
  17. is_owner=True
  18. )
  19. def add_participant(self, thread, user, is_owner=False):
  20. ThreadParticipant.objects.create(
  21. thread=thread,
  22. user=user,
  23. is_owner=is_owner
  24. )
  25. class ThreadParticipant(models.Model):
  26. thread = models.ForeignKey('misago_threads.Thread')
  27. user = models.ForeignKey(settings.AUTH_USER_MODEL)
  28. is_owner = models.BooleanField(default=False)
  29. objects = ThreadParticipantManager()