comment.py 1.2 KB

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