8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-10 20:06:25 +00:00
re2o/tickets/models.py

112 lines
3.5 KiB
Python
Raw Normal View History

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