]> git.0d.be Git - django-panik-nonstop.git/commitdiff
add "create_jingles" management command, to create them from filesystem
authorFrédéric Péters <fpeters@0d.be>
Wed, 17 Jun 2020 13:57:49 +0000 (15:57 +0200)
committerFrédéric Péters <fpeters@0d.be>
Thu, 2 Jul 2020 08:01:29 +0000 (10:01 +0200)
nonstop/management/commands/create_jingles.py [new file with mode: 0644]

diff --git a/nonstop/management/commands/create_jingles.py b/nonstop/management/commands/create_jingles.py
new file mode 100644 (file)
index 0000000..cada442
--- /dev/null
@@ -0,0 +1,24 @@
+import datetime
+import os
+
+from django.core.management.base import BaseCommand, CommandError
+from ...app_settings import app_settings
+
+from ...models import Jingle
+from emissions.utils import get_duration
+
+
+class Command(BaseCommand):
+    def handle(self, verbosity, **kwargs):
+        self.verbose = (int(verbosity) > 1)
+
+        base_path = os.path.abspath(os.path.join(app_settings.LOCAL_BASE_PATH, app_settings.JINGLES_PREFIX))
+        for basedir, dirnames, filenames in os.walk(base_path):
+            for filename in filenames:
+                fullpath = os.path.join(basedir, filename)
+                filepath = fullpath[len(base_path)+1:]
+                if Jingle.objects.filter(filepath=filepath).exists():
+                    continue
+                jingle = Jingle(label=filename, filepath=filepath)
+                jingle.duration = datetime.timedelta(seconds=float(get_duration(fullpath)))
+                jingle.save()