Explorar o código

Error out well when no files found

Colin Powell %!s(int64=3) %!d(string=hai) anos
pai
achega
6a750dd68d
Modificáronse 2 ficheiros con 23 adicións e 2 borrados
  1. 8 1
      games/management/commands/import_gamelist_xml_file.py
  2. 15 1
      games/utils.py

+ 8 - 1
games/management/commands/import_gamelist_xml_file.py

@@ -19,4 +19,11 @@ class Command(BaseCommand):
             options["system"],
             options["file"],
         )
-        self.stdout.write(self.style.SUCCESS(f"Successfully imported {len(games)}"))
+        if games:
+            self.stdout.write(self.style.SUCCESS(f"Successfully imported {len(games)}"))
+        else:
+            self.stdout.write(
+                self.style.ERROR(
+                    "No games imported, check for gamelist.xml file or re-run scraper"
+                )
+            )

+ 15 - 1
games/utils.py

@@ -6,6 +6,10 @@ from django.conf import settings
 
 from .models import Developer, Game, GameSystem, Genre, Publisher
 
+import logging
+
+logger = logging.Logger(__name__)
+
 US_STRINGS = ["(u)", "(usa)", "(us)"]
 JP_STRINGS = ["(j)", "japan", "jp"]
 EU_STRINGS = ["(e)", "eur", "europe", "pal"]
@@ -23,8 +27,18 @@ def import_gamelist_file_to_db_for_system(game_system_slug, file_path=None):
     imported_games = []
     if not file_path:
         file_path = os.path.join(settings.ROMS_DIR, game_system_slug, "gamelist.xml")
+    if not os.path.exists(file_path):
+        logger.info(
+            "File path for {game_system_slug} had no gamelist.xml file, run a scraper first!"
+        )
+        return
+
     gamelist = ET.parse(file_path)
-    game_system = GameSystem.objects.get(retropie_slug=game_system_slug)
+    game_system = GameSystem.objects.filter(retropie_slug=game_system_slug).first()
+    if not game_system:
+        game_system = GameSystem.objects.create(
+            name=game_system_slug, retropie_slug=game_system_slug
+        )
 
     games = gamelist.findall("game")
     for game in games: