view_ticket → class based view

This commit is contained in:
Timothy Hobbs 2023-11-23 20:37:45 +01:00
parent a27508bba7
commit e128cc0d5c
2 changed files with 52 additions and 51 deletions

View File

@ -165,7 +165,7 @@ urlpatterns += [
public.SuccessIframeView.as_view(), public.SuccessIframeView.as_view(),
name="success_iframe", 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, path("change_language/", public.change_language,
name="public_change_language"), name="public_change_language"),
] ]

View File

@ -180,59 +180,60 @@ def search_for_ticket(request, error_message=None):
"Public viewing of tickets without a secret key is forbidden.") "Public viewing of tickets without a secret key is forbidden.")
@protect_view class ViewTicket(TemplateView):
def view_ticket(request): template_name = 'helpdesk/public_view_ticket.html'
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)
# redirect user back to this ticket if possible. def get(self, request, *args, **kwargs):
redirect_url = '' ticket_req = request.GET.get('ticket', None)
if helpdesk_settings.HELPDESK_NAVIGATION_ENABLED: email = request.GET.get('email', None)
redirect_url = reverse('helpdesk:view', args=[ticket_id]) key = request.GET.get('key', '')
return render(request, 'helpdesk/public_view_ticket.html', { if not (ticket_req and email):
'key': key, if ticket_req is None and email is None:
'mail': email, return search_for_ticket(request)
'ticket': ticket, else:
'helpdesk_settings': helpdesk_settings, return search_for_ticket(request, _('Missing ticket ID or e-mail address. Please try again.'))
'next': redirect_url,
}) 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): def change_language(request):