from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden from django.views.decorators.csrf import csrf_exempt import json from proctoring.models.image import Image,FaceCheckImage from datetime import datetime # @csrf_exempt # def GetReportOLD(request): # if request.method != "GET": # return HttpResponseForbidden("Only GET requests are allowed") # try: # data = request.GET # shareId = data.get("shareId", "") # except json.JSONDecodeError: # return HttpResponseBadRequest("Invalid JSON data") # if shareId == "": # return HttpResponseBadRequest("Missing parameters") # # Get the current directory of the script # current_dir = os.path.dirname(os.path.abspath(__file__)) # # Construct the full path to the processed images directory # processed_images_dir = os.path.join(current_dir, "processed_images", shareId) # # Check if the directory exists # if not os.path.exists(processed_images_dir): # return HttpResponseBadRequest("Invalid share ID") # # Create a list to store the base64 encoded images # base64_images = { # "screenshots": [], # "acceptable": [], # "attentionRequired": [], # "faceCheck": "", # "idCheck": "", # } # screenshots_dir = os.path.join(processed_images_dir, "screenshots") # acceptable_dir = os.path.join(processed_images_dir, "passed") # attentionRequired_dir = os.path.join(processed_images_dir, "failed") # faceCheck_dir = os.path.join(processed_images_dir, "identity-check", "face") # idCheck_dir = os.path.join(processed_images_dir, "identity-check", "id") # if os.path.exists(screenshots_dir): # for filename in os.listdir(screenshots_dir): # # Construct the full path to the image file # filepath = os.path.join(screenshots_dir, filename) # # Read the image file # with open(filepath, "rb") as f: # image_data = f.read() # # Encode the image data in base64 # base64_image = base64.b64encode(image_data).decode("utf-8") # # Append the base64 encoded image to the list # base64_images["screenshots"].append(base64_image) # if os.path.exists(acceptable_dir): # for filename in os.listdir(acceptable_dir): # # Construct the full path to the image file # filepath = os.path.join(acceptable_dir, filename) # # Read the image file # with open(filepath, "rb") as f: # image_data = f.read() # # Encode the image data in base64 # base64_image = base64.b64encode(image_data).decode("utf-8") # # Append the base64 encoded image to the list # base64_images["acceptable"].append(base64_image) # if os.path.exists(attentionRequired_dir): # for filename in os.listdir(attentionRequired_dir): # # Construct the full path to the image file # filepath = os.path.join(attentionRequired_dir, filename) # # Read the image file # with open(filepath, "rb") as f: # image_data = f.read() # # Encode the image data in base64 # base64_image = base64.b64encode(image_data).decode("utf-8") # # Append the base64 encoded image to the list # base64_images["attentionRequired"].append(base64_image) # if os.path.exists(faceCheck_dir): # for filename in os.listdir(faceCheck_dir): # # Construct the full path to the image file # filepath = os.path.join(faceCheck_dir, filename) # # Read the image file # with open(filepath, "rb") as f: # image_data = f.read() # # Encode the image data in base64 # base64_image = base64.b64encode(image_data).decode("utf-8") # # Append the base64 encoded image to the list # base64_images["faceCheck"] = base64_image # if os.path.exists(idCheck_dir): # for filename in os.listdir(idCheck_dir): # # Construct the full path to the image file # filepath = os.path.join(idCheck_dir, filename) # # Read the image file # with open(filepath, "rb") as f: # image_data = f.read() # # Encode the image data in base64 # base64_image = base64.b64encode(image_data).decode("utf-8") # # Append the base64 encoded image to the list # base64_images["idCheck"] = base64_image # # # Iterate over the files in the directory # # for filename in os.listdir(processed_images_dir): # # # Construct the full path to the image file # # filepath = os.path.join(processed_images_dir, filename) # # # Read the image file # # with open(filepath, 'rb') as f: # # image_data = f.read() # # # Encode the image data in base64 # # base64_image = base64.b64encode(image_data).decode('utf-8') # # # Append the base64 encoded image to the list # # base64_images.append(base64_image) # # Return the list of base64 encoded images # response = base64_images # return HttpResponse(json.dumps(response)) @csrf_exempt def get_report(request): if request.method != "GET": return HttpResponseForbidden("Only GET requests are allowed") try: data = request.GET shareId = data.get("shareId", "") except json.JSONDecodeError: return HttpResponseBadRequest("Invalid JSON data") if shareId == "": return HttpResponseBadRequest("Missing parameters") # # Get the current directory of the script # current_dir = os.path.dirname(os.path.abspath(__file__)) # # Construct the full path to the processed images directory # processed_images_dir = os.path.join(current_dir, "processed_images", shareId) # # Check if the directory exists # if not os.path.exists(processed_images_dir): # return HttpResponseBadRequest("Invalid share ID") images =list(Image.objects.filter(shareId=shareId).values()) # faces =list(FaceCheckImage.objects.filter(shareId=shareId).values()) # resp = images.append(faces) resp = images return HttpResponse(json.dumps(resp, default=datetime_handler)) def datetime_handler(x): if isinstance(x, datetime): return x.strftime('%Y-%m-%d %H:%M:%S') # You can choose another format if needed raise TypeError("Unknown type")