threadparticipant.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from django.db import models, transaction
  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. @transaction.atomic
  7. def set_owner(self, thread, user):
  8. thread_owner = ThreadParticipant.objects.filter(
  9. thread=thread, is_owner=True)
  10. thread_owner.update(is_owner=False)
  11. self.remove_participant(thread, user)
  12. ThreadParticipant.objects.create(
  13. thread=thread,
  14. user=user,
  15. is_owner=True)
  16. @transaction.atomic
  17. def add_participant(self, thread, user, is_owner=False):
  18. ThreadParticipant.objects.create(
  19. thread=thread,
  20. user=user,
  21. is_owner=is_owner)
  22. class ThreadParticipant(models.Model):
  23. thread = models.ForeignKey('misago_threads.Thread')
  24. user = models.ForeignKey(settings.AUTH_USER_MODEL)
  25. is_owner = models.BooleanField(default=False)
  26. objects = ThreadParticipantManager()