]> git.0d.be Git - django-panik-combo.git/blobdiff - panikombo/forms.py
initial version
[django-panik-combo.git] / panikombo / forms.py
diff --git a/panikombo/forms.py b/panikombo/forms.py
new file mode 100644 (file)
index 0000000..57f0329
--- /dev/null
@@ -0,0 +1,27 @@
+from django import forms
+from django_select2.widgets import HeavySelect2Widget, convert_to_js_string_arr
+
+from emissions.models import SoundFile
+
+from .models import SoundCell
+from .views import soundfiles
+
+class SoundFileWidget(HeavySelect2Widget):
+    def render_texts(self, selected_choices, choices):
+        queryset = SoundFile.objects.filter(id__in=selected_choices)
+        def fmt(soundfile):
+            return '%s - %s - %s' % (soundfile.episode.emission.title,
+                    soundfile.episode.title,
+                    soundfile.title or soundfile.id)
+        texts = [fmt(soundfile) for soundfile in queryset.select_related()]
+        return convert_to_js_string_arr(texts)
+
+
+class SoundCellForm(forms.ModelForm):
+    class Meta:
+        model = SoundCell
+        fields = ('soundfile',)
+
+    def __init__(self, *args, **kwargs):
+        super(SoundCellForm, self).__init__(*args, **kwargs)
+        self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)