diff --git a/cope2n-api/fwd_api/api/accuracy_view.py b/cope2n-api/fwd_api/api/accuracy_view.py index e482e58..3ed49f2 100644 --- a/cope2n-api/fwd_api/api/accuracy_view.py +++ b/cope2n-api/fwd_api/api/accuracy_view.py @@ -165,7 +165,7 @@ class AccuracyViewSet(viewsets.ViewSet): 'Client Request Time (ms)': request.client_request_time, 'Server Processing Time (ms)': request.preprocessing_time + request.ai_inference_time, 'Is Reviewed': request.is_reviewed, - 'Is Bad Quality': request.is_bad_image_quality, + # 'Is Bad Quality': request.is_bad_image_quality, 'created_at': request.created_at.isoformat() }) @@ -180,4 +180,86 @@ class AccuracyViewSet(viewsets.ViewSet): return JsonResponse(response) - return JsonResponse({'error': 'Invalid request method.'}, status=405) \ No newline at end of file + return JsonResponse({'error': 'Invalid request method.'}, status=405) + + +class RequestViewSet(viewsets.ViewSet): + lookup_field = "username" + + @extend_schema(request = { + 'multipart/form-data': { + 'type': 'object', + 'properties': { + 'reviewed_result': { + 'type': 'string', + }, + } + }, + }, responses=None, tags=['Request'] + ) + @action(detail=False, url_path=r"request/(?P[\w\-]+)", methods=["GET", "POST"]) + def get_subscription_request(self, request, request_id=None): + if request.method == 'GET': + base_query = Q(request_id=request_id) + + subscription_request = SubscriptionRequest.objects.filter(base_query).first() + + data = [] + + imeis = [] + purchase_date = [] + retailer = "" + try: + if subscription_request.reviewed_result is not None: + imeis = subscription_request.reviewed_result.get("imei_number", []) + purchase_date = subscription_request.reviewed_result.get("purchase_date", []) + retailer = subscription_request.reviewed_result.get("retailername", "") + elif subscription_request.feedback_result is not None : + imeis = subscription_request.feedback_result.get("imei_number", []) + purchase_date = subscription_request.feedback_result.get("purchase_date", []) + retailer = subscription_request.feedback_result.get("retailername", "") + elif subscription_request.predict_result is not None: + if subscription_request.predict_result.get("status", 404) == 200: + imeis = subscription_request.predict_result.get("content", {}).get("document", [])[0].get("content", [])[3].get("value", []) + purchase_date = subscription_request.predict_result.get("content", {}).get("document", [])[0].get("content", [])[2].get("value", []) + retailer = subscription_request.predict_result.get("content", {}).get("document", [])[0].get("content", [])[0].get("value", []) + except Exception as e: + print(f"[ERROR]: {e}") + print(f"[ERROR]: {subscription_request}") + data.append({ + 'RequestID': subscription_request.request_id, + 'RedemptionID': subscription_request.redemption_id, + 'IMEIs': imeis, + 'Purchase Date': purchase_date, + 'Retailer': retailer, + 'Reviewed result': subscription_request.reviewed_result, + 'Feedback result': subscription_request.feedback_result, + 'Client Request Time (ms)': subscription_request.client_request_time, + 'Server Processing Time (ms)': subscription_request.preprocessing_time + subscription_request.ai_inference_time, + 'Is Reviewed': subscription_request.is_reviewed, + # 'Is Bad Quality': subscription_request.is_bad_image_quality, + 'created_at': subscription_request.created_at.isoformat() + }) + + response = { + 'subscription_requests': data + } + + return JsonResponse(response) + + elif request.method == 'POST': + data = request.data + + base_query = Q(request_id=request_id) + + subscription_request = SubscriptionRequest.objects.filter(base_query).first() + + try: + subscription_request.reviewed_result = data['reviewed_result'] + except Exception as e: + print(f"[ERROR]: {e}") + print(f"[ERROR]: {subscription_request}") + + return JsonResponse({'message': 'success.'}, status=200) + else: + return JsonResponse({'error': 'Invalid request method.'}, status=405) \ No newline at end of file diff --git a/cope2n-api/fwd_api/api_router.py b/cope2n-api/fwd_api/api_router.py index 9a466dc..322cc20 100755 --- a/cope2n-api/fwd_api/api_router.py +++ b/cope2n-api/fwd_api/api_router.py @@ -2,7 +2,7 @@ from django.conf import settings from rest_framework.routers import DefaultRouter, SimpleRouter from fwd_api.api.ctel_view import CtelViewSet -from fwd_api.api.accuracy_view import AccuracyViewSet +from fwd_api.api.accuracy_view import AccuracyViewSet, RequestViewSet from fwd_api.api.ctel_user_view import CtelUserViewSet @@ -16,6 +16,7 @@ else: router.register("ctel", CtelViewSet, basename="CtelAPI") router.register("ctel", CtelUserViewSet, basename="CtelUserAPI") router.register("ctel", AccuracyViewSet, basename="AccuracyAPI") +router.register("ctel", RequestViewSet, basename="RequestAPI") app_name = "api" urlpatterns = router.urls