From ac3a4c669dae1acd6e593aaa88f7ff96453fec27 Mon Sep 17 00:00:00 2001
From: h4-rahman <hamidur2.rahman@live.uwe.ac.uk>
Date: Mon, 29 Apr 2024 01:12:23 +0100
Subject: [PATCH] Formatted results and made them so they are visible on user
 page

---
 myproject/myapp/models.py                | 47 ------------------------
 myproject/myapp/templates/user_page.html | 21 ++++++++---
 myproject/myapp/views.py                 | 26 ++++++++-----
 3 files changed, 33 insertions(+), 61 deletions(-)

diff --git a/myproject/myapp/models.py b/myproject/myapp/models.py
index 620f414..d29a03d 100644
--- a/myproject/myapp/models.py
+++ b/myproject/myapp/models.py
@@ -46,39 +46,6 @@ class Action(Enum):
     DOWNLOAD_BREAKDOWN = "{username} has downloaded a breakdown of their data."
     UNKNOWN = "An unknown error has occurred for user {username}."
 
-# class Logs(models.Model):
-#     """
-#     *   Logs model
-#     """
-#     user_id = models.ForeignKey("User", on_delete=models.CASCADE)
-#     error_id = models.IntegerField()
-#     date = models.DateTimeField()
-
-# class Feedback(models.Model):
-#     """
-#     *   Feedback Model
-#     """
-#     user_id = models.ForeignKey("User", on_delete=models.CASCADE)
-#     content = models.CharField(max_length=2000)
-#     date = models.DateTimeField()
-
-# class Bills(models.Model):
-#     """
-#     *   Bill/receipts Model
-#     """
-#     user_id = models.ForeignKey("User", on_delete=models.CASCADE)
-#     date = models.DateTimeField()
-#     paid = models.BooleanField(default=False)
-
-
-# class Files(models.Model):
-#     """
-#     *   Uploaded files
-#     """
-#     date = models.DateTimeField()
-#     data = models.CharField(max_length=2000)
-#     uploader = models.ForeignKey("User", on_delete=models.CASCADE)
-
 class Audio(models.Model):
     file = models.FileField('audio', upload_to='audio')
 
@@ -86,17 +53,3 @@ class Log(models.Model):
     date = models.DateTimeField(auto_now_add=True)
     user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
     log = models.JSONField()
-
-
-# # LOGIN
-# log_data = get_log_data(Action.LOGIN, 'success', user=request.user.username)
-# create_log(log_data)
-# # REGISTER
-# log_data = get_log_data(Action.REGISTER, 'success', user=request.user.username)
-# create_log(log_data)
-# # INVALID_PASSWORD
-# log_data = get_log_data(Action.INVALID_PASSWORD, 'error', user=request.user.username)
-# create_log(log_data)
-# # GENERATE_FINANCIAL_STATEMENT
-# log_data = get_log_data(Action.GENERATE_FINANCIAL_STATEMENT, 'success', user=request.user.username)
-# create_log(log_data)
diff --git a/myproject/myapp/templates/user_page.html b/myproject/myapp/templates/user_page.html
index 7fbcdc3..84c4288 100644
--- a/myproject/myapp/templates/user_page.html
+++ b/myproject/myapp/templates/user_page.html
@@ -214,11 +214,7 @@
                 >{{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'}}')"
-              >
+              <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 %}
@@ -239,6 +235,16 @@
   </div>
 </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');
@@ -295,6 +301,11 @@ function closeModal() {
                 class="font-medium text-blue-600 dark:text-blue-500 hover:underline"
                 >{{entry.action}}</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>
             <td class="px-6 py-4">{{entry.user}}</td>
             <td class="px-6 py-4">
diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py
index eb925b7..51017a7 100644
--- a/myproject/myapp/views.py
+++ b/myproject/myapp/views.py
@@ -71,10 +71,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(request.user, Action.UPLOAD_FILE, status, file, description)
+            log_data = get_log_data(request.user, Action.UPLOAD_FILE, status, file)
             create_log(request.user, log_data)
 
         return JsonResponse({'message': 'Log created successfully'}, status=201)
@@ -87,7 +86,6 @@ def admin_table(request):
     with connection.cursor() as cursor:
         cursor.execute(query)
         rows = cursor.fetchall()
-    print(rows)
     # Create a list of dictionaries from the query results
     data = []
     for row in rows:
@@ -99,7 +97,8 @@ def admin_table(request):
 
         # Create a dictionary with the date, user, and JSON fields
         date = row[0].strftime('%Y-%m-%d %H:%M:%S')
-        entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status']}
+        entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status'],
+                 'description': log['description']}
         data.append(entry)
 
     # Return the data as a JSON response
@@ -125,7 +124,8 @@ def user_table(request):
 
         # Create a dictionary with the date, user, and JSON fields
         date = row[0].strftime('%Y-%m-%d %H:%M:%S')
-        entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status']}
+        entry = {'date': date, 'user': user_id, 'file': log['file'], 'action': log['action'], 'status': log['status'],
+                 'description': log['description']}
         data.append(entry)
 
     # Return the data as a JSON response
@@ -323,11 +323,19 @@ class InstrumentDetectionView(APIView):
 
     def format_predictions(self, predictions):
         instruments = ['Guitar', 'Drum', 'Violin', 'Piano']
-        formatted_predictions = []
+        instrument_windows = {instrument: [] for instrument in instruments}
+
         for window_index, prediction in enumerate(predictions, start=1):
-            formatted_window = f"<strong>Window {window_index}</strong><br>"
-            formatted_scores = "<br>".join([f"{instruments[i]} - {score:.2f}" for i, score in enumerate(prediction)])
-            formatted_predictions.append(f"{formatted_window}{formatted_scores}")
+            highest_score_index = prediction.index(max(prediction))
+            highest_score_instrument = instruments[highest_score_index]
+            instrument_windows[highest_score_instrument].append(window_index)
+
+        formatted_predictions = []
+        for instrument, windows in instrument_windows.items():
+            if windows:
+                window_list = ', '.join(map(str, windows))
+                formatted_predictions.append(f"{instrument} - Windows: {window_list}")
+
         return formatted_predictions
     
 
-- 
GitLab