Yes, this my code:
from django.shortcuts import render
import requests, json, logging
from blauwestadtech import utils
from blauwestadtech.logging_config import logging_configuration
# Call configuration function
logging_configuration()
# Create logger instance
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.info("Logger in "+__name__+" is initialised...")
# Create your views here.
def create_and_submit_ticket(request):
'''The create_and_submit_ticket view is designed to streamline the process of submitting support tickets. It accepts essential parameters for a ticket and processes them to generate structured ticket data.
This is then converted into a JSON format sent to the Freshdesk.
Please ensure you have a subscription level which allows you to obtain your API hey
:return ticket_data: A dictionary containing all the submitted ticket information.
'''
from . import freshdesk_api_key as fd
from legal.forms import LegalDocumentsFeedback
from django.shortcuts import redirect
from django.shortcuts import HttpResponseRedirect
import os
from django.conf import settings
API_KEY = fd.FRESHDESK_API_KEY
API_URL = fd.MY_FRESHDESK_API_URL
logger.info("Testing..")
if request.method == 'POST':
logger.info("Method Status:POST")
form = LegalDocumentsFeedback(request.POST, request.FILES)
if form.is_valid():
name = form.cleaned_data['name']
subject = form.cleaned_data['subject']
description = form.cleaned_data['description']
email = form.cleaned_data['email']
attachments = request.FILES.getlist('attachments')
ticket_data = {
"name": name,
"subject": subject,
"description": description,
"email": email,
'priority' : 1,
'status' : 2,
'group_id': 201000039106,
'responder_id': 201002411183
}
files = []
ALLOWED_EXTENSIONS = [".pdf", ".jpeg", ".png"]
if attachments:
try:
for attachment in attachments:
extension = os.path.splitext(attachment.name)[1].lower()
if extension in ALLOWED_EXTENSIONS:
logger.info(f"Adding file {attachment} to list...")
files.append(('attachments[]', (attachment.name, attachment, attachment.content_type)))
logger.info(f"Attachment added {attachment}")
else:
logger.warning(f"ATTACMENTS => Invalid file type: {extension}")
except Exception as e:
logger.exception("ATTACHMENTS => Error in uploading attachments. This was caused by: %s",e)
else:
logger.info("No attachments were uploaded")
API_KEY = fd.FRESHDESK_API_KEY
API_URL = fd.MY_FRESHDESK_API_URL
try:
create_ticket = requests.post(
API_URL,
auth = (API_KEY, 'X'),
data = ticket_data,
files = files,
timeout = 30,
headers = {'Content-Type' : 'application/json'}
)
logger.debug("Response Status Code: %s ", create_ticket.status_code)
logger.debug("Response Content: %s ", create_ticket.json())
if create_ticket.status_code == 201:
return redirect('successful_submission')
else:
return redirect('raise_support_ticket_error')
except requests.RequestException as req_ex:
# Handle errors from the requests library
logger.exception("Request failed with exception: %s", req_ex)
print("exception")
return redirect('raise_support_ticket_error')
except Exception as e:
# Handle other exceptions
logger.exception("An unexpected error occurred: %s", e)
print("exception")
return redirect('raise_support_ticket_error')
else:
form = LegalDocumentsFeedback()
return form
This is called by the URL name in the urls.py
from django.urls import path
from django.shortcuts import render
from . import views
urlpatterns = [
path('raise-support-ticket/', views.successful_submission, name="successful_submission"),
path('raise-support-ticket-error/', views.submission_error, name='raise_support_ticket_error'),
path('raise_ticket/', views.create_and_submit_ticket, name='raise_support_ticket'),
]
Error logs
2024-08-12 10:16:44,611 - support.views - DEBUG - Response Status Code: 400
2024-08-12 10:16:44,612 - support.views - DEBUG - Response Content: {'code': 'invalid_json', 'message': 'Request body has invalid json format'}
The next time I see the same error message, I will pick this computer up and I will throw it against the wall