From bd9a227caed4ca3a189218a680d50688cae15ff9 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Thu, 23 Apr 2020 03:21:11 +0200 Subject: [PATCH] Lerennais remarques --- preferences/views.py | 4 ++-- tickets/forms.py | 5 ++++- tickets/models.py | 8 ++++++-- tickets/preferences/views.py | 3 --- tickets/views.py | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/preferences/views.py b/preferences/views.py index 6420dad3..25e7c009 100644 --- a/preferences/views.py +++ b/preferences/views.py @@ -96,8 +96,8 @@ def edit_options_template_function(request, section, forms, models): return redirect(reverse("preferences:display-options")) options_instance, _created = model.objects.get_or_create() - can, msg, permissions = options_instance.can_edit(request.user) - if not can: + _is_allowed_to_edit, msg, permissions = options_instance.can_edit(request.user) + if not _is_allowed_to_edit: messages.error(request, acl_error_message(msg, permissions)) return redirect(reverse("index")) options = form_instance( diff --git a/tickets/forms.py b/tickets/forms.py index a9f6617d..3001de8a 100644 --- a/tickets/forms.py +++ b/tickets/forms.py @@ -42,13 +42,14 @@ class NewTicketForm(FormRevMixin, ModelForm): fields = ["title", "description", "email"] def __init__(self, *args, **kwargs): - request = kwargs.pop("request") + request = kwargs.pop("request", None) super(NewTicketForm, self).__init__(*args, **kwargs) if request.user.is_authenticated: self.fields.pop('email') self.instance.user = request.user self.fields['description'].help_text = render_to_string('tickets/help_text.html') self.instance.language = getattr(request, "LANGUAGE_CODE", "en") + self.instance.request = request class EditTicketForm(FormRevMixin, ModelForm): @@ -71,7 +72,9 @@ class CommentTicketForm(FormRevMixin, ModelForm): fields = ["comment"] def __init__(self, *args, **kwargs): + request = kwargs.pop("request", None) prefix = kwargs.pop("prefix", self.Meta.model.__name__) super(CommentTicketForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields["comment"].label = _("comment") + self.instance.request = request diff --git a/tickets/models.py b/tickets/models.py index 67bf8f34..98a006ac 100644 --- a/tickets/models.py +++ b/tickets/models.py @@ -34,6 +34,7 @@ from django.utils.functional import cached_property from reversion.models import Version from re2o.mixins import AclMixin +from re2o.mail_utils import send_mail_object from django.core.mail import EmailMessage from preferences.models import GeneralOption @@ -69,6 +70,7 @@ class Ticket(AclMixin, models.Model): language = models.CharField( max_length=16, help_text=_("Language of the ticket."), default="en" ) + request = None class Meta: permissions = (("view_ticket", _("Can view a ticket object")),) @@ -113,7 +115,7 @@ class Ticket(AclMixin, models.Model): [to_addr], reply_to=[self.get_mail], ) - mail_to_send.send(fail_silently=False) + send_mail_object(mail_to_send, self.request) def can_view(self, user_request, *_args, **_kwargs): @@ -165,6 +167,7 @@ class CommentTicket(AclMixin, models.Model): on_delete=models.CASCADE, related_name="ticket_comment", ) + request = None class Meta: permissions = (("view_commentticket", _("Can view a ticket object")),) @@ -221,6 +224,7 @@ class CommentTicket(AclMixin, models.Model): return "Comment " + str(self.comment_id) + " on " + str(self.parent_ticket) def publish_mail(self): + """Send mail to user and admin after new comment""" site_url = GeneralOption.get_cached_value("main_site_url") to_addr = TicketOption.get_cached_value("publish_address") context = {"comment": self, "site_url": site_url} @@ -236,7 +240,7 @@ class CommentTicket(AclMixin, models.Model): GeneralOption.get_cached_value("email_from"), [to_addr, self.parent_ticket.get_mail], ) - mail_to_send.send(fail_silently=False) + send_mail_object(mail_to_send, self.request) @receiver(post_save, sender=Ticket) diff --git a/tickets/preferences/views.py b/tickets/preferences/views.py index 29465b1c..70637db8 100644 --- a/tickets/preferences/views.py +++ b/tickets/preferences/views.py @@ -28,11 +28,8 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required from django.shortcuts import render, redirect from django.template.loader import render_to_string -from django.views.decorators.cache import cache_page from django.utils.translation import ugettext as _ from django.urls import reverse -from django.forms import modelformset_factory -from re2o.views import form from re2o.base import re2o_paginator diff --git a/tickets/views.py b/tickets/views.py index bda14df7..aab6aeae 100644 --- a/tickets/views.py +++ b/tickets/views.py @@ -121,7 +121,7 @@ def edit_ticket(request, ticket, ticketid): @can_view(Ticket) def add_comment(request, ticket, ticketid): """ Add a comment to a ticket""" - commentticket = CommentTicketForm(request.POST or None) + commentticket = CommentTicketForm(request.POST or None, request=request) if commentticket.is_valid(): commentticket = commentticket.save(commit=False) commentticket.parent_ticket = ticket