Skip to content

Commit

Permalink
Merge pull request #949 from koriaf/dev-custom-form-class
Browse files Browse the repository at this point in the history
feat(forms): Ability to provide custom public ticket form
  • Loading branch information
gwasser committed Mar 4, 2021
2 parents 78a027b + 1713a86 commit 406207f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
12 changes: 8 additions & 4 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ These changes are visible throughout django-helpdesk
- **HELPDESK_EMAIL_FALLBACK_LOCALE** Fallback locale for templated emails when queue locale not found

**Default:** ``HELPDESK_EMAIL_FALLBACK_LOCALE = "en"``

- **HELPDESK_MAX_EMAIL_ATTACHMENT_SIZE** Maximum size, in bytes, of file attachments that will be sent via email

**Default:** ``HELPDESK_MAX_EMAIL_ATTACHMENT_SIZE = 512000``

- **QUEUE_EMAIL_BOX_UPDATE_ONLY** Only process mail with a valid tracking ID; all other mail will be ignored instead of creating a new ticket.

**Default:** ``QUEUE_EMAIL_BOX_UPDATE_ONLY = False``

- **HELPDESK_ANON_ACCESS_RAISES_404** If True, redirects user to a 404 page when attempting to reach ticket pages while not logged in, rather than redirecting to a login screen.

**Default:** ``HELPDESK_ANON_ACCESS_RAISES_404 = False``

Options shown on public pages
-----------------------------

Expand All @@ -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``

- **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
-----------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions helpdesk/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
# show 'submit a ticket' section on public page?
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 #
Expand Down
14 changes: 12 additions & 2 deletions helpdesk/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
required) views.
"""
import logging
from importlib import import_module

from django.core.exceptions import (
ObjectDoesNotExist, PermissionDenied, ImproperlyConfigured,
Expand All @@ -26,7 +27,6 @@
from helpdesk.decorators import protect_view, is_helpdesk_staff
import helpdesk.views.staff as staff
import helpdesk.views.abstract_views as abstract_views
from helpdesk.forms import PublicTicketForm
from helpdesk.lib import text_is_spam
from helpdesk.models import CustomField, Ticket, Queue, UserSettings, KBCategory, KBItem
from helpdesk.user import huser_from_request
Expand All @@ -42,7 +42,17 @@ def create_ticket(request, *args, **kwargs):


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):
request = self.request
Expand Down

0 comments on commit 406207f

Please sign in to comment.