thread.py 7.3 KB

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