comment.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. # DATA
  7. author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, blank=True, null=True, related_name="tickets_comments")
  8. ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
  9. # FK
  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