comment.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from django.db import models
  2. from django.conf import settings
  3. from tickets.models.ticket import Ticket
  4. class Comment(models.Model):
  5. author = models.ForeignKey(
  6. settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True,
  7. related_name="tickets_comments"
  8. )
  9. ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
  10. date = models.DateTimeField(auto_now_add=True)
  11. email_from = models.CharField(max_length=320, blank=True, null=True)
  12. email_message_id = models.CharField(max_length=255, blank=True, null=True)
  13. body = models.TextField(blank=True)
  14. class Meta:
  15. unique_together = ("ticket", "email_message_id")
  16. @property
  17. def author_text(self):
  18. if self.author is not None:
  19. return str(self.author)
  20. assert self.email_message_id is not None
  21. return str(self.email_from)
  22. @property
  23. def snippet(self):
  24. body_snippet = textwrap.shorten(self.body, width=35, placeholder="...")
  25. # Define here rather than in __str__ so we can use it in the admin list_display
  26. return "{author} - {snippet}...".format(author=self.author_text, snippet=body_snippet)
  27. def __str__(self):
  28. return self.snippet