166 lines
5.7 KiB
Python
166 lines
5.7 KiB
Python
from django.db import transaction
|
|
from django.utils import timezone
|
|
from rest_framework import status
|
|
from rest_framework.views import APIView
|
|
from . import serializers
|
|
from accounts.models import IAmPrincipal
|
|
from ..models import Tickets, TicketAttachment, TicketIssueType
|
|
from goodtimes import constants
|
|
from goodtimes.services import SMSError, SMSService
|
|
from manage_communications import models
|
|
# from nifty11_project.services import SMSError, SMSService
|
|
from goodtimes.utils import ApiResponse
|
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
|
from rest_framework_simplejwt.authentication import JWTAuthentication
|
|
from datetime import datetime
|
|
|
|
|
|
class ContactUsView(APIView):
|
|
|
|
authentication_classes = []
|
|
permission_classes = []
|
|
|
|
def post(self, request):
|
|
|
|
print("request.data: ", request.data)
|
|
|
|
serializer = serializers.ContactUsSerializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save()
|
|
|
|
return ApiResponse.success(data=serializer.data, message=constants.SUCCESS)
|
|
|
|
class TicketView(APIView):
|
|
serializer_class = serializers.TicketsSerializer
|
|
model = Tickets
|
|
authentication_classes = [JWTAuthentication]
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
return Tickets.objects.filter(deleted=False)
|
|
|
|
def get(self, request):
|
|
month = request.GET.get("month")
|
|
status_filter = request.GET.get("status")
|
|
|
|
tickets = self.get_queryset()
|
|
|
|
if month:
|
|
try:
|
|
month_date = datetime.strptime(month, "%Y-%m")
|
|
print(f"month_Date is {month_date} , month is {month_date.month} , year is {month_date.year}, ticket is {tickets}")
|
|
tickets = tickets.filter(created_on__year=month_date.year)
|
|
except ValueError:
|
|
return ApiResponse.error(message="Invalid date format. Use YYYY-MM", errors="Invalid date format. Use YYYY-MM")
|
|
|
|
if status_filter:
|
|
tickets = tickets.filter(ticket_status=status_filter)
|
|
|
|
serializer = self.serializer_class(tickets, many=True)
|
|
ticket_status = [Tickets.REQUESTED, Tickets.VIEWED, Tickets.IN_PROGRESS, Tickets.RESOLVED]
|
|
print(f"ticket status is {ticket_status}")
|
|
data = {
|
|
"ticket_status": ticket_status,
|
|
"ticket": serializer.data
|
|
}
|
|
return ApiResponse.success(message=constants.SUCCESS, data=data)
|
|
|
|
def post(self, request):
|
|
data = request.data.copy()
|
|
print(f"your data is {request.data}")
|
|
attachments_data = data.pop("attachments", [])
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if not serializer.is_valid():
|
|
error_response = {
|
|
"status": status.HTTP_400_BAD_REQUEST,
|
|
"message": constants.VALIDATION_ERROR,
|
|
"errors": serializer.errors,
|
|
}
|
|
return ApiResponse.error(**error_response)
|
|
|
|
try:
|
|
ticket = serializer.save(principal=request.user)
|
|
attachments = []
|
|
for attachment_data in attachments_data:
|
|
attachment = TicketAttachment(image=attachment_data)
|
|
attachment.save()
|
|
attachments.append(attachment)
|
|
|
|
ticket.attachments.add(*attachments)
|
|
except Exception as e:
|
|
error_response = {
|
|
"status": status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
"message": constants.SOMETHING_WRONG,
|
|
"errors": str(e),
|
|
}
|
|
return ApiResponse.error(**error_response)
|
|
|
|
return ApiResponse.success(message=constants.SUCCESS)
|
|
|
|
|
|
class TicketCategoryView(APIView):
|
|
model = TicketIssueType
|
|
serializer_class = serializers.TicketIssueTypeSerializer
|
|
authentication_classes = [JWTAuthentication]
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get(self, request):
|
|
issue_type = self.model.objects.filter(deleted=False)
|
|
print(f"issue type {issue_type}")
|
|
serializer = self.serializer_class(issue_type, many=True)
|
|
return ApiResponse.success(message=constants.SUCCESS, data=serializer.data)
|
|
|
|
|
|
class TicketStopView(APIView):
|
|
model = Tickets
|
|
authentication_classes = [JWTAuthentication]
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get(self, request, pk):
|
|
try:
|
|
ticket = self.model.objects.get(pk=pk)
|
|
ticket.stop_ticket()
|
|
return ApiResponse.success(message=constants.SUCCESS)
|
|
except Exception as e:
|
|
error_message = {
|
|
"status": status.HTTP_404_NOT_FOUND,
|
|
"message": constants.RECORD_NOT_FOUND,
|
|
"errors": str(e)
|
|
}
|
|
return ApiResponse.error(**error_message)
|
|
|
|
|
|
class FeedbackView(APIView):
|
|
serializer_class = serializers.FeedbackSerializer
|
|
authentication_classes = [JWTAuthentication]
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def post(self, request):
|
|
data = request.data.copy()
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if not serializer.is_valid():
|
|
error_response = {
|
|
"status": status.HTTP_400_BAD_REQUEST,
|
|
"message": constants.VALIDATION_ERROR,
|
|
"errors": serializer.errors,
|
|
}
|
|
return ApiResponse.error(**error_response)
|
|
|
|
try:
|
|
serializer.save(principal=request.user)
|
|
except Exception as e:
|
|
error_response = {
|
|
"status": status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
"message": constants.SOMETHING_WRONG,
|
|
"errors": str(e),
|
|
}
|
|
|
|
return ApiResponse.error(**error_response)
|
|
|
|
return ApiResponse.success(message=constants.SUCCESS)
|