web_defender/Ticketsystem/views.py
2024-12-09 13:43:16 +05:30

57 lines
2.6 KiB
Python

from django.http import JsonResponse
from django.views import View
from .forms import HelpdeskSubmissionForm
from .models import HelpdeskSubmission
class HelpdeskSubmissionCreateView(View):
def post(self, request, *args, **kwargs):
form = HelpdeskSubmissionForm(request.POST, request.FILES) # Handle form data and file uploads
if form.is_valid():
submission = form.save(commit=False) # Create the submission object without saving yet
submission.status = 1 # Set status to 1 manually
submission.user = request.user # Assign the logged-in user to the submission
submission.save() # Save the submission to the database
return JsonResponse({'success': True, 'message': 'Submission created successfully!'})
else:
return JsonResponse({'success': False, 'errors': form.errors}, status=400)
class HelpdeskSubmissionListView(View):
def get(self, request, *args, **kwargs):
if request.user.is_authenticated:
submissions = HelpdeskSubmission.objects.filter(user=request.user)
submissions_list = [
{
'title': submission.title,
'description': submission.description,
'status': submission.status,
'image': submission.image.url if submission.image else None
}
for submission in submissions
]
return JsonResponse({'submissions': submissions_list})
else:
return JsonResponse({'error': 'User is not authenticated'}, status=403)
class HelpdeskSubmissionAllView(View):
def get(self, request, *args, **kwargs):
if request.user.is_authenticated:
submissions = HelpdeskSubmission.objects.all() # Get all submissions
submissions_list = [
{
'user': submission.user.username, # Include the username of the user who submitted the ticket
'title': submission.title,
'description': submission.description,
'image': submission.image.url if submission.image else None,
'status': submission.status, # Assuming you have a status field in your model
'created_at': submission.created_at.strftime('%Y-%m-%d %H:%M:%S') # Format the date
}
for submission in submissions
]
return JsonResponse({'submissions': submissions_list})
else:
return JsonResponse({'error': 'User is not authenticated'}, status=403)