thread.py 7.1 KB

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