Ajout ChantDoc.lien (alternative à fichier)

This commit is contained in:
Claude Paroz 2025-10-30 11:53:17 +01:00
parent 75f024feaf
commit bbea558cc2
7 changed files with 60 additions and 4 deletions

View file

@ -55,6 +55,27 @@ class MembreEditForm(BootstrapMixin, forms.ModelForm):
return super().save(**kwargs)
class ChantDocForm(BootstrapMixin, forms.ModelForm):
class Meta:
model = ChantDoc
fields = ["fichier", "lien", "titre"]
def __init__(self, **kwargs):
super().__init__(**kwargs)
if self.instance.pk and not self.instance.lien:
del self.fields["lien"]
if self.instance.pk:# and not self.instance.fichier:
del self.fields["fichier"]
def clean(self):
data = super().clean()
if not data.get("fichier") and not data.get("lien"):
raise forms.ValidationError("Vous devez indiquer un fichier ou un lien")
elif data.get("fichier") and data.get("lien"):
raise forms.ValidationError("Vous ne pouvez pas indiquer à la fois un fichier et un lien")
return data
class ChantEditForm(BootstrapMixin, forms.ModelForm):
class Meta:
model = Chant
@ -64,7 +85,7 @@ class ChantEditForm(BootstrapMixin, forms.ModelForm):
super().__init__(**kwargs)
self.formset = None
if self.instance.pk:
DocFormSet = forms.inlineformset_factory(Chant, ChantDoc, fields=["fichier", "titre"], extra=1)
DocFormSet = forms.inlineformset_factory(Chant, ChantDoc, form=ChantDocForm, extra=1)
self.formset = DocFormSet(data=kwargs.get("data"), files=kwargs.get("files"), instance=self.instance)
def is_valid(self):

View file

@ -0,0 +1,16 @@
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('beesgospel', '0007_chantdoc'),
]
operations = [
migrations.AddField(
model_name='chantdoc',
name='lien',
field=models.URLField(blank=True, verbose_name='Lien'),
),
]

View file

@ -123,6 +123,7 @@ class Chant(models.Model):
class ChantDoc(models.Model):
chant = models.ForeignKey(Chant, on_delete=models.CASCADE)
fichier = models.FileField("Fichier", upload_to="chants", blank=True)
lien = models.URLField("Lien", blank=True)
titre = models.CharField("Titre", max_length=200)
def __str__(self):

View file

