From 24b8e45f6634814f54215852f0cfa5bef6468638 Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Fri, 6 Dec 2019 15:44:20 +0100 Subject: [PATCH] Add ability to hide fields in public ticket submission form using kwargs --- helpdesk/forms.py | 34 ++++++++++-------------------- helpdesk/tests/test_attachments.py | 16 -------------- helpdesk/views/public.py | 5 +++++ 3 files changed, 16 insertions(+), 39 deletions(-) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index aec25d10..af7ef438 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -337,34 +337,22 @@ class PublicTicketForm(AbstractTicketForm): help_text=_('We will e-mail you when your ticket is updated.'), ) - def __init__(self, *args, **kwargs): + def __init__(self, hidden_fields=(), *args, **kwargs): """ Add any (non-staff) custom fields that are defined to the form """ super(PublicTicketForm, self).__init__(*args, **kwargs) - if hasattr(settings, 'HELPDESK_PUBLIC_TICKET_QUEUE'): - del self.fields['queue'] - else: - self.fields['queue'].choices = [ - ('', '--------') - ] + [ - (q.id, q.title) for q in Queue.objects.filter(allow_public_submission=True) - ] - if hasattr(settings, 'HELPDESK_PUBLIC_TICKET_PRIORITY'): - self.fields['priority'].widget = forms.HiddenInput() - if hasattr(settings, 'HELPDESK_PUBLIC_TICKET_DUE_DATE'): - self.fields['due_date'].widget = forms.HiddenInput() + field_hide_table = { + 'queue': 'HELPDESK_PUBLIC_TICKET_QUEUE', + 'priority': 'HELPDESK_PUBLIC_TICKET_PRIORITY', + 'due_date': 'HELPDESK_PUBLIC_TICKET_DUE_DATE', + } + for (field, setting) in field_hide_table.items(): + if hasattr(settings, setting) or field in hidden_fields: + self.fields[field].widget = forms.HiddenInput() - def _get_queue(self): - if getattr(settings, 'HELPDESK_PUBLIC_TICKET_QUEUE', None): - # force queue to be the pre-defined one - # (only for anon submissions) - return Queue.objects.filter( - slug=settings.HELPDESK_PUBLIC_TICKET_QUEUE - ).first() - else: - # get the queue user entered - return Queue.objects.get(id=int(self.cleaned_data['queue'])) + self.fields['queue'].choices = [('', '--------')] + [ + (q.id, q.title) for q in Queue.objects.filter(allow_public_submission=True)] def save(self): """ diff --git a/helpdesk/tests/test_attachments.py b/helpdesk/tests/test_attachments.py index 9b71a962..942a2353 100644 --- a/helpdesk/tests/test_attachments.py +++ b/helpdesk/tests/test_attachments.py @@ -113,21 +113,6 @@ class AttachmentUnitTests(TestCase): ) self.assertEqual(filename, self.file_attrs['filename']) -<<<<<<< HEAD - # TODO: FIXME: what's wrong with this test that we get integrity errors? - # @mock.patch('helpdesk.lib.FollowUpAttachment', autospec=True) - # def test_autofill(self, mock_att_save, mock_queue_save, mock_ticket_save, mock_follow_up_save): - # """ check utf-8 data is parsed correctly """ - # self.follow_up.pk = 100 - # self.follow_up.save() - # obj = models.FollowUpAttachment.objects.create( - # followup=self.follow_up, - # file=self.test_file - # ) - # self.assertEqual(obj.filename, self.file_attrs['filename']) - # self.assertEqual(obj.size, len(self.file_attrs['content'])) - # self.assertEqual(obj.mime_type, "text/plain") -======= @mock.patch('helpdesk.lib.FollowUpAttachment', autospec=True) def test_autofill(self, mock_att_save, mock_queue_save, mock_ticket_save, mock_follow_up_save): """ check utf-8 data is parsed correctly """ @@ -138,7 +123,6 @@ class AttachmentUnitTests(TestCase): self.assertEqual(obj.filename, self.file_attrs['filename']) self.assertEqual(obj.size, len(self.file_attrs['content'])) self.assertEqual(obj.mime_type, "text/plain") ->>>>>>> b899c97... Remove hardcoded pk from test suit def test_kbi_attachment(self, mock_att_save, mock_queue_save, mock_ticket_save): """ check utf-8 data is parsed correctly """ diff --git a/helpdesk/views/public.py b/helpdesk/views/public.py index f89de0b7..eebf9c41 100644 --- a/helpdesk/views/public.py +++ b/helpdesk/views/public.py @@ -88,6 +88,11 @@ class CreateTicketView(FormView): initial_data[qpf] = request.GET.get(qpf, initial_data.get(qpf, "")) return initial_data + def get_form_kwargs(self, *args, **kwargs): + kwargs = super().get_form_kwargs(*args, **kwargs) + kwargs['hidden_fields'] = self.request.GET.get('_hide_fields_', '').split(',') + return kwargs + def form_valid(self, form): request = self.request if text_is_spam(form.cleaned_data['body'], request):