A simple ticketing application written in Python/Django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. from django.db import models
  3. from django.contrib.auth.models import User
  4. try:
  5. from django.utils import timezone
  6. except ImportError:
  7. from datetime import datetime as timezone
  8. def user_unicode(self):
  9. """
  10. return 'last_name, first_name' for User by default
  11. """
  12. return u'%s, %s' % (self.last_name, self.first_name)
  13. User.__unicode__ = user_unicode
  14. class Ticket(models.Model):
  15. title = models.CharField('Title', max_length=255)
  16. owner = models.ForeignKey(User, related_name='owner', blank=True, null=True, verbose_name='Owner', )
  17. description = models.TextField('Description', blank=True, null=True)
  18. STATUS_CHOICES = (
  19. ('TODO', 'TODO'),
  20. ('IN PROGRESS', 'IN PROGRESS'),
  21. ('WAITING', 'WAITING'),
  22. ('DONE', 'DONE'),
  23. )
  24. status = models.CharField('Status', choices=STATUS_CHOICES, max_length=255, blank=True, null=True)
  25. waiting_for = models.ForeignKey(User, related_name='waiting_for', blank=True, null=True, verbose_name='Waiting For', )
  26. closed_date = models.DateTimeField(blank=True, null=True) # set in view when status changed to "DONE"
  27. assigned_to = models.ForeignKey(User,
  28. related_name='assigned_to',
  29. blank=True,
  30. null=True,
  31. verbose_name='Assigned to',)
  32. created = models.DateTimeField(auto_now_add=True)
  33. updated = models.DateTimeField(auto_now=True)
  34. def __unicode__(self):
  35. return str(self.id)
  36. class FollowUp(models.Model):
  37. """
  38. A FollowUp is a comment to a ticket.
  39. """
  40. ticket = models.ForeignKey(Ticket, verbose_name='Ticket', )
  41. date = models.DateTimeField('Date', default=timezone.now)
  42. title = models.CharField('Title', max_length=200, )
  43. text = models.TextField('Text', blank=True, null=True, )
  44. user = models.ForeignKey(User, blank=True, null=True, verbose_name='User', )
  45. created = models.DateTimeField(auto_now_add=True)
  46. modified = models.DateTimeField(auto_now=True)
  47. class Meta:
  48. ordering = ['-modified', ]
  49. def attachment_path(instance, filename):
  50. """
  51. Provide a file path that will help prevent files being overwritten, by
  52. putting attachments in a folder off attachments for ticket/followup_id/.
  53. """
  54. import os
  55. from django.conf import settings
  56. os.umask(0)
  57. path = 'tickets/%s' % instance.ticket.id
  58. print(path)
  59. att_path = os.path.join(settings.MEDIA_ROOT, path)
  60. if settings.DEFAULT_FILE_STORAGE == "django.core.files.storage.FileSystemStorage":
  61. if not os.path.exists(att_path):
  62. os.makedirs(att_path, 0777)
  63. return os.path.join(path, filename)
  64. class Attachment(models.Model):
  65. ticket = models.ForeignKey(Ticket, verbose_name='Ticket', )
  66. file = models.FileField('File', upload_to=attachment_path, max_length=1000, )
  67. filename = models.CharField('Filename', max_length=1000, )
  68. user = models.ForeignKey(User, blank=True, null=True, verbose_name='User', )
  69. created = models.DateTimeField(auto_now_add=True)
  70. def get_upload_to(self, field_attname):
  71. """ Get upload_to path specific to this item """
  72. if not self.id:
  73. return u''
  74. return u'../media/tickets/%s' % (
  75. self.ticket.id,
  76. )
  77. class Meta:
  78. #ordering = ['filename', ]
  79. verbose_name = 'Attachment'
  80. verbose_name_plural = 'Attachments'