]> git.0d.be Git - django-panik-nonstop.git/commitdiff
get local/remote base path from settings
authorFrédéric Péters <fpeters@0d.be>
Sun, 14 Jun 2020 10:59:52 +0000 (12:59 +0200)
committerFrédéric Péters <fpeters@0d.be>
Sun, 14 Jun 2020 11:00:20 +0000 (13:00 +0200)
nonstop/app_settings.py [new file with mode: 0644]
nonstop/management/commands/create_tracks_from_nonstop.py
nonstop/models.py

diff --git a/nonstop/app_settings.py b/nonstop/app_settings.py
new file mode 100644 (file)
index 0000000..5873b69
--- /dev/null
@@ -0,0 +1,17 @@
+from django.conf import settings
+
+
+class AppSettings:
+    def get_setting(self, setting, default=None):
+        return settings.getattr('NONSTOP_' + setting, default)
+
+    @property
+    def REMOTE_BASE_PATH(self):
+        return self.get_setting('REMOTE_BASE_PATH', '/srv/soma/nonstop/')
+
+    @property
+    def LOCAL_BASE_PATH(self):
+        return self.get_setting('LOCAL_BASE_PATH', '/media/nonstop/')
+
+
+app_settings = AppSettings()
index a520aed9276d1cddc3db8c4fe6e9374c4f94027a..8f8fc3c230561ec223c2962673463915ab8f5f9e 100644 (file)
@@ -5,10 +5,9 @@ from django.core.management.base import BaseCommand, CommandError
 from django.utils.text import slugify
 
 from ...models import NonstopFile, Track, Artist
+from ...app_settings import app_settings
 from emissions.models import Nonstop
 
-LOCAL_BASE_PATH = '/media/nonstop/'
-REMOTE_BASE_PATH = '/srv/soma/nonstop/'
 
 tranche_slug_mapping = {
     'Acouphene': 'acouphene',
@@ -35,15 +34,15 @@ class Command(BaseCommand):
                 continue
         count = NonstopFile.objects.filter(**kwargs).count()
         for i, nonstopfile in enumerate(NonstopFile.objects.filter(**kwargs)):
-            filepath = nonstopfile.filepath.replace(REMOTE_BASE_PATH, LOCAL_BASE_PATH)
+            filepath = nonstopfile.filepath.replace(app_settings.REMOTE_BASE_PATH, app_settings.LOCAL_BASE_PATH)
             if self.verbose:
                 print(i, count, filepath)
-            short_filepath = filepath[len(LOCAL_BASE_PATH):]
+            short_filepath = filepath[len(app_settings.LOCAL_BASE_PATH):]
             if short_filepath.startswith('SPOTS'):
                 continue
             if not os.path.exists(filepath):
                 if self.verbose and short_filepath.startswith('Tranches'):
-                    print('missing file:', filepath[len(LOCAL_BASE_PATH):])
+                    print('missing file:', filepath[len(app_settings.LOCAL_BASE_PATH):])
                 continue
             if self.verbose:
                 print(short_filepath)
index 562c972aa53524f0b64fe604410b23cf3d158a64..f7f5e283b7972ac024588bdb99a3b0710315be17 100644 (file)
@@ -11,8 +11,8 @@ from django.dispatch import receiver
 from django.utils.timezone import now
 from django.utils.translation import ugettext_lazy as _
 
-REMOTE_BASE_PATH = '/srv/soma/nonstop/'
-LOCAL_BASE_PATH = '/media/nonstop/'
+from .app_settings import app_settings
+
 
 TRANCHE_SLUG_DIR_MAPPING = {
     'acouphene': 'Acouphene',
@@ -26,6 +26,8 @@ TRANCHE_SLUG_DIR_MAPPING = {
     'up-beat-tempo': 'Up_Beat_Tempo',
 }
 
+
+
 class Artist(models.Model):
     name = models.CharField(_('Name'), max_length=255)
 
@@ -120,7 +122,7 @@ class Track(models.Model):
             if not zone.slug in TRANCHE_SLUG_DIR_MAPPING:
                 continue
             zone_dir = TRANCHE_SLUG_DIR_MAPPING[zone.slug]
-            zone_path = os.path.join(LOCAL_BASE_PATH, 'Tranches', zone_dir, filename)
+            zone_path = os.path.join(app_settings.LOCAL_BASE_PATH, 'Tranches', zone_dir, filename)
             if zone in current_zones:
                 if not os.path.exists(zone_path):
                     os.symlink(os.path.join('..', '..', nonstop_file.short), zone_path)
@@ -137,16 +139,16 @@ class NonstopFile(models.Model):
 
     @property
     def short(self):
-        return self.filepath[len(REMOTE_BASE_PATH):]
+        return self.filepath[len(app_settings.REMOTE_BASE_PATH):]
 
     def set_track_filepath(self, filepath):
-        self.filepath = os.path.join(REMOTE_BASE_PATH, 'tracks', filepath)
+        self.filepath = os.path.join(app_settings.REMOTE_BASE_PATH, 'tracks', filepath)
         self.filename = os.path.basename(filepath)
 
     def get_local_filepath(self):
         if not self.short:
             return None
-        return os.path.join(LOCAL_BASE_PATH, self.short)
+        return os.path.join(app_settings.LOCAL_BASE_PATH, self.short)
 
 
 class SomaLogLine(models.Model):
@@ -196,7 +198,7 @@ class Jingle(models.Model):
     def get_local_filepath(self):
         if not self.short:
             return None
-        return os.path.join(LOCAL_BASE_PATH, 'SPOTS', self.short)
+        return os.path.join(app_settings.LOCAL_BASE_PATH, 'SPOTS', self.short)
 
     @property
     def title(self):