From 7564c5739b385c129fc9dd844296dde3bb3a23f5 Mon Sep 17 00:00:00 2001 From: bbe Date: Sat, 31 Oct 2020 18:24:22 +0100 Subject: [PATCH 1/3] Add a clean method on TicketCC model in order to be sure not to add a user who doesn't have an email address. --- helpdesk/models.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/helpdesk/models.py b/helpdesk/models.py index 95ab7601..d6355779 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -10,7 +10,7 @@ models.py - Model (and hence database) definitions. This is the core of the from django.contrib.auth.models import Permission from django.contrib.auth import get_user_model from django.contrib.contenttypes.models import ContentType -from django.core.exceptions import ObjectDoesNotExist +from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.db import models from django.conf import settings from django.utils import timezone @@ -1666,6 +1666,10 @@ class TicketCC(models.Model): def __str__(self): return '%s for %s' % (self.display, self.ticket.title) + def clean(self): + if self.user and not self.user.email: + raise ValidationError('User has no email address') + class CustomFieldManager(models.Manager): From 1788f2fb84bcea0c1696e95222fb166fc9e4d836 Mon Sep 17 00:00:00 2001 From: bbe Date: Sat, 31 Oct 2020 18:33:41 +0100 Subject: [PATCH 2/3] Update ticketCC views and also reformat HTML files along with some improvements in form errors. --- .../templates/helpdesk/ticket_cc_add.html | 150 +++++++++++------- .../templates/helpdesk/ticket_cc_list.html | 126 +++++++++------ helpdesk/views/staff.py | 33 ++-- 3 files changed, 187 insertions(+), 122 deletions(-) diff --git a/helpdesk/templates/helpdesk/ticket_cc_add.html b/helpdesk/templates/helpdesk/ticket_cc_add.html index 07ee9f0f..b3bffd17 100644 --- a/helpdesk/templates/helpdesk/ticket_cc_add.html +++ b/helpdesk/templates/helpdesk/ticket_cc_add.html @@ -1,72 +1,104 @@ -{% extends "helpdesk/base.html" %}{% load i18n %} +{% extends "helpdesk/base.html" %} + +{% load i18n %} {% block helpdesk_title %}{% trans "Add Ticket CC" %}{% endblock %} {% block helpdesk_breadcrumb %} - - - - + + + + {% endblock %} {% block helpdesk_body %} -

{% trans 'Add Ticket CC' %}

+

{% trans 'Add Ticket CC' %}

-
-
- - {% trans 'To automatically send an email to a user or e-mail address when this ticket is updated, select the user or enter an e-mail address below.' %} -
-
- - +
+
+ + {% trans 'To automatically send an email to a user or e-mail address when this ticket is updated, select the user or enter an e-mail address below.' %} +
+
+ + - -
-
-

{% trans 'Add Email' %}

-
-
-
{% for field in form_email %} -
-
{{ field }}
- {% if field.errors %}
{{ field.errors }}
{% endif %} - {% if field.help_text %}
{{ field.help_text }}
{% endif %} - {% endfor %}
-
- - {% csrf_token %}
-
-
-

{% trans 'Add User' %}

-
-
-
{% for field in form_user %} -
-
{{ field }}
- {% if field.errors %}
{{ field.errors }}
{% endif %} - {% if field.help_text %}
{{ field.help_text }}
{% endif %} - {% endfor %}
-
- - {% csrf_token %}
+ {% if form.errors %} + {% include 'helpdesk/include/alert_form_errors.html' %} +
    {{ form.non_field_errors }}
+ {% endif %} + + +
+
+

{% trans 'Add Email' %}

+
+ {% csrf_token %} +
+
+ {% for field in form_email %} +
{{ field.label_tag }}
+
{{ field }}
+ {% if field.name == 'email' and form.errors.email %} +
{{ form.errors.email }}
+ {% endif %} + {% if field.help_text %} +
{{ field.help_text }}
+ {% endif %} + {% endfor %} +
+
+ + + {% trans "Cancel" %} + +
+
+
+

{% trans 'Add User' %}

+
+ {% csrf_token %} +
+
+ {% for field in form_user %} +
{{ field.label_tag }}
+
{{ field }}
+ {% if field.name == 'user' and form.errors.user %} +
{{ form.errors.user }}
+ {% endif %} + {% if field.help_text %} +
{{ field.help_text }}
+ {% endif %} + {% endfor %} +
+
+ + + {% trans "Cancel" %} + +
+
+
- -
- - - + {% endblock %} diff --git a/helpdesk/templates/helpdesk/ticket_cc_list.html b/helpdesk/templates/helpdesk/ticket_cc_list.html index c186e08e..e959eb5a 100644 --- a/helpdesk/templates/helpdesk/ticket_cc_list.html +++ b/helpdesk/templates/helpdesk/ticket_cc_list.html @@ -1,65 +1,91 @@ -{% extends "helpdesk/base.html" %}{% load i18n %} +{% extends "helpdesk/base.html" %} + +{% load i18n %} {% block helpdesk_title %}{% trans "Ticket CC Settings" %}{% endblock %} {% block helpdesk_breadcrumb %} - - - + + + {% endblock %} {% block helpdesk_body %}{% blocktrans with ticket.title as ticket_title and ticket.id as ticket_id %} -

