attachment.py 848 B

123456789101112131415161718192021222324252627282930
  1. import os
  2. from django.db import models
  3. from django.contrib.auth import get_user_model
  4. from tickets.models.ticket import Ticket
  5. def get_attachment_upload_dir(instance, filename):
  6. return "/".join(["attachments", str(instance.ticket.pk), filename])
  7. class Attachment(models.Model):
  8. # DATA
  9. timestamp = models.DateTimeField(auto_now_add=True)
  10. file = models.FileField(upload_to=get_attachment_upload_dir, max_length=255)
  11. # FK
  12. ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
  13. added_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
  14. def filename(self):
  15. return os.path.basename(self.file.name)
  16. def extension(self):
  17. name, extension = os.path.splitext(self.file.name)
  18. return extension
  19. def __str__(self):
  20. return f"{self.ticket.pk} - {self.file.name}"