post.py 5.2 KB

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