post.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from __future__ import unicode_literals
  2. import copy
  3. from django.contrib.postgres.fields import JSONField
  4. from django.contrib.postgres.search import SearchVector, SearchVectorField
  5. from django.db import models
  6. from django.dispatch import receiver
  7. from django.urls import reverse
  8. from django.utils import six, timezone
  9. from django.utils.encoding import python_2_unicode_compatible
  10. from misago.conf import settings
  11. from misago.core.utils import parse_iso8601_string
  12. from misago.markup import finalise_markup
  13. from misago.threads import threadtypes
  14. from misago.threads.checksums import is_post_valid, update_post_checksum
  15. @python_2_unicode_compatible
  16. class Post(models.Model):
  17. category = models.ForeignKey('misago_categories.Category')
  18. thread = models.ForeignKey('misago_threads.Thread')
  19. poster = models.ForeignKey(
  20. settings.AUTH_USER_MODEL,
  21. blank=True,
  22. null=True,
  23. on_delete=models.SET_NULL,
  24. )
  25. poster_name = models.CharField(max_length=255)
  26. poster_ip = models.GenericIPAddressField()
  27. original = models.TextField()
  28. parsed = models.TextField()
  29. checksum = models.CharField(max_length=64, default='-')
  30. mentions = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="mention_set")
  31. attachments_cache = JSONField(null=True, blank=True)
  32. posted_on = models.DateTimeField()
  33. updated_on = models.DateTimeField()
  34. hidden_on = models.DateTimeField(default=timezone.now)
  35. edits = models.PositiveIntegerField(default=0)
  36. last_editor = models.ForeignKey(
  37. settings.AUTH_USER_MODEL,
  38. blank=True,
  39. null=True,
  40. on_delete=models.SET_NULL,
  41. related_name='+',
  42. )
  43. last_editor_name = models.CharField(max_length=255, null=True, blank=True)
  44. last_editor_slug = models.SlugField(max_length=255, null=True, blank=True)
  45. hidden_by = models.ForeignKey(
  46. settings.AUTH_USER_MODEL,
  47. blank=True,
  48. null=True,
  49. on_delete=models.SET_NULL,
  50. related_name='+',
  51. )
  52. hidden_by_name = models.CharField(max_length=255, null=True, blank=True)
  53. hidden_by_slug = models.SlugField(max_length=255, null=True, blank=True)
  54. has_reports = models.BooleanField(default=False)
  55. has_open_reports = models.BooleanField(default=False)
  56. is_unapproved = models.BooleanField(default=False, db_index=True)
  57. is_hidden = models.BooleanField(default=False)
  58. is_protected = models.BooleanField(default=False)
  59. is_event = models.BooleanField(default=False, db_index=True)
  60. event_type = models.CharField(max_length=255, null=True, blank=True)
  61. event_context = JSONField(null=True, blank=True)
  62. likes = models.PositiveIntegerField(default=0)
  63. last_likes = JSONField(null=True, blank=True)
  64. liked_by = models.ManyToManyField(
  65. settings.AUTH_USER_MODEL,
  66. related_name='liked_post_set',
  67. through='misago_threads.PostLike',
  68. )
  69. search_document = models.TextField(null=True, blank=True)
  70. search_vector = SearchVectorField()
  71. class Meta:
  72. index_together = [
  73. ('thread', 'id'), # speed up threadview for team members
  74. ('is_event', 'is_hidden'),
  75. ('poster', 'posted_on')
  76. ]
  77. def __str__(self):
  78. return '%s...' % self.original[10:].strip()
  79. def delete(self, *args, **kwargs):
  80. from misago.threads.signals import delete_post
  81. delete_post.send(sender=self)
  82. super(Post, self).delete(*args, **kwargs)
  83. def merge(self, other_post):
  84. if self.thread_id != other_post.thread_id:
  85. raise ValueError("only posts belonging to same thread can be merged")
  86. if self.is_event or other_post.is_event:
  87. raise ValueError("can't merge events")
  88. if self.pk == other_post.pk:
  89. raise ValueError("post can't be merged with itself")
  90. other_post.original = six.text_type('\n\n').join((other_post.original, self.original))
  91. other_post.parsed = six.text_type('\n').join((other_post.parsed, self.parsed))
  92. update_post_checksum(other_post)
  93. from misago.threads.signals import merge_post
  94. merge_post.send(sender=self, other_post=other_post)
  95. def move(self, new_thread):
  96. from misago.threads.signals import move_post
  97. self.category = new_thread.category
  98. self.thread = new_thread
  99. move_post.send(sender=self)
  100. @property
  101. def attachments(self):
  102. if hasattr(self, '_hydrated_attachments_cache'):
  103. return self._hydrated_attachments_cache
  104. self._hydrated_attachments_cache = []
  105. if self.attachments_cache:
  106. for attachment in copy.deepcopy(self.attachments_cache):
  107. attachment['uploaded_on'] = parse_iso8601_string(attachment['uploaded_on'])
  108. self._hydrated_attachments_cache.append(attachment)
  109. return self._hydrated_attachments_cache
  110. @property
  111. def content(self):
  112. if not hasattr(self, '_finalised_parsed'):
  113. self._finalised_parsed = finalise_markup(self.parsed)
  114. return self._finalised_parsed
  115. @property
  116. def thread_type(self):
  117. return self.category.thread_type
  118. def get_api_url(self):
  119. return self.thread_type.get_post_api_url(self)
  120. def get_likes_api_url(self):
  121. return self.thread_type.get_post_likes_api_url(self)
  122. def get_editor_api_url(self):
  123. return self.thread_type.get_post_editor_api_url(self)
  124. def get_edits_api_url(self):
  125. return self.thread_type.get_post_edits_api_url(self)
  126. def get_read_api_url(self):
  127. return self.thread_type.get_post_read_api_url(self)
  128. def get_absolute_url(self):
  129. return self.thread_type.get_post_absolute_url(self)
  130. def set_search_document(self, thread_title=None):
  131. if thread_title:
  132. self.search_document = '\n\n'.join([thread_title, self.original])
  133. else:
  134. self.search_document = self.original
  135. def update_search_vector(self):
  136. self.search_vector = SearchVector(
  137. 'search_document',
  138. config=settings.MISAGO_SEARCH_CONFIG,
  139. )
  140. @property
  141. def short(self):
  142. if self.is_valid:
  143. if len(self.original) > 150:
  144. return six.text_type('%s...') % self.original[:150].strip()
  145. else:
  146. return self.original
  147. else:
  148. return ''
  149. @property
  150. def is_valid(self):
  151. return is_post_valid(self)
  152. @property
  153. def is_first_post(self):
  154. return self.pk == self.thread.first_post_id