thread.py 6.7 KB

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