Jelajahi Sumber

Add ability to kick off updates

Colin Powell 3 tahun lalu
induk
melakukan
479b789a32
5 mengubah file dengan 49 tambahan dan 6 penghapusan
  1. TEMPAT SAMPAH
      emus/static/img/spinner.gif
  2. 5 0
      games/urls.py
  3. 24 0
      games/views.py
  4. 19 6
      templates/base.html
  5. 1 0
      templates/games/game_library_list.html

TEMPAT SAMPAH
emus/static/img/spinner.gif


+ 5 - 0
games/urls.py

@@ -20,6 +20,11 @@ urlpatterns = [
         views.trigger_rom_update,
         name="game_library_update",
     ),
+    path(
+        "library/update/status/",
+        views.library_update_status,
+        name="game_library_update_status",
+    ),
     path(
         "publisher/",
         views.PublisherList.as_view(),

+ 24 - 0
games/views.py

@@ -1,6 +1,7 @@
 import json
 import logging
 
+from celery import states
 from django.conf import settings
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.mixins import LoginRequiredMixin
@@ -8,6 +9,7 @@ from django.db.models import Avg, Count, F
 from django.http import HttpResponse
 from django.views.generic import DetailView, ListView
 from django.views.generic.list import MultipleObjectMixin
+from django_celery_results.models import TaskResult
 
 from games.tasks import update_roms
 
@@ -16,6 +18,9 @@ from .models import Developer, Game, GameCollection, GameSystem, Genre, Publishe
 logger = logging.Logger(__name__)
 
 
+IN_PROGRESS_STATES = [states.PENDING, states.STARTED, states.RETRY]
+
+
 class RecentGameList(LoginRequiredMixin, ListView):
     model = Game
     paginate_by = 20
@@ -181,3 +186,22 @@ def trigger_rom_update(request):
         status=200,
         content_type="application/json",
     )
+
+
+@login_required
+def library_update_status(request):
+    update_task = TaskResult.objects.filter(
+        task_name="games.tasks.update_roms",
+        status__in=IN_PROGRESS_STATES,
+    ).first()
+
+    if not update_task:
+        state = states.SUCCESS
+    else:
+        state = update_task.status
+
+    return HttpResponse(
+        json.dumps({"state": state}),
+        status=200,
+        content_type="application/json",
+    )

+ 19 - 6
templates/base.html

@@ -30,18 +30,30 @@
             min-height: 3em;
             border-right: 1px solid #777;
         }
-         .card img { width:18em; padding: 1em; }
-         .card-block { padding: 1em 0 1em 0; }
-         .system-badge { padding: 1em; font-size: normal; }
-         {% for system in game_systems %}
-         .{{system.retropie_slug}} { background: #{{system.get_color}}; }
-         {% endfor %}
+        #library-update-status { margin-right:10px; }
+        .card img { width:18em; padding: 1em; }
+        .card-block { padding: 1em 0 1em 0; }
+        .system-badge { padding: 1em; font-size: normal; }
+        .updating { color:#aaa; margin-right: 8px; }
+        {% for system in game_systems %}
+        .{{system.retropie_slug}} { background: #{{system.get_color}}; }
+        {% endfor %}
         </style>
         {% block head_extra %}{% endblock %}
 
         <link rel="apple-touch-icon" href="/apple-touch-icon.png">
         <!-- Place favicon.ico in the root directory -->
 
+        <script>
+        function checkUpdate(){
+            $.get('/library/update/status/', function(data) {
+                $('#library-update-status').html("");
+                console.log('Checking for task');
+                setTimeout(checkUpdate,5000);
+            });
+        }
+        </script>
+
     </head>
     <body>
         <!--[if lt IE 8]>
@@ -86,6 +98,7 @@
                     </div>
                 </li>
                 </ul>
+                {% if update_in_progress %}<em class="updating">Updating</em><img id="library-update-status" src="{% static 'img/spinner.gif'%}"" width=30 />{% endif %}
                 <form class="form-inline my-2 my-lg-0" method="get" action="{% url 'search:search' %}">
                 <input class="form-control mr-sm-2" name="q" type="search" placeholder="Search" aria-label="Search">
                 <button class="btn btn-primary my-2 my-sm-0" type="submit">Search</button>

+ 1 - 0
templates/games/game_library_list.html

@@ -1,4 +1,5 @@
 {% extends "base.html" %}
+
 {% block page_title %}Game Library{% endblock %}
 
 {% block title %}All games by rating{% endblock %}