-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add form for submitting tfed tickets (#525)
- Loading branch information
Showing
15 changed files
with
233 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
RT | ||
== | ||
|
||
This module leverages RT's REST API to create new interfaces for LNL's ticketing system. | ||
|
||
Views | ||
----- | ||
.. automodule:: rt.views | ||
:members: | ||
:undoc-members: | ||
|
||
----- | ||
|
||
Forms | ||
----- | ||
.. automodule:: rt.forms | ||
:members: | ||
:undoc-members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class RtConfig(AppConfig): | ||
name = 'rt' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django import forms | ||
from multiupload.fields import MultiFileField | ||
|
||
|
||
class TicketSubmissionForm(forms.Form): | ||
subject = forms.CharField(max_length=100) | ||
description = forms.CharField(widget=forms.Textarea, label="Please describe your problem or request in detail.") | ||
attachments = MultiFileField(max_file_size=1024 * 1024 * 20, required=False) # 20 MB limit | ||
|
||
class Meta: | ||
layout = [ | ||
("Field", "subject"), | ||
("Field", "description"), | ||
("Field", "attachments") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from data.tests.util import ViewTestCase | ||
from django.shortcuts import reverse | ||
from django.conf import settings | ||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
|
||
|
||
class RTAPITests(ViewTestCase): | ||
def test_ticket_form(self): | ||
# Check that form loads ok | ||
self.assertOk(self.client.get(reverse("support:new-ticket"))) | ||
|
||
# Test response with invalid data | ||
invalid_data = { | ||
"subject": "Website broken", | ||
"description": "", | ||
"attachments": [], | ||
"save": "Submit" | ||
} | ||
self.assertOk(self.client.post(reverse("support:new-ticket"), invalid_data)) | ||
|
||
# Check response with valid data | ||
attachment = SimpleUploadedFile('test.txt', b'Contents of a file') | ||
valid_data = { | ||
"subject": "Website broken", | ||
"description": "I tried using this website to file my taxes but it isn't working for some reason", | ||
"attachments": [attachment], | ||
"save": "Submit" | ||
} | ||
|
||
# We do not want to submit new tickets if testing is triggered in prod | ||
if settings.RT_TOKEN in ['', None]: | ||
self.assertRedirects(self.client.post(reverse("support:new-ticket"), valid_data), reverse("home")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.conf.urls import include, url | ||
from . import views | ||
|
||
|
||
app_name = "RT" | ||
|
||
urlpatterns = [ | ||
url(r'^tickets/', include([ | ||
url(r'^new/$', views.new_ticket, name="new-ticket"), | ||
])), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import requests | ||
import filetype | ||
from django.shortcuts import render, reverse | ||
from django.http import HttpResponseRedirect | ||
from django.conf import settings | ||
from django.contrib import messages | ||
from django.contrib.auth.decorators import login_required | ||
from base64 import b64encode | ||
from . import forms | ||
|
||
|
||
@login_required | ||
def new_ticket(request): | ||
""" Form for submitting an RT ticket to the Database queue """ | ||
context = {} | ||
if request.method == "POST": | ||
form = forms.TicketSubmissionForm(request.POST) | ||
if form.is_valid(): | ||
subject = request.POST['subject'] | ||
description = request.POST['description'] + "\n\n-----" | ||
description += "\nVersion: %s" % settings.GIT_RELEASE[:7] | ||
description += "\nSubmitted from: %s" % request.META.get('REMOTE_ADDR') | ||
description += "\nDevice Info: %s" % request.META.get('HTTP_USER_AGENT') | ||
attachments = request.FILES.getlist('attachments') | ||
resp = create_ticket("Database", request.user.email, subject, description, attachments=attachments) | ||
if resp.get('id', None): | ||
messages.success(request, "Your ticket has been submitted. Thank you!") | ||
else: | ||
messages.add_message( | ||
request, messages.WARNING, | ||
'Failed to open ticket: %s. Please contact the Webmaster.' % resp.get('message') | ||
) | ||
return HttpResponseRedirect(reverse("home")) | ||
else: | ||
form = forms.TicketSubmissionForm() | ||
context['form'] = form | ||
return render(request, 'helpdesk_form.html', context) | ||
|
||
|
||
# API Methods | ||
def api_request(method, endpoint, data=None): | ||
""" | ||
Send an API request to the RT server | ||
:param method: `GET`, `POST`, `PUT`, or `DELETE` | ||
:param endpoint: RT endpoint | ||
:param data: JSON data (if applicable) | ||
:return: Response | ||
""" | ||
host = 'https://lnl-rt.wpi.edu/rt/REST/2.0/' | ||
headers = {"Content-Type": "application/json", "Authorization": "token " + settings.RT_TOKEN} | ||
if method.lower() == 'get': | ||
response = requests.get(host + endpoint, headers=headers) | ||
if response.status_code != 500: | ||
return response.json() | ||
return {"message": "An unknown error occurred"} | ||
elif method.lower() == 'post': | ||
if data: | ||
response = requests.post(host + endpoint, json=data, headers=headers) | ||
if response.status_code != 500: | ||
return response.json() | ||
return {"message": "An unknown error occurred"} | ||
return {"message": "Bad request"} | ||
|
||
|
||
def create_ticket(queue, reporter, subject, content, html=False, cc=None, attachments=None): | ||
""" | ||
Create a new ticket in RT | ||
:param queue: The ticket queue to send the ticket to | ||
:param reporter: Email address of the user submitting the ticket | ||
:param subject: Brief subject line for quick reference | ||
:param content: The contents of the ticket | ||
:param html: If true, will set ContentType to text/html instead of text/plain | ||
:param cc: List of additional email addresses to include in the ticket [Optional] | ||
:param attachments: A list of attachments to include [Optional] | ||
:return: API response | ||
""" | ||
endpoint = 'ticket' | ||
payload = { | ||
"Subject": subject, | ||
"Queue": queue, | ||
"Requestor": reporter, | ||
"Content": content, | ||
"ContentType": "text/plain" | ||
} | ||
if cc: | ||
payload["Cc"] = ','.join(cc) | ||
if html: | ||
payload["ContentType"] = "text/html" | ||
if attachments: | ||
files = [] | ||
for attachment in attachments: | ||
ft = filetype.guess(attachment) | ||
if ft: | ||
mime_type = ft.mime | ||
else: | ||
mime_type = "application/octet-stream" | ||
attachment.seek(0) | ||
file_contents = b64encode(attachment.read()).decode('utf-8') | ||
files.append({"FileName": attachment.name, "FileType": mime_type, "FileContent": file_contents}) | ||
payload["Attachments"] = files | ||
return api_request('POST', endpoint, payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{% extends 'base_semanticui.html' %} | ||
{% load i18n %} | ||
{% load semanticui %} | ||
|
||
{% block content %} | ||
<div class="ui middle aligned centered grid container"> | ||
<div class="twelve wide column"> | ||
<h3 class="ui {% if inverted %}inverted {% endif %}dividing header">Submit a Ticket</h3> | ||
<form class="ui {% if inverted %}inverted {% endif %}form" action="" method="POST" id="form" enctype="multipart/form-data"> | ||
{% csrf_token %} | ||
{{ form.management_form }} | ||
{% render_form form %} | ||
<br> | ||
<button class="ui animated {% if inverted %}inverted {% else %}secondary {% endif %}button" type="submit" value="{% trans "submit" %}"> | ||
<div class="visible content">Submit</div> | ||
<div class="hidden content"><i class="check icon"></i></div> | ||
</button> | ||
</form> | ||
</div> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters