event.py 925 B

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