From 4e018db5a6754a0cbeae1a61b35cb7a54eee32aa Mon Sep 17 00:00:00 2001 From: Alex Seeholzer Date: Fri, 13 Jan 2017 15:14:23 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Added=20unique=20related=5Fname=20=E2=80=9C?= =?UTF-8?q?usersetttings=5Fhelpdesk=E2=80=9D=20for=20the=20one-to-one=20re?= =?UTF-8?q?lation=20to=20User=20in=20UserSettings.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helpdesk/forms.py | 2 +- helpdesk/models.py | 6 ++++-- helpdesk/views/public.py | 2 +- helpdesk/views/staff.py | 10 +++++----- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index 013ab270..7ab2024f 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -253,7 +253,7 @@ class AbstractTicketForm(CustomFieldMixin, forms.Form): if ticket.assigned_to and \ ticket.assigned_to != user and \ - ticket.assigned_to.usersettings.settings.get('email_on_ticket_assign', False) and \ + ticket.assigned_to.usersettings_helpdesk.settings.get('email_on_ticket_assign', False) and \ ticket.assigned_to.email and \ ticket.assigned_to.email not in messages_sent_to: send_templated_mail( diff --git a/helpdesk/models.py b/helpdesk/models.py index 4dc28a36..273d4cef 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -1084,10 +1084,12 @@ class UserSettings(models.Model): as notification preferences and other things that should probably be configurable. - We should always refer to user.usersettings.settings['setting_name']. + We should always refer to user.usersettings_helpdesk.settings['setting_name']. """ - user = models.OneToOneField(settings.AUTH_USER_MODEL) + user = models.OneToOneField( + settings.AUTH_USER_MODEL, + related_name="usersettings_helpdesk") settings_pickled = models.TextField( _('Settings Dictionary'), diff --git a/helpdesk/views/public.py b/helpdesk/views/public.py index f15ecb2f..b66900f5 100644 --- a/helpdesk/views/public.py +++ b/helpdesk/views/public.py @@ -27,7 +27,7 @@ def homepage(request): (request.user.is_authenticated() and helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE): try: - if request.user.usersettings.settings.get('login_view_ticketlist', False): + if request.user.usersettings_helpdesk.settings.get('login_view_ticketlist', False): return HttpResponseRedirect(reverse('helpdesk:list')) else: return HttpResponseRedirect(reverse('helpdesk:dashboard')) diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index a482dea3..546aec25 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -578,10 +578,10 @@ def update_ticket(request, ticket_id, public=False): if (not reassigned or (reassigned and - ticket.assigned_to.usersettings.settings.get( + ticket.assigned_to.usersettings_helpdesk.settings.get( 'email_on_ticket_assign', False))) or \ (not reassigned and - ticket.assigned_to.usersettings.settings.get( + ticket.assigned_to.usersettings_helpdesk.settings.get( 'email_on_ticket_change', False)): send_templated_mail( template_staff, @@ -914,7 +914,7 @@ def ticket_list(request): return render(request, 'helpdesk/ticket_list.html', dict( context, tickets=ticket_qs, - default_tickets_per_page=request.user.usersettings.settings.get('tickets_per_page') or 25, + default_tickets_per_page=request.user.usersettings_helpdesk.settings.get('tickets_per_page') or 25, user_choices=User.objects.filter(is_active=True, is_staff=True), queue_choices=user_queues, status_choices=Ticket.STATUS_CHOICES, @@ -965,7 +965,7 @@ def create_ticket(request): return HttpResponseRedirect(reverse('helpdesk:dashboard')) else: initial_data = {} - if request.user.usersettings.settings.get('use_email_as_submitter', False) and request.user.email: + if request.user.usersettings_helpdesk.settings.get('use_email_as_submitter', False) and request.user.email: initial_data['submitter_email'] = request.user.email if 'queue' in request.GET: initial_data['queue'] = request.GET['queue'] @@ -1312,7 +1312,7 @@ delete_saved_query = staff_member_required(delete_saved_query) def user_settings(request): - s = request.user.usersettings + s = request.user.usersettings_helpdesk if request.POST: form = UserSettingsForm(request.POST) if form.is_valid(): From 645f19684853abf53c411de8ebfeb9586c82c3e9 Mon Sep 17 00:00:00 2001 From: Alex Seeholzer Date: Fri, 13 Jan 2017 17:37:10 +0100 Subject: [PATCH 2/3] Added tests covering the usersettings_helpdesk occurrences --- helpdesk/tests/test_ticket_actions.py | 130 ++++++++++++++++++++++++++ helpdesk/tests/test_usersettings.py | 30 ++++++ helpdesk/views/staff.py | 5 +- 3 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 helpdesk/tests/test_ticket_actions.py create mode 100644 helpdesk/tests/test_usersettings.py diff --git a/helpdesk/tests/test_ticket_actions.py b/helpdesk/tests/test_ticket_actions.py new file mode 100644 index 00000000..e8e879a0 --- /dev/null +++ b/helpdesk/tests/test_ticket_actions.py @@ -0,0 +1,130 @@ +from django.contrib.auth import get_user_model +from django.core import mail +from django.core.urlresolvers import reverse +from django.test import TestCase +from django.test.client import Client +from helpdesk.models import CustomField, Queue, Ticket + +try: # python 3 + from urllib.parse import urlparse +except ImportError: # python 2 + from urlparse import urlparse + + +class TicketActionsTestCase(TestCase): + fixtures = ['emailtemplate.json'] + + def setUp(self): + self.queue_public = Queue.objects.create( + title='Queue 1', + slug='q1', + allow_public_submission=True, + new_ticket_cc='new.public@example.com', + updated_ticket_cc='update.public@example.com') + + self.ticket_data = { + 'title': 'Test Ticket', + 'description': 'Some Test Ticket', + } + + self.client = Client() + + def loginUser(self, is_staff=True): + User = get_user_model() + self.user = User.objects.create( + username='User_1', + is_staff=is_staff, + ) + self.user.set_password('pass') + self.user.save() + self.client.login(username='User_1', password='pass') + + def test_delete_ticket_staff(self): + # make staff user + self.loginUser() + + """Tests whether staff can delete tickets""" + ticket_data = dict(queue=self.queue_public, **self.ticket_data) + ticket = Ticket.objects.create(**ticket_data) + ticket_id = ticket.id + + response = self.client.get(reverse('helpdesk:delete', kwargs={'ticket_id': ticket_id}), follow=True) + self.assertContains(response, 'Are you sure you want to delete this ticket') + + response = self.client.post(reverse('helpdesk:delete', kwargs={'ticket_id': ticket_id}), follow=True) + first_redirect = response.redirect_chain[0] + first_redirect_url = first_redirect[0] + + # Ensure we landed on the "View" page. + # Django 1.9 compatible way of testing this + # https://docs.djangoproject.com/en/1.9/releases/1.9/#http-redirects-no-longer-forced-to-absolute-uris + urlparts = urlparse(first_redirect_url) + self.assertEqual(urlparts.path, reverse('helpdesk:home')) + + # test ticket deleted + with self.assertRaises(Ticket.DoesNotExist): + Ticket.objects.get(pk=ticket_id) + + def test_update_ticket_staff(self): + """Tests whether staff can update ticket details""" + + # make staff user + self.loginUser() + + # create second user + User = get_user_model() + self.user2 = User.objects.create( + username='User_2', + is_staff=True, + ) + + initial_data = { + 'title': 'Private ticket test', + 'queue': self.queue_public, + 'assigned_to': self.user, + 'status': Ticket.OPEN_STATUS, + } + + # create ticket + ticket = Ticket.objects.create(**initial_data) + ticket_id = ticket.id + + # assign new owner + post_data = { + 'owner': self.user2.id, + } + response = self.client.post(reverse('helpdesk:update', kwargs={'ticket_id': ticket_id}), post_data, follow=True) + self.assertContains(response, 'Changed Owner from User_1 to User_2') + + # change status with users email assigned and submitter email assigned, + # which triggers emails being sent + ticket.assigned_to = self.user2 + ticket.submitter_email = 'submitter@test.com' + ticket.save() + self.user2.email = 'user2@test.com' + self.user2.save() + self.user.email = 'user1@test.com' + self.user.save() + post_data = { + 'new_status': Ticket.CLOSED_STATUS, + 'public': True + } + + # do this also to a newly assigned user (different from logged in one) + ticket.assigned_to = self.user + response = self.client.post(reverse('helpdesk:update', kwargs={'ticket_id': ticket_id}), post_data, follow=True) + self.assertContains(response, 'Changed Status from Open to Closed') + post_data = { + 'new_status': Ticket.OPEN_STATUS, + 'owner': self.user2.id, + 'public': True + } + response = self.client.post(reverse('helpdesk:update', kwargs={'ticket_id': ticket_id}), post_data, follow=True) + self.assertContains(response, 'Changed Status from Open to Closed') + + def test_create_ticket_getform(self): + self.loginUser() + response = self.client.get(reverse('helpdesk:submit'), follow=True) + self.assertEqual(response.status_code, 200) + + # TODO this needs to be checked further \ No newline at end of file diff --git a/helpdesk/tests/test_usersettings.py b/helpdesk/tests/test_usersettings.py new file mode 100644 index 00000000..25776ce9 --- /dev/null +++ b/helpdesk/tests/test_usersettings.py @@ -0,0 +1,30 @@ +from django.contrib.auth import get_user_model +from django.core import mail +from django.core.urlresolvers import reverse +from django.test import TestCase +from django.test.client import Client +from helpdesk.models import CustomField, Queue, Ticket + +try: # python 3 + from urllib.parse import urlparse +except ImportError: # python 2 + from urlparse import urlparse + + +class TicketActionsTestCase(TestCase): + fixtures = ['emailtemplate.json'] + + def setUp(self): + User = get_user_model() + self.user = User.objects.create( + username='User_1', + is_staff=True, + ) + self.user.set_password('pass') + self.user.save() + self.client.login(username='User_1', password='pass') + + def test_get_user_settings(self): + + response = self.client.get(reverse('helpdesk:user_settings'), follow=True) + self.assertContains(response, "Use the following options") diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index 546aec25..1ed3cd09 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -337,10 +337,11 @@ def return_ticketccstring_and_show_subscribe(user, ticket): # check whether current user is a submitter or assigned to ticket assignedto_username = str(ticket.assigned_to).upper() - submitter_email = ticket.submitter_email.upper() strings_to_check = list() + if ticket.submitter_email is not None: + submitter_email = ticket.submitter_email.upper() + strings_to_check.append(submitter_email) strings_to_check.append(assignedto_username) - strings_to_check.append(submitter_email) if strings_to_check.__contains__(username) or strings_to_check.__contains__(useremail): show_subscribe = False From a2315eb0c4b0c9cca9d61cf5d706f3fb05936348 Mon Sep 17 00:00:00 2001 From: Alex Seeholzer Date: Fri, 13 Jan 2017 17:39:56 +0100 Subject: [PATCH 3/3] added pep8 fix and missing migration --- .../0014_usersettings_related_name.py | 20 +++++++++++++++++++ helpdesk/tests/test_ticket_actions.py | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 helpdesk/migrations/0014_usersettings_related_name.py diff --git a/helpdesk/migrations/0014_usersettings_related_name.py b/helpdesk/migrations/0014_usersettings_related_name.py new file mode 100644 index 00000000..133f90bc --- /dev/null +++ b/helpdesk/migrations/0014_usersettings_related_name.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0013_email_box_local_dir_and_logging'), + ] + + operations = [ + migrations.AlterField( + model_name='usersettings', + name='user', + field=models.OneToOneField(to=settings.AUTH_USER_MODEL, related_name='usersettings_helpdesk'), + ), + ] diff --git a/helpdesk/tests/test_ticket_actions.py b/helpdesk/tests/test_ticket_actions.py index e8e879a0..42fd11a8 100644 --- a/helpdesk/tests/test_ticket_actions.py +++ b/helpdesk/tests/test_ticket_actions.py @@ -127,4 +127,4 @@ class TicketActionsTestCase(TestCase): response = self.client.get(reverse('helpdesk:submit'), follow=True) self.assertEqual(response.status_code, 200) - # TODO this needs to be checked further \ No newline at end of file + # TODO this needs to be checked further