django-orchestra-test/orchestra/apps/issues/models.py

198 lines
7.0 KiB
Python
Raw Normal View History

2014-07-29 20:10:37 +00:00
from django.conf import settings as djsettings
2014-05-08 16:59:35 +00:00
from django.db import models
from django.utils.translation import ugettext_lazy as _
from orchestra.apps.contacts import settings as contacts_settings
2014-09-04 15:55:43 +00:00
from orchestra.apps.contacts.models import Contact
2015-03-29 16:10:07 +00:00
from orchestra.core.translations import ModelTranslation
2014-05-08 16:59:35 +00:00
from orchestra.models.fields import MultiSelectField
from orchestra.utils import send_email_template
from . import settings
class Queue(models.Model):
name = models.CharField(_("name"), max_length=128, unique=True)
2015-03-29 16:10:07 +00:00
verbose_name = models.CharField(_("verbose_name"), max_length=128, blank=True)
2014-05-08 16:59:35 +00:00
default = models.BooleanField(_("default"), default=False)
notify = MultiSelectField(_("notify"), max_length=256, blank=True,
2014-09-04 15:55:43 +00:00
choices=Contact.EMAIL_USAGES,
2014-05-08 16:59:35 +00:00
default=contacts_settings.CONTACTS_DEFAULT_EMAIL_USAGES,
help_text=_("Contacts to notify by email"))
def __unicode__(self):
2015-03-29 16:10:07 +00:00
return self.verbose_name or self.name
2014-05-08 16:59:35 +00:00
def save(self, *args, **kwargs):
""" mark as default queue if needed """
existing_default = Queue.objects.filter(default=True)
if self.default:
existing_default.update(default=False)
elif not existing_default:
self.default = True
super(Queue, self).save(*args, **kwargs)
class Ticket(models.Model):
HIGH = 'HIGH'
MEDIUM = 'MEDIUM'
LOW = 'LOW'
PRIORITIES = (
(HIGH, 'High'),
(MEDIUM, 'Medium'),
(LOW, 'Low'),
)
NEW = 'NEW'
IN_PROGRESS = 'IN_PROGRESS'
RESOLVED = 'RESOLVED'
FEEDBACK = 'FEEDBACK'
REJECTED = 'REJECTED'
CLOSED = 'CLOSED'
STATES = (
(NEW, 'New'),
(IN_PROGRESS, 'In Progress'),
(RESOLVED, 'Resolved'),
(FEEDBACK, 'Feedback'),
(REJECTED, 'Rejected'),
(CLOSED, 'Closed'),
)
2014-07-29 20:10:37 +00:00
creator = models.ForeignKey(djsettings.AUTH_USER_MODEL, verbose_name=_("created by"),
2014-05-13 13:46:40 +00:00
related_name='tickets_created', null=True)
creator_name = models.CharField(_("creator name"), max_length=256, blank=True)
2014-07-29 20:10:37 +00:00
owner = models.ForeignKey(djsettings.AUTH_USER_MODEL, null=True, blank=True,
2014-05-08 16:59:35 +00:00
related_name='tickets_owned', verbose_name=_("assigned to"))
queue = models.ForeignKey(Queue, related_name='tickets', null=True, blank=True)
subject = models.CharField(_("subject"), max_length=256)
description = models.TextField(_("description"))
priority = models.CharField(_("priority"), max_length=32, choices=PRIORITIES,
default=MEDIUM)
state = models.CharField(_("state"), max_length=32, choices=STATES, default=NEW)
2014-09-26 15:05:20 +00:00
created_at = models.DateTimeField(_("created"), auto_now_add=True)
updated_at = models.DateTimeField(_("modified"), auto_now=True)
2014-05-08 16:59:35 +00:00
cc = models.TextField("CC", help_text=_("emails to send a carbon copy to"),
blank=True)
class Meta:
2014-09-26 15:05:20 +00:00
ordering = ['-updated_at']
2014-05-08 16:59:35 +00:00
def __unicode__(self):
return unicode(self.pk)
def get_notification_emails(self):
""" Get emails of the users related to the ticket """
emails = list(settings.ISSUES_SUPPORT_EMAILS)
emails.append(self.creator.email)
if self.owner:
emails.append(self.owner.email)
for contact in self.creator.account.contacts.all():
2014-07-23 18:28:40 +00:00
if self.queue and set(contact.email_usage).union(set(self.queue.notify)):
2014-05-08 16:59:35 +00:00
emails.append(contact.email)
for message in self.messages.distinct('author'):
emails.append(message.author.email)
return set(emails + self.get_cc_emails())
def notify(self, message=None, content=None):
""" Send an email to ticket stakeholders notifying an state update """
emails = self.get_notification_emails()
template = 'issues/ticket_notification.mail'
html_template = 'issues/ticket_notification_html.mail'
context = {
'ticket': self,
'ticket_message': message
}
send_email_template(template, context, emails, html=html_template)
def save(self, *args, **kwargs):
""" notify stakeholders of new ticket """
new_issue = not self.pk
2014-05-13 13:46:40 +00:00
if not self.creator_name and self.creator:
self.creator_name = self.creator.get_full_name()
2014-05-08 16:59:35 +00:00
super(Ticket, self).save(*args, **kwargs)
if new_issue:
# PK should be available for rendering the template
self.notify()
def is_involved_by(self, user):
""" returns whether user has participated or is referenced on the ticket
as owner or member of the group
"""
return Ticket.objects.filter(pk=self.pk).involved_by(user).exists()
def get_cc_emails(self):
return self.cc.split(',') if self.cc else []
def mark_as_read_by(self, user):
2014-05-13 13:46:40 +00:00
self.trackers.get_or_create(user=user)
2014-05-08 16:59:35 +00:00
def mark_as_unread_by(self, user):
2014-05-13 13:46:40 +00:00
self.trackers.filter(user=user).delete()
2014-05-08 16:59:35 +00:00
def mark_as_unread(self):
2014-05-13 13:46:40 +00:00
self.trackers.all().delete()
2014-05-08 16:59:35 +00:00
def is_read_by(self, user):
2014-05-13 13:46:40 +00:00
return self.trackers.filter(user=user).exists()
2014-05-08 16:59:35 +00:00
def reject(self):
self.state = Ticket.REJECTED
self.save(update_fields=['state'])
2014-05-08 16:59:35 +00:00
def resolve(self):
self.state = Ticket.RESOLVED
self.save(update_fields=['state'])
2014-05-08 16:59:35 +00:00
def close(self):
self.state = Ticket.CLOSED
self.save(update_fields=['state'])
2014-05-08 16:59:35 +00:00
def take(self, user):
self.owner = user
self.save(update_fields=['state'])
2014-05-08 16:59:35 +00:00
class Message(models.Model):
ticket = models.ForeignKey('issues.Ticket', verbose_name=_("ticket"),
related_name='messages')
2014-07-29 20:10:37 +00:00
author = models.ForeignKey(djsettings.AUTH_USER_MODEL, verbose_name=_("author"),
2014-05-08 16:59:35 +00:00
related_name='ticket_messages')
2014-05-13 13:46:40 +00:00
author_name = models.CharField(_("author name"), max_length=256, blank=True)
2014-05-08 16:59:35 +00:00
content = models.TextField(_("content"))
created_on = models.DateTimeField(_("created on"), auto_now_add=True)
2014-05-13 13:46:40 +00:00
class Meta:
2014-09-24 20:09:41 +00:00
get_latest_by = 'id'
2014-05-13 13:46:40 +00:00
2014-05-08 16:59:35 +00:00
def __unicode__(self):
return u"#%i" % self.id
def save(self, *args, **kwargs):
""" notify stakeholders of ticket update """
if not self.pk:
self.ticket.mark_as_unread()
2014-05-13 13:46:40 +00:00
self.ticket.mark_as_read_by(self.author)
2014-05-08 16:59:35 +00:00
self.ticket.notify(message=self)
2014-05-13 13:46:40 +00:00
self.author_name = self.author.get_full_name()
2014-05-08 16:59:35 +00:00
super(Message, self).save(*args, **kwargs)
@property
def number(self):
2014-05-08 16:59:35 +00:00
return self.ticket.messages.filter(id__lte=self.id).count()
class TicketTracker(models.Model):
""" Keeps track of user read tickets """
ticket = models.ForeignKey(Ticket, verbose_name=_("ticket"),
related_name='trackers')
2014-07-29 20:10:37 +00:00
user = models.ForeignKey(djsettings.AUTH_USER_MODEL, verbose_name=_("user"),
2014-05-08 16:59:35 +00:00
related_name='ticket_trackers')
class Meta:
2014-09-24 20:09:41 +00:00
unique_together = (
('ticket', 'user'),
)
2015-03-29 16:10:07 +00:00
ModelTranslation.register(Queue, ('verbose_name',))