A simple ticketing application written in Python/Django
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

models.py 4.0KB

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