]> git.0d.be Git - django-panik-emissions.git/commitdiff
add (hidden by default) external URL field to soundfile model
authorFrédéric Péters <fpeters@0d.be>
Thu, 27 May 2021 12:55:21 +0000 (14:55 +0200)
committerFrédéric Péters <fpeters@0d.be>
Thu, 27 May 2021 12:55:21 +0000 (14:55 +0200)
emissions/forms.py
emissions/migrations/0019_auto_20210527_1454.py [new file with mode: 0644]
emissions/models.py

index 4a84c66eb46ac953296128d7b9520d70de22f1b1..ee90c8a03212096514065bd88618a5c276ed4766 100644 (file)
@@ -246,18 +246,31 @@ class SoundFileForm(forms.ModelForm):
             'file': JqueryFileUploadInput(),
         }
 
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        if getattr(settings, 'USE_EXTERNAL_SOUNDS', True):
+            self.fields['file'].widget = forms.HiddenInput()
+        else:
+            self.fields['external_url'].widget = forms.HiddenInput()
+
     def clean(self):
         super().clean()
-        if self.cleaned_data.get('file'):
-            duration = get_duration(self.cleaned_data['file'].file.name)
-            if not duration:
-                raise ValidationError(_('Invalid file, could not get duration.'))
+        if getattr(settings, 'USE_EXTERNAL_SOUNDS', True):
+            if not self.cleaned_data.get('external_url'):
+                raise ValidationError(_('Missing URL'))
+        else:
+            if not self.cleaned_data.get('file'):
+                raise ValidationError(_('Missing file'))
+            if self.cleaned_data.get('file'):
+                duration = get_duration(self.cleaned_data['file'].file.name)
+                if not duration:
+                    raise ValidationError(_('Invalid file, could not get duration.'))
 
 
 class SoundFileEditForm(forms.ModelForm):
     class Meta:
         model = SoundFile
-        exclude = ('has_focus', 'got_focus', 'file', 'duration', 'download_count')
+        exclude = ('has_focus', 'got_focus', 'file', 'external_url', 'duration', 'download_count')
         widgets = {
             'episode': forms.HiddenInput(),
         }
diff --git a/emissions/migrations/0019_auto_20210527_1454.py b/emissions/migrations/0019_auto_20210527_1454.py
new file mode 100644 (file)
index 0000000..3ef1170
--- /dev/null
@@ -0,0 +1,24 @@
+# Generated by Django 2.2.19 on 2021-05-27 14:54
+
+from django.db import migrations, models
+import emissions.models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('emissions', '0018_emission_tags'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='soundfile',
+            name='external_url',
+            field=models.URLField(blank=True, null=True, verbose_name='URL'),
+        ),
+        migrations.AlterField(
+            model_name='soundfile',
+            name='file',
+            field=models.FileField(blank=True, max_length=250, null=True, upload_to=emissions.models.get_sound_path, verbose_name='File'),
+        ),
+    ]
index 88b69a1e76c2dc7c7109abc10b4a4ace3a71431c..859eb6aee2c2544bc7e6ba8c3668a8ead63678ac 100644 (file)
@@ -492,7 +492,8 @@ class SoundFile(models.Model):
         ordering = ['creation_timestamp']
 
     episode = models.ForeignKey('Episode', verbose_name=_('Episode'), on_delete=models.CASCADE)
-    file = models.FileField(_('File'), upload_to=get_sound_path, max_length=250)
+    file = models.FileField(_('File'), upload_to=get_sound_path, max_length=250, blank=True, null=True)
+    external_url = models.URLField(_('URL'), null=True, blank=True)
     podcastable = models.BooleanField(
         _('Podcastable'),
         default=False,