post.py 6.5 KB

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