2008-02-06 05:36:07 +01:00
"""
Jutda Helpdesk - A Django powered ticket tracker for small enterprise .
( c ) Copyright 2008 Jutda . All Rights Reserved . See LICENSE for details .
2007-12-27 01:29:17 +01:00
2008-02-06 05:36:07 +01:00
models . py - Model ( and hence database ) definitions . This is the core of the
helpdesk structure .
2007-12-27 01:29:17 +01:00
"""
2008-02-06 05:36:07 +01:00
2007-12-27 01:29:17 +01:00
from datetime import datetime
2008-01-15 01:20:00 +01:00
2007-12-27 01:29:17 +01:00
from django . contrib . auth . models import User
2008-01-15 01:20:00 +01:00
from django . db import models
from django . conf import settings
2008-02-08 01:19:58 +01:00
from django . dispatch import dispatcher
2007-12-27 01:29:17 +01:00
class Queue ( models . Model ) :
2007-12-27 02:35:34 +01:00
"""
A queue is a collection of tickets into what would generally be business
areas or departments .
For example , a company may have a queue for each Product they provide , or
a queue for each of Accounts , Pre - Sales , and Support .
TODO : Add e - mail inboxes ( either using piped e - mail or IMAP / POP3 ) so we
can automatically get tickets via e - mail .
"""
2007-12-27 01:29:17 +01:00
title = models . CharField ( maxlength = 100 )
2008-01-15 01:20:00 +01:00
slug = models . SlugField ( help_text = ' This slug is used when building ticket ID \' s. Once set, try not to change it or e-mailing may get messy. ' )
email_address = models . EmailField ( blank = True , null = True , help_text = ' All outgoing e-mails for this queue will use this e-mail address. If you use IMAP or POP3, this shoul be the e-mail address for that mailbox. ' )
2008-01-22 06:54:22 +01:00
escalate_days = models . IntegerField ( blank = True , null = True , help_text = ' For tickets which are not held, how often do you wish to increase their priority? Set to 0 for no escalation. ' )
2007-12-27 01:29:17 +01:00
2008-01-07 21:22:13 +01:00
def _from_address ( self ) :
2008-01-15 01:20:00 +01:00
if not self . email_address :
return ' NO QUEUE EMAIL ADDRESS DEFINED < %s > ' % settings . DEFAULT_FROM_EMAIL
else :
return ' %s < %s > ' % ( self . title , self . email_address )
2008-01-07 21:22:13 +01:00
from_address = property ( _from_address )
2008-01-21 02:02:12 +01:00
new_ticket_cc = models . EmailField ( blank = True , null = True , help_text = ' If an e-mail address is entered here, then it will receive notification of all new tickets created for this queue ' )
updated_ticket_cc = models . EmailField ( blank = True , null = True , help_text = ' If an e-mail address is entered here, then it will receive notification of all activity (new tickets, closed tickets, updates, reassignments, etc) for this queue ' )
2008-01-07 21:22:13 +01:00
email_box_type = models . CharField ( maxlength = 5 , choices = ( ( ' pop3 ' , ' POP 3 ' ) , ( ' imap ' , ' IMAP ' ) ) , blank = True , null = True , help_text = ' E-Mail Server Type - Both POP3 and IMAP are supported. Select your email server type here. ' )
email_box_host = models . CharField ( maxlength = 200 , blank = True , null = True , help_text = ' Your e-mail server address - either the domain name or IP address. May be " localhost " . ' )
email_box_port = models . IntegerField ( blank = True , null = True , help_text = ' Port number to use for accessing e-mail. Default for POP3 is " 110 " , and for IMAP is " 143 " . This may differ on some servers. ' )
email_box_user = models . CharField ( maxlength = 200 , blank = True , null = True , help_text = ' Username for accessing this mailbox. ' )
email_box_pass = models . CharField ( maxlength = 200 , blank = True , null = True , help_text = ' Password for the above username ' )
email_box_imap_folder = models . CharField ( maxlength = 100 , blank = True , null = True , help_text = ' If using IMAP, what folder do you wish to fetch messages from? This allows you to use one IMAP account for multiple queues, by filtering messages on your IMAP server into separate folders. Default: INBOX. ' )
email_box_interval = models . IntegerField ( help_text = ' How often do you wish to check this mailbox? (in Minutes) ' , blank = True , null = True , default = ' 5 ' )
email_box_last_check = models . DateTimeField ( blank = True , null = True , editable = False ) # Updated by the auto-pop3-and-imap-checker
2007-12-27 01:29:17 +01:00
def __unicode__ ( self ) :
return u " %s " % self . title
class Admin :
2008-01-15 01:20:00 +01:00
list_display = ( ' title ' , ' slug ' , ' email_address ' )
class Meta :
ordering = ( ' title ' , )
2008-01-07 21:22:13 +01:00
def save ( self ) :
if self . email_box_type == ' imap ' and not self . email_box_imap_folder :
self . email_box_imap_folder = ' INBOX '
super ( Queue , self ) . save ( )
2007-12-27 01:29:17 +01:00
2008-01-15 01:20:00 +01:00
2007-12-27 01:29:17 +01:00
class Ticket ( models . Model ) :
"""
To allow a ticket to be entered as quickly as possible , only the
bare minimum fields are required . These basically allow us to
sort and manage the ticket . The user can always go back and
enter more information later .
A good example of this is when a customer is on the phone , and
you want to give them a ticket ID as quickly as possible . You can
enter some basic info , save the ticket , give the customer the ID
and get off the phone , then add in further detail at a later time
( once the customer is not on the line ) .
2007-12-27 02:35:34 +01:00
Note that assigned_to is optional - unassigned tickets are displayed on
the dashboard to prompt users to take ownership of them .
2007-12-27 01:29:17 +01:00
"""
OPEN_STATUS = 1
REOPENED_STATUS = 2
RESOLVED_STATUS = 3
CLOSED_STATUS = 4
STATUS_CHOICES = (
( OPEN_STATUS , ' Open ' ) ,
( REOPENED_STATUS , ' Reopened ' ) ,
( RESOLVED_STATUS , ' Resolved ' ) ,
( CLOSED_STATUS , ' Closed ' ) ,
)
2008-01-10 01:28:45 +01:00
PRIORITY_CHOICES = (
( 1 , ' 1 (Critical) ' ) ,
( 2 , ' 2 (High) ' ) ,
( 3 , ' 3 (Normal) ' ) ,
( 4 , ' 4 (Low) ' ) ,
( 5 , ' 5 (Very Low) ' ) ,
)
2007-12-27 01:29:17 +01:00
title = models . CharField ( maxlength = 200 )
queue = models . ForeignKey ( Queue )
2008-01-10 01:28:45 +01:00
created = models . DateTimeField ( blank = True )
modified = models . DateTimeField ( blank = True )
2007-12-27 01:29:17 +01:00
submitter_email = models . EmailField ( blank = True , null = True , help_text = ' The submitter will receive an email for all public follow-ups left for this task. ' )
assigned_to = models . ForeignKey ( User , related_name = ' assigned_to ' , blank = True , null = True )
status = models . IntegerField ( choices = STATUS_CHOICES , default = OPEN_STATUS )
2008-01-16 05:52:30 +01:00
on_hold = models . BooleanField ( blank = True , null = True )
2007-12-27 01:29:17 +01:00
description = models . TextField ( blank = True , null = True )
resolution = models . TextField ( blank = True , null = True )
2008-01-10 01:28:45 +01:00
priority = models . IntegerField ( choices = PRIORITY_CHOICES , default = 3 , blank = 3 )
2008-01-22 06:54:22 +01:00
last_escalation = models . DateTimeField ( blank = True , null = True , editable = False )
2008-01-10 01:28:45 +01:00
2007-12-27 01:29:17 +01:00
def _get_assigned_to ( self ) :
2007-12-27 02:35:34 +01:00
""" Custom property to allow us to easily print ' Unassigned ' if a
ticket has no owner , or the users name if it ' s assigned. If the user
has a full name configured , we use that , otherwise their username . """
2007-12-27 01:29:17 +01:00
if not self . assigned_to :
return ' Unassigned '
else :
if self . assigned_to . get_full_name ( ) :
return self . assigned_to . get_full_name ( )
else :
return self . assigned_to
get_assigned_to = property ( _get_assigned_to )
2008-01-07 21:22:13 +01:00
def _get_ticket ( self ) :
""" A user-friendly ticket ID, which is a combination of ticket ID
and queue slug . This is generally used in e - mails . """
2008-01-16 01:26:24 +01:00
return " [ %s ] " % ( self . ticket_for_url )
2008-01-07 21:22:13 +01:00
ticket = property ( _get_ticket )
2008-01-16 01:26:24 +01:00
def _get_ticket_for_url ( self ) :
return " %s - %s " % ( self . queue . slug , self . id )
ticket_for_url = property ( _get_ticket_for_url )
2008-01-12 06:37:45 +01:00
def _get_priority_img ( self ) :
from django . conf import settings
return " %s /helpdesk/priorities/priority %s .png " % ( settings . MEDIA_URL , self . priority )
get_priority_img = property ( _get_priority_img )
2008-01-16 05:52:30 +01:00
def _get_status ( self ) :
held_msg = ' '
2008-01-16 06:00:58 +01:00
if self . on_hold : held_msg = ' - On Hold '
2008-01-21 00:31:27 +01:00
return ' %s %s ' % ( self . get_status_display ( ) , held_msg )
2008-01-16 05:52:30 +01:00
get_status = property ( _get_status )
2008-01-16 01:26:24 +01:00
def _get_ticket_url ( self ) :
from django . contrib . sites . models import Site
from django . core . urlresolvers import reverse
site = Site . objects . get_current ( )
return " http:// %s %s ?ticket= %s &email= %s " % ( site . domain , reverse ( ' helpdesk_public_view ' ) , self . ticket_for_url , self . submitter_email )
ticket_url = property ( _get_ticket_url )
2008-01-21 02:02:12 +01:00
def _get_staff_url ( self ) :
from django . contrib . sites . models import Site
from django . core . urlresolvers import reverse
site = Site . objects . get_current ( )
return " http:// %s %s " % ( site . domain , reverse ( ' helpdesk_view ' , args = [ self . id ] ) )
staff_url = property ( _get_staff_url )
2007-12-27 01:29:17 +01:00
class Admin :
2008-01-15 01:20:00 +01:00
list_display = ( ' title ' , ' status ' , ' assigned_to ' , ' submitter_email ' , )
2007-12-27 01:29:17 +01:00
date_hierarchy = ' created '
2008-01-15 01:20:00 +01:00
list_filter = ( ' assigned_to ' , ' status ' , )
2007-12-27 01:29:17 +01:00
class Meta :
get_latest_by = " created "
def __unicode__ ( self ) :
2008-01-15 01:20:00 +01:00
return u ' %s ' % self . title
2007-12-27 01:29:17 +01:00
def get_absolute_url ( self ) :
return ( ' helpdesk.views.view_ticket ' , [ str ( self . id ) ] )
2008-01-15 01:20:00 +01:00
get_absolute_url = models . permalink ( get_absolute_url )
2007-12-27 01:29:17 +01:00
def save ( self ) :
if not self . id :
# This is a new ticket as no ID yet exists.
self . created = datetime . now ( )
2008-01-10 01:28:45 +01:00
if not self . priority :
self . priority = 3
self . modified = datetime . now ( )
2007-12-27 01:29:17 +01:00
super ( Ticket , self ) . save ( )
2008-01-16 01:26:24 +01:00
class FollowUpManager ( models . Manager ) :
def private_followups ( self ) :
return self . filter ( public = False )
def public_followups ( self ) :
return self . filter ( public = True )
2007-12-27 01:29:17 +01:00
class FollowUp ( models . Model ) :
2007-12-27 02:35:34 +01:00
""" A FollowUp is a comment and/or change to a ticket. We keep a simple
title , the comment entered by the user , and the new status of a ticket
to enable easy flagging of details on the view - ticket page .
The title is automatically generated at save - time , based on what action
the user took .
Tickets that aren ' t public are never shown to or e-mailed to the submitter,
although all staff can see them .
"""
2007-12-27 01:29:17 +01:00
ticket = models . ForeignKey ( Ticket )
date = models . DateTimeField ( auto_now_add = True )
title = models . CharField ( maxlength = 200 , blank = True , null = True )
comment = models . TextField ( blank = True , null = True )
public = models . BooleanField ( blank = True , null = True )
2008-01-16 01:26:24 +01:00
user = models . ForeignKey ( User , blank = True , null = True )
2007-12-27 01:29:17 +01:00
new_status = models . IntegerField ( choices = Ticket . STATUS_CHOICES , blank = True , null = True )
2008-01-16 01:26:24 +01:00
objects = FollowUpManager ( )
2007-12-27 01:29:17 +01:00
class Meta :
ordering = [ ' date ' ]
class Admin :
pass
def __unicode__ ( self ) :
2008-01-15 01:20:00 +01:00
return u ' %s ' % self . title
2007-12-27 01:29:17 +01:00
2008-02-08 06:29:51 +01:00
def get_absolute_url ( self ) :
return " %s #followup %s " % ( self . ticket . get_absolute_url ( ) , self . id )
2008-01-10 01:28:45 +01:00
def save ( self ) :
t = self . ticket
t . modified = datetime . now ( )
t . save ( )
super ( FollowUp , self ) . save ( )
2007-12-27 01:29:17 +01:00
class TicketChange ( models . Model ) :
2007-12-27 02:35:34 +01:00
""" For each FollowUp, any changes to the parent ticket (eg Title, Priority,
etc ) are tracked here for display purposes .
"""
2007-12-27 01:29:17 +01:00
followup = models . ForeignKey ( FollowUp , edit_inline = models . TABULAR )
field = models . CharField ( maxlength = 100 , core = True )
old_value = models . TextField ( blank = True , null = True , core = True )
new_value = models . TextField ( blank = True , null = True , core = True )
2007-12-27 02:35:34 +01:00
def __unicode__ ( self ) :
2008-01-15 01:20:00 +01:00
str = u ' %s ' % field
2007-12-27 02:35:34 +01:00
if not new_value :
str + = ' removed '
elif not old_value :
str + = ' set to %s ' % new_value
else :
str + = ' changed from " %s " to " %s " ' % ( old_value , new_value )
return str
2008-01-07 21:22:13 +01:00
2008-02-08 01:19:58 +01:00
class DynamicFileField ( models . FileField ) :
"""
Allows model instance to specify upload_to dynamically .
Model class should have a method like :
def get_upload_to ( self , attname ) :
return ' path/to/ %d ' % self . parent . id
Based on : http : / / scottbarnham . com / blog / 2007 / 07 / 31 / uploading - images - to - a - dynamic - path - with - django /
"""
def contribute_to_class ( self , cls , name ) :
""" Hook up events so we can access the instance. """
super ( DynamicFileField , self ) . contribute_to_class ( cls , name )
dispatcher . connect ( self . _post_init , models . signals . post_init , sender = cls )
def _post_init ( self , instance = None ) :
""" Get dynamic upload_to value from the model instance. """
if hasattr ( instance , ' get_upload_to ' ) :
self . upload_to = instance . get_upload_to ( self . attname )
def db_type ( self ) :
""" Required by Django for ORM. """
return ' varchar(100) '
class Attachment ( models . Model ) :
2008-01-15 05:00:19 +01:00
followup = models . ForeignKey ( FollowUp , edit_inline = models . TABULAR )
2008-02-08 01:19:58 +01:00
file = DynamicFileField ( upload_to = ' helpdesk/attachments ' , core = True )
2008-01-15 05:00:19 +01:00
filename = models . CharField ( maxlength = 100 )
mime_type = models . CharField ( maxlength = 30 )
size = models . IntegerField ( help_text = ' Size of this file in bytes ' )
2008-02-08 01:19:58 +01:00
def get_upload_to ( self , field_attname ) :
""" Get upload_to path specific to this item """
return ' helpdesk/attachments/ %s / %s ' % ( self . followup . ticket . ticket_for_url , self . followup . id )
2008-01-15 05:00:19 +01:00
def __unicode__ ( self ) :
return u ' %s ' % self . filename
class Meta :
2008-02-08 01:19:58 +01:00
ordering = [ ' filename ' , ]
2008-01-11 02:00:01 +01:00
class PreSetReply ( models . Model ) :
2008-01-15 01:20:00 +01:00
""" We can allow the admin to define a number of pre-set replies, used to
simplify the sending of updates and resolutions . These are basically Django
templates with a limited context - however if yo uwanted to get crafy it would
be easy to write a reply that displays ALL updates in hierarchical order etc
with use of for loops over { { ticket . followup_set . all } } and friends .
When replying to a ticket , the user can select any reply set for the current
queue , and the body text is fetched via AJAX . """
queues = models . ManyToManyField ( Queue , blank = True , null = True , help_text = ' Leave blank to allow this reply to be used for all queues, or select those queues you wish to limit this reply to. ' )
name = models . CharField ( max_length = 100 , help_text = ' Only used to assist users with selecting a reply - not shown to the user. ' )
2008-01-11 02:00:01 +01:00
body = models . TextField ( help_text = ' Context available: {{ ticket }} - ticket object (eg {{ ticket.title }}); {{ queue }} - The queue; and {{ user }} - the current user. ' )
class Admin :
2008-01-15 01:20:00 +01:00
list_display = ( ' name ' , )
2008-01-11 02:00:01 +01:00
class Meta :
ordering = [ ' name ' , ]
2008-01-15 01:20:00 +01:00
def __unicode__ ( self ) :
return u ' %s ' % self . name
2008-01-22 06:54:22 +01:00
class EscalationExclusion ( models . Model ) :
2008-02-06 05:36:07 +01:00
queues = models . ManyToManyField ( Queue , blank = True , null = True , help_text = ' Leave blank for this exclusion to be applied to all queues, or select those queues you wish to exclude with this entry. ' )
2008-01-22 06:54:22 +01:00
name = models . CharField ( maxlength = 100 )
date = models . DateField ( help_text = ' Date on which escalation should not happen ' )
class Admin :
pass
def __unicode__ ( self ) :
return u ' %s ' % self . name
2008-02-08 01:19:58 +01:00