Merge pull request #949 from koriaf/dev-custom-form-class

feat(forms): Ability to provide custom public ticket form
This commit is contained in:
Garret Wassermann 2021-03-04 11:37:12 -05:00 committed by GitHub
commit 406207ff74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

View File

@ -107,6 +107,10 @@ These options only change display of items on public-facing pages, not staff pag
**Default:** ``HELPDESK_SUBMIT_A_TICKET_PUBLIC = True`` **Default:** ``HELPDESK_SUBMIT_A_TICKET_PUBLIC = True``
- **HELPDESK_PUBLIC_TICKET_FORM_CLASS** Define custom form class to show on public pages for anon users. You can use it for adding custom fields and validation, captcha and so on.
**Default:** ``HELPDESK_PUBLIC_TICKET_FORM_CLASS = "helpdesk.forms.PublicTicketForm"``
Options for public ticket submission form Options for public ticket submission form
----------------------------------------- -----------------------------------------

View File

@ -79,6 +79,13 @@ HELPDESK_VIEW_A_TICKET_PUBLIC = getattr(settings, 'HELPDESK_VIEW_A_TICKET_PUBLIC
# show 'submit a ticket' section on public page? # show 'submit a ticket' section on public page?
HELPDESK_SUBMIT_A_TICKET_PUBLIC = getattr(settings, 'HELPDESK_SUBMIT_A_TICKET_PUBLIC', True) HELPDESK_SUBMIT_A_TICKET_PUBLIC = getattr(settings, 'HELPDESK_SUBMIT_A_TICKET_PUBLIC', True)
# change that to custom class to have extra fields or validation (like captcha)
HELPDESK_PUBLIC_TICKET_FORM_CLASS = getattr(
settings,
"HELPDESK_PUBLIC_TICKET_FORM_CLASS",
"helpdesk.forms.PublicTicketForm"
)
################################### ###################################
# options for update_ticket views # # options for update_ticket views #

View File

@ -7,6 +7,7 @@ views/public.py - All public facing views, eg non-staff (no authentication
required) views. required) views.
""" """
import logging import logging
from importlib import import_module
from django.core.exceptions import ( from django.core.exceptions import (
ObjectDoesNotExist, PermissionDenied, ImproperlyConfigured, ObjectDoesNotExist, PermissionDenied, ImproperlyConfigured,
@ -26,7 +27,6 @@ from helpdesk import settings as helpdesk_settings
from helpdesk.decorators import protect_view, is_helpdesk_staff from helpdesk.decorators import protect_view, is_helpdesk_staff
import helpdesk.views.staff as staff import helpdesk.views.staff as staff
import helpdesk.views.abstract_views as abstract_views import helpdesk.views.abstract_views as abstract_views
from helpdesk.forms import PublicTicketForm
from helpdesk.lib import text_is_spam from helpdesk.lib import text_is_spam
from helpdesk.models import CustomField, Ticket, Queue, UserSettings, KBCategory, KBItem from helpdesk.models import CustomField, Ticket, Queue, UserSettings, KBCategory, KBItem
from helpdesk.user import huser_from_request from helpdesk.user import huser_from_request
@ -42,7 +42,17 @@ def create_ticket(request, *args, **kwargs):
class BaseCreateTicketView(abstract_views.AbstractCreateTicketMixin, FormView): class BaseCreateTicketView(abstract_views.AbstractCreateTicketMixin, FormView):
form_class = PublicTicketForm
def get_form_class(self):
try:
the_module, the_form_class = helpdesk_settings.HELPDESK_PUBLIC_TICKET_FORM_CLASS.rsplit(".", 1)
the_module = import_module(the_module)
the_form_class = getattr(the_module, the_form_class)
except Exception as e:
raise ImproperlyConfigured(
f"Invalid custom form class {helpdesk_settings.HELPDESK_PUBLIC_TICKET_FORM_CLASS}"
) from e
return the_form_class
def dispatch(self, *args, **kwargs): def dispatch(self, *args, **kwargs):
request = self.request request = self.request