thread.py 7.0 KB

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