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.6KB

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