thread.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from django.db import models, transaction
  2. from django.utils.translation import ugettext_lazy as _
  3. from misago.conf import settings
  4. from misago.core.utils import slugify
  5. __all__ = [
  6. 'THREAD_WEIGHT_DEFAULT',
  7. 'THREAD_WEIGHT_PINNED',
  8. 'THREAD_WEIGHT_GLOBAL',
  9. 'THREAD_WEIGHT_CHOICES',
  10. 'Thread',
  11. ]
  12. THREAD_WEIGHT_DEFAULT = 0
  13. THREAD_WEIGHT_PINNED = 1
  14. THREAD_WEIGHT_GLOBAL = 2
  15. THREAD_WEIGHT_CHOICES = (
  16. (THREAD_WEIGHT_DEFAULT, _("Don't pin thread")),
  17. (THREAD_WEIGHT_PINNED, _("Pin thread within category")),
  18. (THREAD_WEIGHT_GLOBAL, _("Pin thread globally"))
  19. )
  20. class Thread(models.Model):
  21. category = models.ForeignKey('misago_categories.Category')
  22. title = models.CharField(max_length=255)
  23. slug = models.CharField(max_length=255)
  24. replies = models.PositiveIntegerField(default=0, db_index=True)
  25. has_reported_posts = models.BooleanField(default=False)
  26. has_open_reports = models.BooleanField(default=False)
  27. has_unapproved_posts = models.BooleanField(default=False)
  28. has_hidden_posts = models.BooleanField(default=False)
  29. started_on = models.DateTimeField(db_index=True)
  30. last_post_on = models.DateTimeField(db_index=True)
  31. first_post = models.ForeignKey(
  32. 'misago_threads.Post',
  33. related_name='+',
  34. null=True,
  35. blank=True,
  36. on_delete=models.SET_NULL
  37. )
  38. starter = models.ForeignKey(
  39. settings.AUTH_USER_MODEL,
  40. null=True,
  41. blank=True,
  42. on_delete=models.SET_NULL
  43. )
  44. starter_name = models.CharField(max_length=255)
  45. starter_slug = models.CharField(max_length=255)
  46. last_post = models.ForeignKey(
  47. 'misago_threads.Post',
  48. related_name='+',
  49. null=True,
  50. blank=True,
  51. on_delete=models.SET_NULL
  52. )
  53. last_poster = models.ForeignKey(
  54. settings.AUTH_USER_MODEL,
  55. related_name='last_poster_set',
  56. null=True,
  57. blank=True,
  58. on_delete=models.SET_NULL
  59. )
  60. last_poster_name = models.CharField(max_length=255, null=True, blank=True)
  61. last_poster_slug = models.CharField(max_length=255, null=True, blank=True)
  62. weight = models.PositiveIntegerField(default=THREAD_WEIGHT_DEFAULT)
  63. is_poll = models.BooleanField(default=False)
  64. is_unapproved = models.BooleanField(default=False, db_index=True)
  65. is_hidden = models.BooleanField(default=False)
  66. is_closed = models.BooleanField(default=False)
  67. participants = models.ManyToManyField(
  68. settings.AUTH_USER_MODEL,
  69. related_name='private_thread_set',
  70. through='ThreadParticipant',
  71. through_fields=('thread', 'user')
  72. )
  73. class Meta:
  74. index_together = [
  75. ['category', 'id'],
  76. ['category', 'last_post_on'],
  77. ['category', 'replies'],
  78. ]
  79. def __unicode__(self):
  80. return self.title
  81. def lock(self):
  82. return Thread.objects.select_for_update().get(id=self.id)
  83. def delete(self, *args, **kwargs):
  84. from ..signals import delete_thread
  85. delete_thread.send(sender=self)
  86. super(Thread, self).delete(*args, **kwargs)
  87. def merge(self, other_thread):
  88. if self.pk == other_thread.pk:
  89. raise ValueError("thread can't be merged with itself")
  90. from ..signals import merge_thread
  91. merge_thread.send(sender=self, other_thread=other_thread)
  92. def move(self, new_category):
  93. from ..signals import move_thread
  94. self.category = new_category
  95. move_thread.send(sender=self)
  96. def synchronize(self):
  97. self.replies = self.post_set.filter(is_event=False, is_unapproved=False).count()
  98. if self.replies > 0:
  99. self.replies -= 1
  100. reported_post_qs = self.post_set.filter(has_reports=True)
  101. self.has_reported_posts = reported_post_qs.exists()
  102. if self.has_reported_posts:
  103. open_reports_qs = self.post_set.filter(has_open_reports=True)
  104. self.has_open_reports = open_reports_qs.exists()
  105. else:
  106. self.has_open_reports = False
  107. unapproved_post_qs = self.post_set.filter(is_unapproved=True)
  108. self.has_unapproved_posts = unapproved_post_qs.exists()
  109. hidden_post_qs = self.post_set.filter(is_hidden=True)[:1]
  110. self.has_hidden_posts = hidden_post_qs.exists()
  111. first_post = self.post_set.order_by('id')[:1][0]
  112. self.set_first_post(first_post)
  113. last_post_qs = self.post_set.filter(is_unapproved=False).order_by('-id')
  114. last_post = last_post_qs[:1]
  115. if last_post:
  116. self.set_last_post(last_post[0])
  117. else:
  118. self.set_last_post(first_post)
  119. @property
  120. def thread_type(self):
  121. return self.category.thread_type
  122. def get_api_url(self):
  123. return self.thread_type.get_thread_api_url(self)
  124. def get_absolute_url(self, page=1):
  125. return self.thread_type.get_thread_absolute_url(self, page)
  126. def get_new_post_url(self):
  127. return self.thread_type.get_thread_new_post_url(self)
  128. def get_last_post_url(self):
  129. return self.thread_type.get_thread_last_post_url(self)
  130. def get_unapproved_post_url(self):
  131. return self.thread_type.get_thread_unapproved_post_url(self)
  132. def set_title(self, title):
  133. self.title = title
  134. self.slug = slugify(title)
  135. def set_first_post(self, post):
  136. self.started_on = post.posted_on
  137. self.first_post = post
  138. self.starter = post.poster
  139. self.starter_name = post.poster_name
  140. if post.poster:
  141. self.starter_slug = post.poster.slug
  142. else:
  143. self.starter_slug = slugify(post.poster_name)
  144. self.is_unapproved = post.is_unapproved
  145. self.is_hidden = post.is_hidden
  146. def set_last_post(self, post):
  147. self.last_post_on = post.posted_on
  148. self.last_post = post
  149. self.last_poster = post.poster
  150. self.last_poster_name = post.poster_name
  151. if post.poster:
  152. self.last_poster_slug = post.poster.slug
  153. else:
  154. self.last_poster_slug = slugify(post.poster_name)