attachment.py 827 B

123456789101112131415161718192021222324252627
  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. ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
  9. added_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
  10. timestamp = models.DateTimeField(auto_now_add=True)
  11. file = models.FileField(upload_to=get_attachment_upload_dir, max_length=255)
  12. def filename(self):
  13. return os.path.basename(self.file.name)
  14. def extension(self):
  15. name, extension = os.path.splitext(self.file.name)
  16. return extension
  17. def __str__(self):
  18. return f"{self.ticket.pk} - {self.file.name}"