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
|
2019-09-01 21:25:41 +00:00
|
|
|
from django.template import 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 13:06:56 +00:00
|
|
|
from re2o.mixins import AclMixin
|
|
|
|
|
2019-08-11 09:03:47 +00:00
|
|
|
from preferences.models import GeneralOption
|
|
|
|
|
2019-07-07 17:09:56 +00:00
|
|
|
import users.models
|
|
|
|
|
2019-08-14 15:09:46 +00:00
|
|
|
from .preferences.models import Preferences
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2019-08-11 13:06:56 +00:00
|
|
|
class Ticket(AclMixin, models.Model):
|
2019-08-14 17:39:19 +00:00
|
|
|
"""Model of a ticket"""
|
2019-07-07 17:09:56 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"users.User",
|
2019-07-07 17:09:56 +00:00
|
|
|
on_delete=models.CASCADE,
|
2019-07-12 11:41:17 +00:00
|
|
|
related_name="tickets",
|
|
|
|
blank=True,
|
2019-11-04 16:55:03 +00:00
|
|
|
null=True,
|
|
|
|
)
|
2019-07-07 17:09:56 +00:00
|
|
|
title = models.CharField(
|
2019-11-20 00:49:36 +00:00
|
|
|
max_length=255, help_text=_("Title of the ticket."), blank=False, null=False
|
2019-11-04 16:55:03 +00:00
|
|
|
)
|
2019-07-10 08:28:16 +00:00
|
|
|
description = models.TextField(
|
2019-07-07 17:09:56 +00:00
|
|
|
max_length=3000,
|
2019-11-20 00:49:36 +00:00
|
|
|
help_text=_("Description of the ticket."),
|
2019-07-07 17:09:56 +00:00
|
|
|
blank=False,
|
2019-11-04 16:55:03 +00:00
|
|
|
null=False,
|
|
|
|
)
|
2019-07-07 17:09:56 +00:00
|
|
|
date = models.DateTimeField(auto_now_add=True)
|
2019-07-12 11:41:17 +00:00
|
|
|
email = models.EmailField(
|
2019-11-20 00:49:36 +00:00
|
|
|
help_text=_("An email address to get back to you."), max_length=100, null=True
|
2019-11-04 16:55:03 +00:00
|
|
|
)
|
2019-07-07 17:09:56 +00:00
|
|
|
solved = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_tickets", _("Can view a ticket object")),)
|
2019-11-20 00:49:36 +00:00
|
|
|
verbose_name = _("ticket")
|
|
|
|
verbose_name_plural = _("tickets")
|
2019-07-11 08:27:58 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-08-14 15:09:46 +00:00
|
|
|
if self.user:
|
2019-11-20 00:49:36 +00:00
|
|
|
return _("Ticket from %(name)s. Date: %(date)s.").format(name=self.user.surname, date=self.date)
|
2019-08-14 15:09:46 +00:00
|
|
|
else:
|
2019-11-20 00:49:36 +00:00
|
|
|
return _("Anonymous ticket. Date: %s.") % (self.date)
|
2019-08-05 14:38:53 +00:00
|
|
|
|
|
|
|
def publish_mail(self):
|
2019-08-14 15:09:46 +00:00
|
|
|
site_url = GeneralOption.objects.first().main_site_url
|
2019-08-10 09:45:13 +00:00
|
|
|
to_addr = Preferences.objects.first().publish_address
|
2019-11-04 16:55:03 +00:00
|
|
|
context = {"ticket": self, "site_url": site_url}
|
2019-08-20 20:37:59 +00:00
|
|
|
|
|
|
|
lang = Preferences.objects.first().mail_language
|
2019-11-04 16:55:03 +00:00
|
|
|
if lang == 0:
|
2019-11-20 00:49:36 +00:00
|
|
|
obj = "Nouveau ticket ouvert"
|
2019-11-04 16:55:03 +00:00
|
|
|
template = loader.get_template("tickets/publication_mail_fr")
|
2019-08-20 20:37:59 +00:00
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
obj = "New ticket opened"
|
|
|
|
template = loader.get_template("tickets/publication_mail_en")
|
2019-08-05 14:38:53 +00:00
|
|
|
send_mail(
|
2019-08-20 20:37:59 +00:00
|
|
|
obj,
|
2019-08-10 09:45:13 +00:00
|
|
|
template.render(context),
|
2019-11-04 16:55:03 +00:00
|
|
|
GeneralOption.get_cached_value("email_from"),
|
2019-08-10 09:45:13 +00:00
|
|
|
[to_addr],
|
2019-11-04 16:55:03 +00:00
|
|
|
fail_silently=False,
|
|
|
|
)
|
2019-09-06 12:14:21 +00:00
|
|
|
|
2019-08-11 13:06:56 +00:00
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
2019-08-14 17:39:19 +00:00
|
|
|
""" Check that the user has the right to view the ticket
|
|
|
|
or that it is the author"""
|
2019-11-04 16:55:03 +00:00
|
|
|
if (
|
2020-03-29 00:00:00 +00:00
|
|
|
not user_request.has_perm("tickets.view_tickets")
|
2019-11-04 16:55:03 +00:00
|
|
|
and self.user != user_request
|
|
|
|
):
|
2019-09-06 12:14:21 +00:00
|
|
|
return (
|
|
|
|
False,
|
|
|
|
_("You don't have the right to view other tickets than yours."),
|
2020-03-29 00:00:00 +00:00
|
|
|
("tickets.view_tickets",),
|
2019-09-06 12:14:21 +00:00
|
|
|
)
|
2019-08-11 13:06:56 +00:00
|
|
|
else:
|
2019-09-06 12:14:21 +00:00
|
|
|
return True, None, None
|
|
|
|
|
2019-08-11 13:06:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_view_all(user_request, *_args, **_kwargs):
|
2019-08-14 17:39:19 +00:00
|
|
|
""" Check that the user has access to the list of all tickets"""
|
2019-11-04 16:55:03 +00:00
|
|
|
can = user_request.has_perm("tickets.view_tickets")
|
|
|
|
return (
|
2019-09-09 12:40:40 +00:00
|
|
|
can,
|
2019-11-04 16:55:03 +00:00
|
|
|
_("You don't have the right to view the list of tickets.")
|
|
|
|
if not can
|
|
|
|
else None,
|
|
|
|
("tickets.view_tickets",),
|
2019-08-11 13:06:56 +00:00
|
|
|
)
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
def can_create(user_request, *_args, **_kwargs):
|
2019-08-14 17:39:19 +00:00
|
|
|
""" Authorise all users to open tickets """
|
2019-09-06 12:14:21 +00:00
|
|
|
return True, None, None
|
2019-08-10 09:45:13 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2019-08-10 09:45:13 +00:00
|
|
|
@receiver(post_save, sender=Ticket)
|
|
|
|
def ticket_post_save(**kwargs):
|
2019-08-14 17:39:19 +00:00
|
|
|
""" Send the mail to publish the new ticket """
|
2019-11-04 16:55:03 +00:00
|
|
|
if kwargs["created"]:
|
2019-08-10 22:41:33 +00:00
|
|
|
if Preferences.objects.first().publish_address:
|
2019-11-04 16:55:03 +00:00
|
|
|
ticket = kwargs["instance"]
|
2019-08-10 22:41:33 +00:00
|
|
|
ticket.publish_mail()
|