Ticket CC Settings

+

Ticket CC Settings

-

The following people will receive an e-mail whenever {{ ticket_title }} is updated. Some people can also view or edit the ticket via the public ticket views.

+

The following people will receive an e-mail whenever {{ ticket_title }} is updated. Some people can also view or edit the ticket via the public ticket views.

-

You can add a new recipient to the list or delete any of the items below as required.

{% endblocktrans %} +

You can add a new recipient to the list or delete any of the items below as required.

{% endblocktrans %} -
-
-
-
- {% trans "Ticket CC List" %} -
- -
- -
- - - - - - - - - - - {% for person in copies_to %} - - - - - - - {% endfor %} - -
{% trans "E-Mail Address or Helpdesk User" %}{% trans "View?" %}{% trans "Update?" %}{% trans "Delete" %}
{{ person.display }}{{ person.can_view }}{{ person.can_update }}
-
- -
- -
- +
+
+
+
+ {% trans "Ticket CC List" %}
- + +
+ +
+ + + + + + + + + + + {% for person in copies_to %} + + + + + + + {% endfor %} + +
{% trans "E-Mail Address or Helpdesk User" %}{% trans "View?" %}{% trans "Update?" %}{% trans "Delete" %}
{{ person.display }} + {% if person.can_view %} + + {% else %} + + {% endif %} + + {% if person.can_update %} + + {% else %} + + {% endif %} + + + + +
+
+ +
+
- + +
+ +
+ -

+

+ + + +

{% endblock %} diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 9fa1b869..d24c109e 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -1508,21 +1508,27 @@ def ticket_cc_add(request, ticket_id): ticket = get_object_or_404(Ticket, id=ticket_id) ticket_perm_check(request, ticket) + form = None if request.method == 'POST': form = TicketCCForm(request.POST) if form.is_valid(): - ticketcc = form.save(commit=False) - ticketcc.ticket = ticket - ticketcc.save() - return HttpResponseRedirect(reverse('helpdesk:ticket_cc', - kwargs={'ticket_id': ticket.id})) - else: - form_email = TicketCCEmailForm() - form_user = TicketCCUserForm() + user = form.cleaned_data.get('user') + email = form.cleaned_data.get('email') + if user and ticket.ticketcc_set.filter(user=user).exists(): + form.add_error('user', _('Impossible to add twice the same user')) + elif email and ticket.ticketcc_set.filter(email=email).exists(): + form.add_error('email', _('Impossible to add twice the same email address')) + else: + ticketcc = form.save(commit=False) + ticketcc.ticket = ticket + ticketcc.save() + return HttpResponseRedirect(reverse('helpdesk:ticket_cc', kwargs={'ticket_id': ticket.id})) + return render(request, 'helpdesk/ticket_cc_add.html', { 'ticket': ticket, - 'form_email': form_email, - 'form_user': form_user, + 'form': form, + 'form_email': TicketCCEmailForm(), + 'form_user': TicketCCUserForm(), }) @@ -1531,13 +1537,14 @@ ticket_cc_add = staff_member_required(ticket_cc_add) @helpdesk_staff_member_required def ticket_cc_del(request, ticket_id, cc_id): + ticket = get_object_or_404(Ticket, id=ticket_id) cc = get_object_or_404(TicketCC, ticket__id=ticket_id, id=cc_id) if request.method == 'POST': cc.delete() - return HttpResponseRedirect(reverse('helpdesk:ticket_cc', - kwargs={'ticket_id': cc.ticket.id})) - return render(request, 'helpdesk/ticket_cc_del.html', {'cc': cc}) + return HttpResponseRedirect(reverse('helpdesk:ticket_cc', kwargs={'ticket_id': cc.ticket.id})) + + return render(request, 'helpdesk/ticket_cc_del.html', {'ticket': ticket, 'cc': cc}) ticket_cc_del = staff_member_required(ticket_cc_del) From 4c0b9b073b4e426df644aa1aee3c084bad49011e Mon Sep 17 00:00:00 2001 From: bbe Date: Sat, 31 Oct 2020 18:41:31 +0100 Subject: [PATCH 3/3] revert blocktrans indenting --- helpdesk/templates/helpdesk/ticket_cc_list.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/helpdesk/templates/helpdesk/ticket_cc_list.html b/helpdesk/templates/helpdesk/ticket_cc_list.html index e959eb5a..d9685250 100644 --- a/helpdesk/templates/helpdesk/ticket_cc_list.html +++ b/helpdesk/templates/helpdesk/ticket_cc_list.html @@ -15,11 +15,11 @@ {% endblock %} {% block helpdesk_body %}{% blocktrans with ticket.title as ticket_title and ticket.id as ticket_id %} -

Ticket CC Settings

+

Ticket CC Settings

-

The following people will receive an e-mail whenever {{ ticket_title }} is updated. Some people can also view or edit the ticket via the public ticket views.

+

The following people will receive an e-mail whenever {{ ticket_title }} is updated. Some people can also view or edit the ticket via the public ticket views.

-

You can add a new recipient to the list or delete any of the items below as required.

{% endblocktrans %} +

You can add a new recipient to the list or delete any of the items below as required.

{% endblocktrans %}