From 59985ad1da5ffc23194d4d2d5c8729f5742ec8fe Mon Sep 17 00:00:00 2001
From: h4-rahman <hamidur2.rahman@live.uwe.ac.uk>
Date: Tue, 23 Apr 2024 06:03:07 +0100
Subject: [PATCH] Save and display model results

---
 myproject/myapp/templates/_base.html     |  7 +++++
 myproject/myapp/templates/user_page.html | 38 ++++++++++++++++++++++++
 myproject/myapp/views.py                 | 10 ++-----
 3 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/myproject/myapp/templates/_base.html b/myproject/myapp/templates/_base.html
index de172c2..0a3beef 100644
--- a/myproject/myapp/templates/_base.html
+++ b/myproject/myapp/templates/_base.html
@@ -9,6 +9,13 @@
     {% compress css %}
     <link rel="stylesheet" href="{% static 'src/output.css' %}" />
     {% endcompress %}
+    <style>
+      /* Ensure the modal body takes up all available space minus a margin, allowing for scrolling */
+      .modal-body {
+        max-height: calc(100vh - 4rem); /* Adjust the 4rem to increase/decrease the top and bottom margin */
+        overflow-y: auto; /* Enable vertical scrolling */
+      }
+    </style>
   </head>
 
   <body>
diff --git a/myproject/myapp/templates/user_page.html b/myproject/myapp/templates/user_page.html
index dffc797..5a36c2d 100644
--- a/myproject/myapp/templates/user_page.html
+++ b/myproject/myapp/templates/user_page.html
@@ -206,12 +206,50 @@
                 class="font-medium text-blue-600 dark:text-blue-500 hover:underline"
                 >{{entry.file}}</a
               >
+              {% if entry.description %}
+              <button
+                type="button"
+                class="ml-4 px-2 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600"
+                onclick="showModal('{{entry.description|join:'\n'}}')"
+              >
+                Show Results
+              </button>
+              {% endif %}
             </td>
           </tr>
           {%endfor%}
         </tbody>
       </table>
     </div>
+
+<!-- Modal -->
+<div id="myModal" class="hidden fixed inset-0 flex items-center justify-center bg-black bg-opacity-50 z-50">
+  <!-- Modal content with scrolling enabled -->
+  <div class="modal-body bg-white dark:bg-gray-800 rounded-lg p-4 md:p-6 w-full max-w-2xl">
+    <h3 class="text-2xl font-bold mb-4">Predictions:</h3>
+    <ul id="predictionList" class="space-y-2"></ul>
+    <button onclick="closeModal()" class="mt-4 px-4 py-2 bg-red-500 text-white rounded-md hover:bg-red-600">Close</button>
+  </div>
+</div>
+
+<script>
+function showModal(predictions) {
+  var predictionList = document.getElementById('predictionList');
+  predictionList.innerHTML = '';
+  predictions.split('\n').forEach(function(prediction) {
+    var li = document.createElement('li');
+    li.innerHTML = prediction;
+    li.className = 'bg-gray-100 dark:bg-gray-700 px-4 py-2 rounded-md';
+    predictionList.appendChild(li);
+  });
+  document.getElementById('myModal').classList.remove('hidden');
+}
+
+function closeModal() {
+  document.getElementById('myModal').classList.add('hidden');
+}
+</script>
+
     {% if 1 %} {% comment %} REPLACE WITH LOGIC TO CHECK PROPER USER
     {%endcomment %}
     {% if user_profile.user_type == 2 %}
diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py
index 40b209f..b42e991 100644
--- a/myproject/myapp/views.py
+++ b/myproject/myapp/views.py
@@ -66,10 +66,9 @@ def log_fileupload(request):
         data = json.loads(request.body)
         status = data.get('status')
         file = data.get('file')
-        description = data.get('description')
 
         if request.user.is_authenticated:
-            log_data = get_log_data(Action.UPLOAD_FILE, status, file, description)
+            log_data = get_log_data(Action.UPLOAD_FILE, status, file)
             create_log(request.user, log_data)
 
         return JsonResponse({'message': 'Log created successfully'}, status=201)
@@ -113,7 +112,7 @@ def user_table(request):
         log = json.loads(row[2])
         # Create a dictionary with the date, user, and JSON fields
         date = row[0].strftime('%Y-%m-%d %H:%M:%S')
-        entry = {'date': date, 'user': row[1], 'file': log['file'], 'action': log['action'], 'status': log['status']}
+        entry = {'date': date, 'user': row[1], 'file': log['file'], 'action': log['action'], 'status': log['status'], 'description': log['description']}
         data.append(entry)
 
     # Return the data as a JSON response
@@ -183,6 +182,7 @@ def users(request):
     context['token_count'] = token_count
     context['user_profile'] = user_profile
     context['user'] = user
+    print(context['user_data'])
 
     return render(request, 'user_page.html', context)
 
@@ -266,10 +266,6 @@ class InstrumentDetectionView(APIView):
         if serializer.is_valid():
             audio_file = serializer.validated_data['audio_file']
             
-            # Save the uploaded file temporarily
-            # with open('temp_audio.wav', 'wb') as f:
-            #     f.write(audio_file.read())
-            
             # Preprocess the audio file
             preprocessed_data = preprocess_audio_for_inference(audio_file)
             
-- 
GitLab