| 1 | from django.db import models |
|---|
| 2 | from django.contrib.auth.models import User |
|---|
| 3 | from django.utils.translation import ugettext_lazy as _ |
|---|
| 4 | |
|---|
| 5 | from datetime import datetime |
|---|
| 6 | #from utilities import safe_truncate |
|---|
| 7 | |
|---|
| 8 | import choices |
|---|
| 9 | |
|---|
| 10 | class Ticket(models.Model): |
|---|
| 11 | class Meta: |
|---|
| 12 | ordering = ['-created'] |
|---|
| 13 | |
|---|
| 14 | creator = models.ForeignKey(User, related_name='ticket_creator', editable=False) |
|---|
| 15 | created = models.DateTimeField(_('Date of Registration'),default=datetime.now, |
|---|
| 16 | editable=False) |
|---|
| 17 | context = models.CharField(_('Context'), max_length=256, |
|---|
| 18 | choices=choices.TICKET_CONTEXT, default=choices.TICKET_CONTEXT[0][0], ) |
|---|
| 19 | type = models.CharField(_('Ticket Type'), max_length=256, |
|---|
| 20 | choices=choices.TICKET_TYPE, default=choices.TICKET_TYPE[0][0], ) |
|---|
| 21 | |
|---|
| 22 | @models.permalink |
|---|
| 23 | def get_absolute_url(self): |
|---|
| 24 | return ('ticket.history', [str(self.id)]) |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | def __unicode__(self): |
|---|
| 28 | return u'%s' % (self.context) |
|---|
| 29 | |
|---|
| 30 | def save(self): |
|---|
| 31 | self.updated = datetime.now() |
|---|
| 32 | super(Ticket, self).save() |
|---|
| 33 | |
|---|
| 34 | def opened_tickets(self): |
|---|
| 35 | ''' return set of Followups related to this ticket with a |
|---|
| 36 | status = new or open |
|---|
| 37 | ''' |
|---|
| 38 | return (r.followup for r in |
|---|
| 39 | self.followup_set.filter(status="new").select_related()) |
|---|
| 40 | |
|---|
| 41 | def is_closed(self): |
|---|
| 42 | ''' return a boolean if the ticket is already close |
|---|
| 43 | ''' |
|---|
| 44 | fw = self.followup_set.latest() |
|---|
| 45 | return fw.status == "closed" |
|---|
| 46 | |
|---|
| 47 | def is_resolved(self): |
|---|
| 48 | ''' return a boolean if the ticket is already resolved |
|---|
| 49 | ''' |
|---|
| 50 | fw = self.followup_set.latest() |
|---|
| 51 | return fw.status == "resolved" |
|---|
| 52 | |
|---|
| 53 | def reopen(self): |
|---|
| 54 | ''' reopen tickect |
|---|
| 55 | ''' |
|---|
| 56 | fw = self.followup_set.latest() |
|---|
| 57 | fw.status = "reopened" |
|---|
| 58 | return fw.status == "closed" |
|---|
| 59 | |
|---|
| 60 | def closed_tickets(self): |
|---|
| 61 | ''' return set of Followups related to this ticket with a |
|---|
| 62 | status = close |
|---|
| 63 | ''' |
|---|
| 64 | return (r.followup for r in |
|---|
| 65 | self.followup_set.filter(status="close").select_related()) |
|---|
| 66 | |
|---|
| 67 | class Followup(models.Model): |
|---|
| 68 | ticket = models.ForeignKey(Ticket, db_index=True) |
|---|
| 69 | iteration_date = models.DateTimeField(_('Date of Iteration'), db_index=True, |
|---|
| 70 | editable=False, ) |
|---|
| 71 | subject = models.CharField(_('Subject'), max_length=256, db_index=True) |
|---|
| 72 | description = models.TextField(_('Description'), max_length=2000, ) |
|---|
| 73 | reported_by = models.ForeignKey(User, related_name='ticket_reported_by', |
|---|
| 74 | editable=False, null=True, blank=True, db_index=True, ) |
|---|
| 75 | to_user = models.EmailField(_('To User (e-mail)'), max_length=256, db_index=True, |
|---|
| 76 | editable=False, null=True, blank=True ) |
|---|
| 77 | status = models.CharField(_('Ticket Status'), max_length=256, |
|---|
| 78 | choices=choices.TICKET_STATUS, default=choices.TICKET_STATUS[0][0], |
|---|
| 79 | db_index=True, ) |
|---|
| 80 | |
|---|
| 81 | class Meta: |
|---|
| 82 | get_latest_by = 'iteration_date' |
|---|
| 83 | |
|---|
| 84 | def save(self): |
|---|
| 85 | self.iteration_date = datetime.now() |
|---|
| 86 | super(Followup, self).save() |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | class Media(models.Model): |
|---|
| 90 | followup = models.ForeignKey(Followup, related_name="ticket_media", null=True, |
|---|
| 91 | blank=True, db_index=True, ) |
|---|
| 92 | file = models.FileField(_('Upload File'), blank=True, |
|---|
| 93 | upload_to='tickets_files', ) |
|---|