@ -58,6 +58,7 @@ nav {
.red-bottom { border-bottom: 1px solid red; }
.overleft { margin-left: -4rem; }
.overright { margin-right: -4rem; }
.icon { max-width: 1.5rem; }
.harlow { font-family: "Harlow Solid"; }
.homeurl { color: white; text-decoration: none; }
.homeurl:hover { color: lightgrey; }

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?><!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
<svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M9.83824 18.4467C10.0103 18.7692 10.1826 19.0598 10.3473 19.3173C8.59745 18.9238 7.07906 17.9187 6.02838 16.5383C6.72181 16.1478 7.60995 15.743 8.67766 15.4468C8.98112 16.637 9.40924 17.6423 9.83824 18.4467ZM11.1618 17.7408C10.7891 17.0421 10.4156 16.1695 10.1465 15.1356C10.7258 15.0496 11.3442 15 12.0001 15C12.6559 15 13.2743 15.0496 13.8535 15.1355C13.5844 16.1695 13.2109 17.0421 12.8382 17.7408C12.5394 18.3011 12.2417 18.7484 12 19.0757C11.7583 18.7484 11.4606 18.3011 11.1618 17.7408ZM9.75 12C9.75 12.5841 9.7893 13.1385 9.8586 13.6619C10.5269 13.5594 11.2414 13.5 12.0001 13.5C12.7587 13.5 13.4732 13.5593 14.1414 13.6619C14.2107 13.1384 14.25 12.5841 14.25 12C14.25 11.4159 14.2107 10.8616 14.1414 10.3381C13.4732 10.4406 12.7587 10.5 12.0001 10.5C11.2414 10.5 10.5269 10.4406 9.8586 10.3381C9.7893 10.8615 9.75 11.4159 9.75 12ZM8.38688 10.0288C8.29977 10.6478 8.25 11.3054 8.25 12C8.25 12.6946 8.29977 13.3522 8.38688 13.9712C7.11338 14.3131 6.05882 14.7952 5.24324 15.2591C4.76698 14.2736 4.5 13.168 4.5 12C4.5 10.832 4.76698 9.72644 5.24323 8.74088C6.05872 9.20472 7.1133 9.68686 8.38688 10.0288ZM10.1465 8.86445C10.7258 8.95042 11.3442 9 12.0001 9C12.6559 9 13.2743 8.95043 13.8535 8.86447C13.5844 7.83055 13.2109 6.95793 12.8382 6.2592C12.5394 5.69894 12.2417 5.25156 12 4.92432C11.7583 5.25156 11.4606 5.69894 11.1618 6.25918C10.7891 6.95791 10.4156 7.83053 10.1465 8.86445ZM15.6131 10.0289C15.7002 10.6479 15.75 11.3055 15.75 12C15.75 12.6946 15.7002 13.3521 15.6131 13.9711C16.8866 14.3131 17.9412 14.7952 18.7568 15.2591C19.233 14.2735 19.5 13.1679 19.5 12C19.5 10.8321 19.233 9.72647 18.7568 8.74093C17.9413 9.20477 16.8867 9.6869 15.6131 10.0289ZM17.9716 7.46178C17.2781 7.85231 16.39 8.25705 15.3224 8.55328C15.0189 7.36304 14.5908 6.35769 14.1618 5.55332C13.9897 5.23077 13.8174 4.94025 13.6527 4.6827C15.4026 5.07623 16.921 6.08136 17.9716 7.46178ZM8.67765 8.55325C7.61001 8.25701 6.7219 7.85227 6.02839 7.46173C7.07906 6.08134 8.59745 5.07623 10.3472 4.6827C10.1826 4.94025 10.0103 5.23076 9.83823 5.5533C9.40924 6.35767 8.98112 7.36301 8.67765 8.55325ZM15.3224 15.4467C15.0189 16.637 14.5908 17.6423 14.1618 18.4467C13.9897 18.7692 13.8174 19.0598 13.6527 19.3173C15.4026 18.9238 16.921 17.9186 17.9717 16.5382C17.2782 16.1477 16.3901 15.743 15.3224 15.4467ZM12 21C16.9706 21 21 16.9706 21 12C21 7.02944 16.9706 3 12 3C7.02944 3 3 7.02944 3 12C3 16.9706 7.02944 21 12 21Z" fill="#080341"/>
</svg>

After

Width:  |  Height:  |  Size: 2.6 KiB

View file

@ -11,7 +11,13 @@
{% for subform in form.formset %}
<div class="card text-bg-secondary mt-2">
<div class="card-body">
{{ subform }}
{% if subform.fichier %}{{ subform.fichier.as_field_group }}
{% elif subform.instance.fichier %}
<div>Fichier: <a href="{{ subform.instance.fichier.url }}">{{ subform.instance.fichier }}</a></div>
{% endif %}
{{ subform.lien.as_field_group }}
{{ subform.titre.as_field_group }}
{{ subform.DELETE.as_field_group }}
</div>
</div>
{% endfor %}

View file

@ -11,9 +11,16 @@
<td>{{ chant.numero }}</td>
<td>{{ chant.titre }}</td>
<td>{% for doc in chant.chantdoc_set.all %}
{% if doc.fichier %}
<a href="{{ doc.fichier.url }}">
<img src="{{ doc.fichier.path|icon_url }}" title="{{ doc.titre }}" data-bs-toggle="tooltip">
</a>{% endfor %}
<img src="{{ doc.fichier.path|icon_url }}" title="{{ doc.titre }}" class="icon" data-bs-toggle="tooltip">
</a>
{% else %}
<a href="{{ doc.lien }}">
<img src="{% static 'img/web-icon.svg' %}" title="{{ doc.titre }}" class="icon" data-bs-toggle="tooltip">
</a>
{% endif %}
{% endfor %}
</td>
<td>{{ chant.particularite }}</td>
{% if perms.beesgospel.change_chant %}