2020-04-22 16:55:33 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2019 Arthur Grisel-Davy
|
|
|
|
# Copyright © 2020 Gabriel Détraz
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
"""
|
|
|
|
Ticket model
|
|
|
|
"""
|
|
|
|
|
2020-05-15 23:45:53 +00:00
|
|
|
from __future__ import absolute_import
|
2020-04-22 16:55:33 +00:00
|
|
|
|
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-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
|
2020-04-23 00:42:18 +00:00
|
|
|
from django.utils.functional import cached_property
|
|
|
|
|
|
|
|
from reversion.models import Version
|
2019-07-06 08:25:24 +00:00
|
|
|
|
2019-08-11 13:06:56 +00:00
|
|
|
from re2o.mixins import AclMixin
|
2020-04-23 01:21:11 +00:00
|
|
|
from re2o.mail_utils import send_mail_object
|
2020-04-22 19:37:21 +00:00
|
|
|
from django.core.mail import EmailMessage
|
2019-08-11 13:06:56 +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
|
|
|
|
|
2020-04-22 16:55:33 +00:00
|
|
|
from .preferences.models import TicketOption
|
2019-08-14 15:09:46 +00:00
|
|
|
|
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,
|
|
|
|
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)
|
2020-04-23 00:42:18 +00:00
|
|
|
language = models.CharField(
|
|
|
|
max_length=16, help_text=_("Language of the ticket."), default="en"
|
|
|
|
)
|
2020-04-23 01:21:11 +00:00
|
|
|
request = None
|
2019-07-07 17:09:56 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-04-23 00:42:18 +00:00
|
|
|
permissions = (("view_ticket", _("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:
|
2020-05-03 00:12:04 +00:00
|
|
|
return _("Ticket from {name}. Date: {date}.").format(name=self.user.get_full_name(),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
|
|
|
|
2020-04-23 00:42:18 +00:00
|
|
|
@cached_property
|
|
|
|
def opened_by(self):
|
|
|
|
"""Return full name of this ticket opener"""
|
|
|
|
if self.user:
|
|
|
|
return self.user.get_full_name()
|
|
|
|
else:
|
|
|
|
return _("Anonymous user")
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def get_mail(self):
|
|
|
|
"""Return the mail of the owner of this ticket"""
|
|
|
|
return self.email or self.user.get_mail
|
|
|
|
|
2020-04-22 19:37:21 +00:00
|
|
|
def publish_mail(self):
|
2020-04-22 16:55:33 +00:00
|
|
|
site_url = GeneralOption.get_cached_value("main_site_url")
|
|
|
|
to_addr = TicketOption.get_cached_value("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
|
|
|
|
2020-04-23 00:42:18 +00:00
|
|
|
if self.language == "fr":
|
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")
|
2020-04-19 18:06:34 +00:00
|
|
|
|
2020-04-22 19:37:21 +00:00
|
|
|
mail_to_send = EmailMessage(
|
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],
|
2020-04-23 00:42:18 +00:00
|
|
|
reply_to=[self.get_mail],
|
2019-11-04 16:55:03 +00:00
|
|
|
)
|
2020-04-23 01:21:11 +00:00
|
|
|
send_mail_object(mail_to_send, self.request)
|
2020-04-22 19:37:21 +00:00
|
|
|
|
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-04-23 00:42:18 +00:00
|
|
|
not user_request.has_perm("tickets.view_ticket")
|
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-04-23 00:42:18 +00:00
|
|
|
("tickets.view_ticket",),
|
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"""
|
2020-04-23 00:42:18 +00:00
|
|
|
can = user_request.has_perm("tickets.view_ticket")
|
2019-11-04 16:55:03 +00:00
|
|
|
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,
|
2020-04-23 00:42:18 +00:00
|
|
|
("tickets.view_ticket",),
|
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
|
|
|
|
2020-04-23 00:42:18 +00:00
|
|
|
class CommentTicket(AclMixin, models.Model):
|
|
|
|
"""A comment of a ticket"""
|
|
|
|
date = models.DateTimeField(auto_now_add=True)
|
|
|
|
comment = models.TextField(
|
|
|
|
max_length=4095,
|
|
|
|
blank=False,
|
|
|
|
null=False,
|
|
|
|
)
|
|
|
|
parent_ticket = models.ForeignKey(
|
|
|
|
"Ticket", on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
created_by = models.ForeignKey(
|
|
|
|
"users.User",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="ticket_comment",
|
|
|
|
)
|
2020-04-23 01:21:11 +00:00
|
|
|
request = None
|
2020-04-23 00:42:18 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
permissions = (("view_commentticket", _("Can view a ticket object")),)
|
|
|
|
verbose_name = _("ticket")
|
|
|
|
verbose_name_plural = _("tickets")
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def comment_id(self):
|
|
|
|
return CommentTicket.objects.filter(parent_ticket=self.parent_ticket, pk__lt=self.pk).count() + 1
|
|
|
|
|
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
|
|
|
""" Check that the user has the right to view the ticket comment
|
|
|
|
or that it is the author"""
|
|
|
|
if (
|
|
|
|
not user_request.has_perm("tickets.view_commentticket")
|
|
|
|
and self.parent_ticket.user != user_request
|
|
|
|
):
|
|
|
|
return (
|
|
|
|
False,
|
|
|
|
_("You don't have the right to view other tickets comments than yours."),
|
|
|
|
("tickets.view_commentticket",),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return True, None, None
|
|
|
|
|
|
|
|
def can_edit(self, user_request, *_args, **_kwargs):
|
|
|
|
""" Check that the user has the right to edit the ticket comment
|
|
|
|
or that it is the author"""
|
|
|
|
if (
|
2020-04-30 20:00:17 +00:00
|
|
|
not user_request.has_perm("tickets.change_commentticket")
|
2020-04-23 00:42:18 +00:00
|
|
|
and (self.parent_ticket.user != user_request or self.parent_ticket.user != self.created_by)
|
|
|
|
):
|
|
|
|
return (
|
|
|
|
False,
|
|
|
|
_("You don't have the right to edit other tickets comments than yours."),
|
2020-04-30 20:00:17 +00:00
|
|
|
("tickets.change_commentticket",),
|
2020-04-23 00:42:18 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
return True, None, None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def can_view_all(user_request, *_args, **_kwargs):
|
|
|
|
""" Check that the user has access to the list of all tickets comments"""
|
|
|
|
can = user_request.has_perm("tickets.view_commentticket")
|
|
|
|
return (
|
|
|
|
can,
|
|
|
|
_("You don't have the right to view the list of tickets.")
|
|
|
|
if not can
|
|
|
|
else None,
|
|
|
|
("tickets.view_commentticket",),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Comment " + str(self.comment_id) + " on " + str(self.parent_ticket)
|
|
|
|
|
|
|
|
def publish_mail(self):
|
2020-04-23 01:21:11 +00:00
|
|
|
"""Send mail to user and admin after new comment"""
|
2020-04-23 00:42:18 +00:00
|
|
|
site_url = GeneralOption.get_cached_value("main_site_url")
|
|
|
|
to_addr = TicketOption.get_cached_value("publish_address")
|
|
|
|
context = {"comment": self, "site_url": site_url}
|
|
|
|
|
|
|
|
if self.parent_ticket.language == "fr":
|
|
|
|
template = loader.get_template("tickets/update_mail_fr")
|
|
|
|
else:
|
|
|
|
template = loader.get_template("tickets/update_mail_en")
|
|
|
|
obj = _("Update of your ticket")
|
|
|
|
mail_to_send = EmailMessage(
|
|
|
|
obj,
|
|
|
|
template.render(context),
|
|
|
|
GeneralOption.get_cached_value("email_from"),
|
|
|
|
[to_addr, self.parent_ticket.get_mail],
|
|
|
|
)
|
2020-04-23 01:21:11 +00:00
|
|
|
send_mail_object(mail_to_send, self.request)
|
2020-04-23 00:42:18 +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"]:
|
2020-04-22 16:55:33 +00:00
|
|
|
if TicketOption.get_cached_value("publish_address"):
|
2019-11-04 16:55:03 +00:00
|
|
|
ticket = kwargs["instance"]
|
2020-04-22 19:37:21 +00:00
|
|
|
ticket.publish_mail()
|
2020-04-23 00:42:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=CommentTicket)
|
|
|
|
def comment_post_save(**kwargs):
|
|
|
|
""" Send the mail to publish the new comment """
|
|
|
|
if kwargs["created"]:
|
|
|
|
if TicketOption.get_cached_value("publish_address"):
|
|
|
|
comment = kwargs["instance"]
|
|
|
|
comment.publish_mail()
|