From e128cc0d5c260ab0dad44b73f3b8c8483509167c Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Thu, 23 Nov 2023 20:37:45 +0100 Subject: [PATCH] =?UTF-8?q?view=5Fticket=20=E2=86=92=20class=20based=20vie?= =?UTF-8?q?w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helpdesk/urls.py | 2 +- helpdesk/views/public.py | 101 ++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 51 deletions(-) diff --git a/helpdesk/urls.py b/helpdesk/urls.py index eb538cfd..fbd8e0ce 100644 --- a/helpdesk/urls.py +++ b/helpdesk/urls.py @@ -165,7 +165,7 @@ urlpatterns += [ public.SuccessIframeView.as_view(), name="success_iframe", ), - path("view/", public.view_ticket, name="public_view"), + path("view/", public.ViewTicket.as_view(), name="public_view"), path("change_language/", public.change_language, name="public_change_language"), ] diff --git a/helpdesk/views/public.py b/helpdesk/views/public.py index c2d15b6d..5968d6a0 100644 --- a/helpdesk/views/public.py +++ b/helpdesk/views/public.py @@ -180,59 +180,60 @@ def search_for_ticket(request, error_message=None): "Public viewing of tickets without a secret key is forbidden.") -@protect_view -def view_ticket(request): - ticket_req = request.GET.get('ticket', None) - email = request.GET.get('email', None) - key = request.GET.get('key', '') - - 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: - ticket = Ticket.objects.get( - id=ticket_id, submitter_email__iexact=email) - else: - 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.update_ticket import update_ticket - update_ticket( - request.user, - ticket, - public = True, - comment = _('Submitter accepted resolution and closed ticket'), - new_status = Ticket.CLOSED_STATUS, - ) - return HttpResponseRedirect(ticket.ticket_url) +class ViewTicket(TemplateView): + template_name = 'helpdesk/public_view_ticket.html' - # redirect user back to this ticket if possible. - redirect_url = '' - if helpdesk_settings.HELPDESK_NAVIGATION_ENABLED: - redirect_url = reverse('helpdesk:view', args=[ticket_id]) + def get(self, request, *args, **kwargs): + ticket_req = request.GET.get('ticket', None) + email = request.GET.get('email', None) + key = request.GET.get('key', '') - return render(request, 'helpdesk/public_view_ticket.html', { - 'key': key, - 'mail': email, - 'ticket': ticket, - 'helpdesk_settings': helpdesk_settings, - 'next': redirect_url, - }) + 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.')) + + try: + queue, ticket_id = Ticket.queue_and_id_from_query(ticket_req) + if hasattr(settings, 'HELPDESK_VIEW_A_TICKET_PUBLIC') and settings.HELPDESK_VIEW_A_TICKET_PUBLIC: + ticket = Ticket.objects.get(id=ticket_id, submitter_email__iexact=email) + else: + 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 'close' in request.GET and ticket.status == Ticket.RESOLVED_STATUS: + from helpdesk.update_ticket import update_ticket + update_ticket( + request.user, + ticket, + public=True, + comment=_('Submitter accepted resolution and closed ticket'), + new_status=Ticket.CLOSED_STATUS, + ) + return HttpResponseRedirect(ticket.ticket_url) + + # Prepare context for rendering + context = { + 'key': key, + 'mail': email, + 'ticket': ticket, + 'helpdesk_settings': helpdesk_settings, + 'next': self.get_next_url(ticket_id) + } + return self.render_to_response(context) + + def get_next_url(self, ticket_id): + redirect_url = '' + if is_helpdesk_staff(self.request.user): + redirect_url = reverse('helpdesk:view', args=[ticket_id]) + if 'close' in self.request.GET: + redirect_url += '?close' + elif helpdesk_settings.HELPDESK_NAVIGATION_ENABLED: + redirect_url = reverse('helpdesk:view', args=[ticket_id]) + return redirect_url def change_language(request):