2019-07-06 08:25:24 +00:00
|
|
|
from django.db import models
|
2019-07-07 17:09:56 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-08-05 14:38:53 +00:00
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.template import Context, loader
|
2019-08-10 09:45:13 +00:00
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
2019-07-06 08:25:24 +00:00
|
|
|
|
2019-08-11 09:03:47 +00:00
|
|
|
from preferences.models import GeneralOption
|
|
|
|
|
2019-07-07 17:09:56 +00:00
|
|
|
import users.models
|
|
|
|
|
|
|
|
class Ticket(models.Model):
|
|
|
|
"""Class définissant un ticket"""
|
|
|
|
|
|
|
|
user = models.ForeignKey(
|
|
|
|
'users.User',
|
|
|
|
on_delete=models.CASCADE,
|
2019-07-12 11:41:17 +00:00
|
|
|
related_name="tickets",
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
2019-07-07 17:09:56 +00:00
|
|
|
title = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
help_text=_("Nom du ticket"),
|
|
|
|
blank=False,
|
|
|
|
null=False,)
|
2019-07-10 08:28:16 +00:00
|
|
|
description = models.TextField(
|
2019-07-07 17:09:56 +00:00
|
|
|
max_length=3000,
|
|
|
|
help_text=_("Description du ticket"),
|
|
|
|
blank=False,
|
|
|
|
null=False)
|
|
|
|
date = models.DateTimeField(auto_now_add=True)
|
2019-07-12 11:41:17 +00:00
|
|
|
email = models.EmailField(
|
|
|
|
help_text = _("Une adresse mail pour vous recontacter"),
|
|
|
|
max_length=100,
|
|
|
|
null=True)
|
2019-07-07 17:09:56 +00:00
|
|
|
assigned_staff = models.ForeignKey(
|
|
|
|
'users.User',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name="tickets_assigned",
|
2019-07-10 08:28:16 +00:00
|
|
|
blank=True,
|
2019-07-07 17:09:56 +00:00
|
|
|
null=True)
|
|
|
|
#categories = models.OneToManyFiled('Category')
|
|
|
|
solved = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Ticket")
|
|
|
|
verbose_name_plural = _("Tickets")
|
2019-07-11 08:27:58 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Ticket de {} date: {}".format(self.user.surname,self.date)
|
2019-08-05 14:38:53 +00:00
|
|
|
|
|
|
|
def publish_mail(self):
|
2019-08-10 09:45:13 +00:00
|
|
|
to_addr = Preferences.objects.first().publish_address
|
|
|
|
template = loader.get_template('tickets/publication_mail')
|
2019-08-05 14:38:53 +00:00
|
|
|
context = Context({'ticket':self})
|
|
|
|
send_mail(
|
|
|
|
'Nouvelle ouverture de ticket',
|
2019-08-10 09:45:13 +00:00
|
|
|
template.render(context),
|
2019-08-11 09:03:47 +00:00
|
|
|
GeneralOption.get_cached_value('email_from'),
|
2019-08-10 09:45:13 +00:00
|
|
|
[to_addr],
|
|
|
|
fail_silently = False)
|
2019-08-05 14:38:53 +00:00
|
|
|
|
|
|
|
class Preferences(models.Model):
|
|
|
|
""" Class cannonique définissants les préférences des tickets """
|
2019-08-10 09:45:13 +00:00
|
|
|
|
2019-08-05 14:38:53 +00:00
|
|
|
publish_address = models.EmailField(
|
|
|
|
help_text = _("Adresse mail pour annoncer les nouveau tickets (laisser vide pour ne rien annoncer)"),
|
|
|
|
max_length = 1000,
|
|
|
|
null = True)
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Préférences des tickets")
|
2019-08-10 09:45:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Ticket)
|
|
|
|
def ticket_post_save(**kwargs):
|
|
|
|
"""Envoit du mail de publication du ticket"""
|
2019-08-10 22:41:33 +00:00
|
|
|
if kwargs['created']:
|
|
|
|
if Preferences.objects.first().publish_address:
|
|
|
|
ticket = kwargs['instance']
|
|
|
|
ticket.publish_mail()
|