2008-05-21 23:16:44 +02:00
|
|
|
"""
|
2011-01-26 00:08:41 +01:00
|
|
|
django-helpdesk - A Django powered ticket tracker for small enterprise.
|
2008-05-21 23:16:44 +02:00
|
|
|
|
|
|
|
(c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details.
|
|
|
|
|
|
|
|
views/public.py - All public facing views, eg non-staff (no authentication
|
|
|
|
required) views.
|
|
|
|
"""
|
2018-09-08 20:36:35 +02:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
|
2019-12-06 15:05:57 +01:00
|
|
|
from django.urls import reverse
|
2018-01-09 09:55:27 +01:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
2016-10-21 17:14:12 +02:00
|
|
|
from django.shortcuts import render
|
2016-11-21 02:16:14 +01:00
|
|
|
from django.utils.http import urlquote
|
2008-05-21 23:16:44 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-01-09 09:55:27 +01:00
|
|
|
from django.conf import settings
|
2018-09-08 20:36:35 +02:00
|
|
|
from django.views.generic.base import TemplateView
|
2018-09-07 19:05:16 +02:00
|
|
|
from django.views.generic.edit import FormView
|
2008-05-21 23:16:44 +02:00
|
|
|
|
2011-06-09 17:24:33 +02:00
|
|
|
from helpdesk import settings as helpdesk_settings
|
2017-10-30 08:17:40 +01:00
|
|
|
from helpdesk.decorators import protect_view, is_helpdesk_staff
|
2018-09-07 19:05:16 +02:00
|
|
|
import helpdesk.views.staff as staff
|
2008-05-21 23:16:44 +02:00
|
|
|
from helpdesk.forms import PublicTicketForm
|
2016-10-21 17:14:12 +02:00
|
|
|
from helpdesk.lib import text_is_spam
|
2020-01-07 13:33:06 +01:00
|
|
|
from helpdesk.models import CustomField, Ticket, Queue, UserSettings, KBCategory, KBItem
|
2008-05-21 23:16:44 +02:00
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2018-09-07 19:05:16 +02:00
|
|
|
def create_ticket(request, *args, **kwargs):
|
|
|
|
if is_helpdesk_staff(request.user):
|
|
|
|
return staff.CreateTicketView.as_view()(request, *args, **kwargs)
|
|
|
|
else:
|
|
|
|
return CreateTicketView.as_view()(request, *args, **kwargs)
|
2011-11-19 09:34:07 +01:00
|
|
|
|
2018-09-07 19:05:16 +02:00
|
|
|
|
2019-12-12 15:22:12 +01:00
|
|
|
class BaseCreateTicketView(FormView):
|
2018-09-07 19:05:16 +02:00
|
|
|
form_class = PublicTicketForm
|
|
|
|
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
request = self.request
|
|
|
|
if not request.user.is_authenticated and helpdesk_settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT:
|
|
|
|
return HttpResponseRedirect(reverse('login'))
|
|
|
|
|
|
|
|
if is_helpdesk_staff(request.user) or \
|
|
|
|
(request.user.is_authenticated and
|
|
|
|
helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE):
|
|
|
|
try:
|
2018-10-05 14:54:22 +02:00
|
|
|
if request.user.usersettings_helpdesk.login_view_ticketlist:
|
2018-09-07 19:05:16 +02:00
|
|
|
return HttpResponseRedirect(reverse('helpdesk:list'))
|
|
|
|
else:
|
|
|
|
return HttpResponseRedirect(reverse('helpdesk:dashboard'))
|
|
|
|
except UserSettings.DoesNotExist:
|
2015-12-22 10:10:00 +01:00
|
|
|
return HttpResponseRedirect(reverse('helpdesk:dashboard'))
|
2018-09-07 19:05:16 +02:00
|
|
|
return super().dispatch(*args, **kwargs)
|
|
|
|
|
2018-09-12 15:28:54 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['kb_categories'] = KBCategory.objects.all()
|
|
|
|
return context
|
2018-09-07 19:05:16 +02:00
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
request = self.request
|
|
|
|
initial_data = {}
|
2008-10-07 08:52:52 +02:00
|
|
|
try:
|
|
|
|
queue = Queue.objects.get(slug=request.GET.get('queue', None))
|
|
|
|
except Queue.DoesNotExist:
|
|
|
|
queue = None
|
2018-01-09 09:55:27 +01:00
|
|
|
|
|
|
|
# add pre-defined data for public ticket
|
|
|
|
if hasattr(settings, 'HELPDESK_PUBLIC_TICKET_QUEUE'):
|
|
|
|
# get the requested queue; return an error if queue not found
|
|
|
|
try:
|
|
|
|
queue = Queue.objects.get(slug=settings.HELPDESK_PUBLIC_TICKET_QUEUE)
|
|
|
|
except Queue.DoesNotExist:
|
|
|
|
return HttpResponse(status=500)
|
|
|
|
if hasattr(settings, 'HELPDESK_PUBLIC_TICKET_PRIORITY'):
|
|
|
|
initial_data['priority'] = settings.HELPDESK_PUBLIC_TICKET_PRIORITY
|
|
|
|
if hasattr(settings, 'HELPDESK_PUBLIC_TICKET_DUE_DATE'):
|
|
|
|
initial_data['due_date'] = settings.HELPDESK_PUBLIC_TICKET_DUE_DATE
|
|
|
|
|
2008-10-07 08:52:52 +02:00
|
|
|
if queue:
|
|
|
|
initial_data['queue'] = queue.id
|
2009-08-04 14:04:13 +02:00
|
|
|
|
2017-12-28 13:23:51 +01:00
|
|
|
if request.user.is_authenticated and request.user.email:
|
2009-08-04 14:04:13 +02:00
|
|
|
initial_data['submitter_email'] = request.user.email
|
2018-12-27 16:39:31 +01:00
|
|
|
|
2020-01-07 13:33:06 +01:00
|
|
|
query_param_fields = ['submitter_email', 'title', 'body', 'queue', 'kbitem']
|
2019-12-12 17:17:17 +01:00
|
|
|
custom_fields = ["custom_%s" % f.name for f in CustomField.objects.filter(staff_only=False)]
|
|
|
|
query_param_fields += custom_fields
|
2018-12-27 16:39:31 +01:00
|
|
|
for qpf in query_param_fields:
|
|
|
|
initial_data[qpf] = request.GET.get(qpf, initial_data.get(qpf, ""))
|
2018-09-07 19:05:16 +02:00
|
|
|
return initial_data
|
|
|
|
|
2019-12-06 15:44:20 +01:00
|
|
|
def get_form_kwargs(self, *args, **kwargs):
|
|
|
|
kwargs = super().get_form_kwargs(*args, **kwargs)
|
|
|
|
kwargs['hidden_fields'] = self.request.GET.get('_hide_fields_', '').split(',')
|
2020-01-06 14:25:37 +01:00
|
|
|
kwargs['readonly_fields'] = self.request.GET.get('_readonly_fields_', '').split(',')
|
2020-01-07 13:33:06 +01:00
|
|
|
kbitem = self.request.GET.get('kbitem', None)
|
|
|
|
if kbitem:
|
|
|
|
try:
|
|
|
|
kwargs['kbcategory'] = KBItem.objects.get(pk=int(kbitem))
|
|
|
|
except (ValueError, KBItem.DoesNotExist):
|
|
|
|
pass
|
2019-12-06 15:44:20 +01:00
|
|
|
return kwargs
|
|
|
|
|
2018-09-07 19:05:16 +02:00
|
|
|
def form_valid(self, form):
|
|
|
|
request = self.request
|
|
|
|
if text_is_spam(form.cleaned_data['body'], request):
|
|
|
|
# This submission is spam. Let's not save it.
|
|
|
|
return render(request, template_name='helpdesk/public_spam.html')
|
|
|
|
else:
|
|
|
|
ticket = form.save()
|
|
|
|
try:
|
2018-09-08 20:36:35 +02:00
|
|
|
return HttpResponseRedirect('%s?ticket=%s&email=%s&key=%s' % (
|
2018-09-07 19:05:16 +02:00
|
|
|
reverse('helpdesk:public_view'),
|
|
|
|
ticket.ticket_for_url,
|
2018-09-08 20:36:35 +02:00
|
|
|
urlquote(ticket.submitter_email),
|
|
|
|
ticket.secret_key)
|
2018-09-07 19:05:16 +02:00
|
|
|
)
|
|
|
|
except ValueError:
|
|
|
|
# if someone enters a non-int string for the ticket
|
|
|
|
return HttpResponseRedirect(reverse('helpdesk:home'))
|
2009-08-04 14:04:13 +02:00
|
|
|
|
2018-09-07 19:05:16 +02:00
|
|
|
def get_success_url(self):
|
|
|
|
request = self.request
|
2008-05-21 23:16:44 +02:00
|
|
|
|
2012-03-01 23:28:54 +01:00
|
|
|
|
2019-12-12 15:22:12 +01:00
|
|
|
class CreateTicketIframeView(BaseCreateTicketView):
|
|
|
|
template_name = 'helpdesk/public_create_ticket_iframe.html'
|
|
|
|
|
|
|
|
|
|
|
|
class CreateTicketView(BaseCreateTicketView):
|
|
|
|
template_name = 'helpdesk/public_create_ticket.html'
|
|
|
|
|
|
|
|
|
2018-09-07 19:05:16 +02:00
|
|
|
class Homepage(CreateTicketView):
|
|
|
|
template_name = 'helpdesk/public_homepage.html'
|
2008-05-21 23:16:44 +02:00
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2018-09-08 20:36:35 +02:00
|
|
|
def search_for_ticket(request, error_message=None):
|
|
|
|
if hasattr(settings, 'HELPDESK_VIEW_A_TICKET_PUBLIC') and settings.HELPDESK_VIEW_A_TICKET_PUBLIC:
|
|
|
|
email = request.GET.get('email', None)
|
|
|
|
return render(request, 'helpdesk/public_view_form.html', {
|
|
|
|
'ticket': False,
|
|
|
|
'email': email,
|
|
|
|
'error_message': error_message,
|
|
|
|
'helpdesk_settings': helpdesk_settings,
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
raise PermissionDenied("Public viewing of tickets without a secret key is forbidden.")
|
|
|
|
|
|
|
|
|
2017-10-04 03:42:26 +02:00
|
|
|
@protect_view
|
2008-05-21 23:16:44 +02:00
|
|
|
def view_ticket(request):
|
2017-04-13 12:36:38 +02:00
|
|
|
ticket_req = request.GET.get('ticket', None)
|
|
|
|
email = request.GET.get('email', None)
|
2018-09-08 20:36:35 +02:00
|
|
|
key = request.GET.get('key', '')
|
2008-05-21 23:16:44 +02:00
|
|
|
|
2018-09-08 20:36:35 +02:00
|
|
|
if not (ticket_req and email):
|
|
|
|
if ticket_req is None and email is None:
|
|
|
|
return search_for_ticket(request)
|
|
|
|
else:
|
|
|
|
return search_for_ticket(request, _('Missing ticket ID or e-mail address. Please try again.'))
|
|
|
|
|
|
|
|
queue, ticket_id = Ticket.queue_and_id_from_query(ticket_req)
|
|
|
|
try:
|
|
|
|
if hasattr(settings, 'HELPDESK_VIEW_A_TICKET_PUBLIC') and settings.HELPDESK_VIEW_A_TICKET_PUBLIC:
|
2016-06-24 03:46:37 +02:00
|
|
|
ticket = Ticket.objects.get(id=ticket_id, submitter_email__iexact=email)
|
2016-10-21 17:14:12 +02:00
|
|
|
else:
|
2018-09-08 20:36:35 +02:00
|
|
|
ticket = Ticket.objects.get(id=ticket_id, submitter_email__iexact=email, secret_key__iexact=key)
|
|
|
|
except (ObjectDoesNotExist, ValueError):
|
|
|
|
return search_for_ticket(request, _('Invalid ticket ID or e-mail address. Please try again.'))
|
|
|
|
|
|
|
|
if is_helpdesk_staff(request.user):
|
|
|
|
redirect_url = reverse('helpdesk:view', args=[ticket_id])
|
|
|
|
if 'close' in request.GET:
|
|
|
|
redirect_url += '?close'
|
|
|
|
return HttpResponseRedirect(redirect_url)
|
|
|
|
|
|
|
|
if 'close' in request.GET and ticket.status == Ticket.RESOLVED_STATUS:
|
|
|
|
from helpdesk.views.staff import update_ticket
|
|
|
|
# Trick the update_ticket() view into thinking it's being called with
|
|
|
|
# a valid POST.
|
|
|
|
request.POST = {
|
|
|
|
'new_status': Ticket.CLOSED_STATUS,
|
|
|
|
'public': 1,
|
|
|
|
'title': ticket.title,
|
|
|
|
'comment': _('Submitter accepted resolution and closed ticket'),
|
|
|
|
}
|
|
|
|
if ticket.assigned_to:
|
|
|
|
request.POST['owner'] = ticket.assigned_to.id
|
|
|
|
request.GET = {}
|
|
|
|
|
|
|
|
return update_ticket(request, ticket_id, public=True)
|
|
|
|
|
|
|
|
# redirect user back to this ticket if possible.
|
|
|
|
redirect_url = ''
|
|
|
|
if helpdesk_settings.HELPDESK_NAVIGATION_ENABLED:
|
|
|
|
redirect_url = reverse('helpdesk:view', args=[ticket_id])
|
2017-04-13 12:30:29 +02:00
|
|
|
|
2018-09-08 20:36:35 +02:00
|
|
|
return render(request, 'helpdesk/public_view_ticket.html', {
|
2019-05-22 22:36:46 +02:00
|
|
|
'key': key,
|
|
|
|
'mail': email,
|
2018-09-08 20:36:35 +02:00
|
|
|
'ticket': ticket,
|
2017-04-13 12:30:29 +02:00
|
|
|
'helpdesk_settings': helpdesk_settings,
|
2018-09-08 20:36:35 +02:00
|
|
|
'next': redirect_url,
|
2017-04-13 12:30:29 +02:00
|
|
|
})
|
2016-10-21 17:14:12 +02:00
|
|
|
|
2008-08-19 10:50:38 +02:00
|
|
|
|
2011-11-27 09:26:56 +01:00
|
|
|
def change_language(request):
|
2011-11-27 09:43:51 +01:00
|
|
|
return_to = ''
|
2015-04-28 01:13:54 +02:00
|
|
|
if 'return_to' in request.GET:
|
2011-11-27 09:43:51 +01:00
|
|
|
return_to = request.GET['return_to']
|
|
|
|
|
2016-10-21 17:14:12 +02:00
|
|
|
return render(request, 'helpdesk/public_change_language.html', {'next': return_to})
|