Add ability to hide fields in public ticket submission form using kwargs

This commit is contained in:
Timothy Hobbs 2019-12-06 15:44:20 +01:00
parent 3b5a7fe49a
commit 24b8e45f66
3 changed files with 16 additions and 39 deletions

View File

@ -337,34 +337,22 @@ class PublicTicketForm(AbstractTicketForm):
help_text=_('We will e-mail you when your ticket is updated.'), 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 Add any (non-staff) custom fields that are defined to the form
""" """
super(PublicTicketForm, self).__init__(*args, **kwargs) super(PublicTicketForm, self).__init__(*args, **kwargs)
if hasattr(settings, 'HELPDESK_PUBLIC_TICKET_QUEUE'): field_hide_table = {
del self.fields['queue'] 'queue': 'HELPDESK_PUBLIC_TICKET_QUEUE',
else: 'priority': 'HELPDESK_PUBLIC_TICKET_PRIORITY',
self.fields['queue'].choices = [ 'due_date': 'HELPDESK_PUBLIC_TICKET_DUE_DATE',
('', '--------') }
] + [ for (field, setting) in field_hide_table.items():
(q.id, q.title) for q in Queue.objects.filter(allow_public_submission=True) if hasattr(settings, setting) or field in hidden_fields:
] self.fields[field].widget = forms.HiddenInput()
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()
def _get_queue(self): self.fields['queue'].choices = [('', '--------')] + [
if getattr(settings, 'HELPDESK_PUBLIC_TICKET_QUEUE', None): (q.id, q.title) for q in Queue.objects.filter(allow_public_submission=True)]
# 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']))
def save(self): def save(self):
""" """

View File

@ -113,21 +113,6 @@ class AttachmentUnitTests(TestCase):
) )
self.assertEqual(filename, self.file_attrs['filename']) 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) @mock.patch('helpdesk.lib.FollowUpAttachment', autospec=True)
def test_autofill(self, mock_att_save, mock_queue_save, mock_ticket_save, mock_follow_up_save): def test_autofill(self, mock_att_save, mock_queue_save, mock_ticket_save, mock_follow_up_save):
""" check utf-8 data is parsed correctly """ """ 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.filename, self.file_attrs['filename'])
self.assertEqual(obj.size, len(self.file_attrs['content'])) self.assertEqual(obj.size, len(self.file_attrs['content']))
self.assertEqual(obj.mime_type, "text/plain") 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): def test_kbi_attachment(self, mock_att_save, mock_queue_save, mock_ticket_save):
""" check utf-8 data is parsed correctly """ """ check utf-8 data is parsed correctly """

View File

@ -88,6 +88,11 @@ class CreateTicketView(FormView):
initial_data[qpf] = request.GET.get(qpf, initial_data.get(qpf, "")) initial_data[qpf] = request.GET.get(qpf, initial_data.get(qpf, ""))
return initial_data 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): def form_valid(self, form):
request = self.request request = self.request
if text_is_spam(form.cleaned_data['body'], request): if text_is_spam(form.cleaned_data['body'], request):