diff --git a/beesgospel/admin.py b/beesgospel/admin.py index a8e71a4..119f6ed 100644 --- a/beesgospel/admin.py +++ b/beesgospel/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from django.contrib.auth.admin import UserAdmin -from beesgospel.models import Agenda, Chant, Document, Membre, User +from beesgospel.models import Agenda, Chant, ChantDoc, Document, Membre, User @admin.register(Agenda) @@ -33,6 +33,11 @@ class ChantAdmin(admin.ModelAdmin): ordering = ["titre"] +@admin.register(ChantDoc) +class ChantDocAdmin(admin.ModelAdmin): + list_display = ["titre", "chant", "fichier", "lien"] + + @admin.register(User) class UserAdmin(UserAdmin): list_display = ["email", "is_active", "is_staff", "last_login"] diff --git a/beesgospel/forms.py b/beesgospel/forms.py index 9a47efe..10450a5 100644 --- a/beesgospel/forms.py +++ b/beesgospel/forms.py @@ -1,3 +1,5 @@ +import os +import subprocess from secrets import token_hex from django import forms @@ -75,6 +77,23 @@ class ChantDocForm(BootstrapMixin, forms.ModelForm): raise forms.ValidationError("Vous ne pouvez pas indiquer à la fois un fichier et un lien") return data + def save(self, **kwargs): + is_new = not self.instance.pk + instance = super().save(**kwargs) + if is_new and instance.fichier and instance.fichier.name.endswith(".m4a"): + old_path = instance.fichier.path + try: + subprocess.run([ + "ffmpeg", "-i", old_path, "-q:a", "6", old_path.replace(".m4a", ".mp3"), + ], check=True) + except subprocess.CalledProcessError: + pass + else: + instance.fichier.name = instance.fichier.name.replace(".m4a", ".mp3") + instance.save() + os.unlink(old_path) + return instance + class ChantEditForm(BootstrapMixin, forms.ModelForm): class Meta: