thread.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.pgutils import PgPartialIndex
  7. from misago.core.utils import slugify
  8. @python_2_unicode_compatible
  9. class Thread(models.Model):
  10. WEIGHT_DEFAULT = 0
  11. WEIGHT_PINNED = 1
  12. WEIGHT_GLOBAL = 2
  13. WEIGHT_CHOICES = [
  14. (WEIGHT_DEFAULT, _("Don't pin thread")),
  15. (WEIGHT_PINNED, _("Pin thread within category")),
  16. (WEIGHT_GLOBAL, _("Pin thread globally")),
  17. ]
  18. category = models.ForeignKey(
  19. 'misago_categories.Category',
  20. on_delete=models.CASCADE,
  21. )
  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_events = models.BooleanField(default=False)
  26. has_poll = models.BooleanField(default=False)
  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_post_is_event = models.BooleanField(default=False)
  56. last_poster = models.ForeignKey(
  57. settings.AUTH_USER_MODEL,
  58. related_name='last_poster_set',
  59. null=True,
  60. blank=True,
  61. on_delete=models.SET_NULL,
  62. )
  63. last_poster_name = models.CharField(max_length=255, null=True, blank=True)
  64. last_poster_slug = models.CharField(max_length=255, null=True, blank=True)
  65. weight = models.PositiveIntegerField(default=WEIGHT_DEFAULT)
  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. answer = models.ForeignKey(
  70. 'misago_threads.Post',
  71. related_name='+',
  72. null=True,
  73. blank=True,
  74. on_delete=models.SET_NULL,
  75. )
  76. answer_is_protected = models.BooleanField(default=False)
  77. answer_set_on = models.DateTimeField(null=True, blank=True)
  78. participants = models.ManyToManyField(
  79. settings.AUTH_USER_MODEL,
  80. related_name='privatethread_set',
  81. through='ThreadParticipant',
  82. through_fields=('thread', 'user'),
  83. )
  84. class Meta:
  85. indexes = [
  86. PgPartialIndex(
  87. fields=['weight'],
  88. where={'weight': 2},
  89. ),
  90. PgPartialIndex(
  91. fields=['weight'],
  92. where={'weight': 1},
  93. ),
  94. PgPartialIndex(
  95. fields=['weight'],
  96. where={'weight': 0},
  97. ),
  98. PgPartialIndex(
  99. fields=['weight'],
  100. where={'weight__lt': 2},
  101. ),
  102. PgPartialIndex(
  103. fields=['has_reported_posts'],
  104. where={'has_reported_posts': True},
  105. ),
  106. PgPartialIndex(
  107. fields=['has_unapproved_posts'],
  108. where={'has_unapproved_posts': True},
  109. ),
  110. PgPartialIndex(
  111. fields=['is_hidden'],
  112. where={'is_hidden': False},
  113. ),
  114. ]
  115. index_together = [
  116. ['category', 'id'],
  117. ['category', 'last_post_on'],
  118. ['category', 'replies'],
  119. ]
  120. def __str__(self):
  121. return self.title
  122. def delete(self, *args, **kwargs):
  123. from misago.threads.signals import delete_thread
  124. delete_thread.send(sender=self)
  125. super(Thread, self).delete(*args, **kwargs)
  126. def merge(self, other_thread):
  127. if self.pk == other_thread.pk:
  128. raise ValueError("thread can't be merged with itself")
  129. from misago.threads.signals import merge_thread
  130. merge_thread.send(sender=self, other_thread=other_thread)
  131. def move(self, new_category):
  132. from misago.threads.signals import move_thread
  133. self.category = new_category
  134. move_thread.send(sender=self)
  135. def synchronize(self):
  136. try:
  137. self.has_poll = bool(self.poll)
  138. except ObjectDoesNotExist:
  139. self.has_poll = False
  140. self.replies = self.post_set.filter(is_event=False, is_unapproved=False).count()
  141. if self.replies > 0:
  142. self.replies -= 1
  143. reported_post_qs = self.post_set.filter(has_reports=True)
  144. self.has_reported_posts = reported_post_qs.exists()
  145. if self.has_reported_posts:
  146. open_reports_qs = self.post_set.filter(has_open_reports=True)
  147. self.has_open_reports = open_reports_qs.exists()
  148. else:
  149. self.has_open_reports = False
  150. unapproved_post_qs = self.post_set.filter(is_unapproved=True)
  151. self.has_unapproved_posts = unapproved_post_qs.exists()
  152. hidden_post_qs = self.post_set.filter(is_hidden=True)[:1]
  153. self.has_hidden_posts = hidden_post_qs.exists()
  154. posts = self.post_set.order_by('id')
  155. first_post = posts.first()
  156. self.set_first_post(first_post)
  157. last_post = posts.filter(is_unapproved=False).last()
  158. if last_post:
  159. self.set_last_post(last_post)
  160. else:
  161. self.set_last_post(first_post)
  162. self.has_events = False
  163. if last_post:
  164. if last_post.is_event:
  165. self.has_events = True
  166. else:
  167. self.has_events = self.post_set.filter(is_event=True).exists()
  168. @property
  169. def thread_type(self):
  170. return self.category.thread_type
  171. def get_api_url(self):
  172. return self.thread_type.get_thread_api_url(self)
  173. def get_editor_api_url(self):
  174. return self.thread_type.get_thread_editor_api_url(self)
  175. def get_merge_api_url(self):
  176. return self.thread_type.get_thread_merge_api_url(self)
  177. def get_posts_api_url(self):
  178. return self.thread_type.get_thread_posts_api_url(self)
  179. def get_post_merge_api_url(self):
  180. return self.thread_type.get_post_merge_api_url(self)
  181. def get_post_move_api_url(self):
  182. return self.thread_type.get_post_move_api_url(self)
  183. def get_post_split_api_url(self):
  184. return self.thread_type.get_post_split_api_url(self)
  185. def get_poll_api_url(self):
  186. return self.thread_type.get_thread_poll_api_url(self)
  187. def get_absolute_url(self, page=1):
  188. return self.thread_type.get_thread_absolute_url(self, page)
  189. def get_new_post_url(self):
  190. return self.thread_type.get_thread_new_post_url(self)
  191. def get_last_post_url(self):
  192. return self.thread_type.get_thread_last_post_url(self)
  193. def get_unapproved_post_url(self):
  194. return self.thread_type.get_thread_unapproved_post_url(self)
  195. def set_title(self, title):
  196. self.title = title
  197. self.slug = slugify(title)
  198. def set_first_post(self, post):
  199. self.started_on = post.posted_on
  200. self.first_post = post
  201. self.starter = post.poster
  202. self.starter_name = post.poster_name
  203. if post.poster:
  204. self.starter_slug = post.poster.slug
  205. else:
  206. self.starter_slug = slugify(post.poster_name)
  207. self.is_unapproved = post.is_unapproved
  208. self.is_hidden = post.is_hidden
  209. def set_last_post(self, post):
  210. self.last_post_on = post.posted_on
  211. self.last_post_is_event = post.is_event
  212. self.last_post = post
  213. self.last_poster = post.poster
  214. self.last_poster_name = post.poster_name
  215. if post.poster:
  216. self.last_poster_slug = post.poster.slug
  217. else:
  218. self.last_poster_slug = slugify(post.poster_name)