From ef01c405d5b8ce7ac2dc853bec6e68b1dd6b2b02 Mon Sep 17 00:00:00 2001 From: amandine-sahl Date: Tue, 29 Oct 2024 11:22:05 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Affichage=20du=20m=C3=A9dia=20originel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apptax/admin/admin_view.py | 7 +++---- apptax/admin/templates/admin/details_taxref.html | 2 +- apptax/taxonomie/models.py | 4 ++-- apptax/tests/fixtures.py | 13 +++++++++++++ apptax/tests/test_models.py | 15 +++++++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/apptax/admin/admin_view.py b/apptax/admin/admin_view.py index 97951aab..a1f92c33 100644 --- a/apptax/admin/admin_view.py +++ b/apptax/admin/admin_view.py @@ -563,10 +563,9 @@ def can_delete(self): column_exclude_list = ("url",) form_extra_fields = { "chemin": ImageUploadFieldWithoutDelete( - label="Image", + label="Téléverser un fichier", base_path=Path(current_app.config["MEDIA_FOLDER"], "taxhub").absolute(), namegen=taxref_media_file_name, - thumbnail_size=(150, 150, True), endpoint="media_taxhub", ) } @@ -579,8 +578,8 @@ def _list_thumbnail(view, context, model, name): html = "" if model.types.nom_type_media in ("Photo", "Photo_principale"): html = f""" - - Taxon image + + Taxon image """ elif model.types.nom_type_media in ("Audio"): diff --git a/apptax/admin/templates/admin/details_taxref.html b/apptax/admin/templates/admin/details_taxref.html index 5051df6a..51440571 100644 --- a/apptax/admin/templates/admin/details_taxref.html +++ b/apptax/admin/templates/admin/details_taxref.html @@ -173,7 +173,7 @@
{{theme}}
{% if media.types.nom_type_media in ("Photo", "Photo_principale") %} - + Taxon image {% elif media.types.nom_type_media in ("Audio")%} diff --git a/apptax/taxonomie/models.py b/apptax/taxonomie/models.py index 090bf105..5b1d095b 100644 --- a/apptax/taxonomie/models.py +++ b/apptax/taxonomie/models.py @@ -317,10 +317,10 @@ class TMedias(db.Model): taxon = db.relationship(Taxref, back_populates="medias") @hybrid_property - def media_url(self): + def media_url(self): if self.url: return self.url - else: + elif self.chemin: return url_for("media_taxhub", filename=self.chemin, _external=True) def __repr__(self): diff --git a/apptax/tests/fixtures.py b/apptax/tests/fixtures.py index b2abdb28..e35103e7 100644 --- a/apptax/tests/fixtures.py +++ b/apptax/tests/fixtures.py @@ -149,6 +149,19 @@ def nom_with_media(): taxon.medias.append(media) +@pytest.fixture +def nom_with_media_chemin(): + with db.session.begin_nested(): + taxon = Taxref.query.get(60577) + media = TMedias( + titre="test", + chemin="mon_image.jpg", + is_public=True, + types=BibTypesMedia.query.first(), + ) + taxon.medias.append(media) + + @pytest.fixture(scope="session") def users(app): users = {} diff --git a/apptax/tests/test_models.py b/apptax/tests/test_models.py index b59ab1e1..e2d9441c 100644 --- a/apptax/tests/test_models.py +++ b/apptax/tests/test_models.py @@ -44,3 +44,18 @@ def test_taxref_comparison(self): assert not animalia <= cinnamon assert not cinnamon <= capra_ibex assert not capra_ibex <= cinnamon + + + def test_tmedias_media_url_url(self, nom_with_media): + taxon = db.session.execute( + sa.select(Taxref).where(Taxref.cd_nom == 60577) + ).scalar_one() + assert len(taxon.medias) == 1 + assert taxon.medias[0].media_url == "http://photo.com" + + def test_tmedias_media_url_chemin(self, nom_with_media_chemin): + taxon = db.session.execute( + sa.select(Taxref).where(Taxref.cd_nom == 60577) + ).scalar_one() + assert len(taxon.medias) == 1 + assert taxon.medias[0].media_url.endswith("media/taxhub/mon_image.jpg") From db862e2601bdc86698852a93772bfae8bb2cdc4b Mon Sep 17 00:00:00 2001 From: amandine-sahl Date: Tue, 29 Oct 2024 11:22:40 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Correction=20migration=20chemin=20m=C3=A9di?= =?UTF-8?q?a=20-=20cas=20des=20chaines=20vides?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../versions/52d1b5dd965e_strip_static_media_from_path.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apptax/migrations/versions/52d1b5dd965e_strip_static_media_from_path.py b/apptax/migrations/versions/52d1b5dd965e_strip_static_media_from_path.py index f97a79a2..86552698 100644 --- a/apptax/migrations/versions/52d1b5dd965e_strip_static_media_from_path.py +++ b/apptax/migrations/versions/52d1b5dd965e_strip_static_media_from_path.py @@ -25,7 +25,7 @@ def upgrade(): SET chemin = regexp_replace(chemin, '^static/medias/', '') WHERE - chemin IS NOT NULL AND url IS NULL + NULLIF(chemin, '') IS NOT NULL AND NULLIF(url, '') IS NULL """ ) From d29b54d592f666287a0a13363ea1aa47963d9f79 Mon Sep 17 00:00:00 2001 From: amandine-sahl Date: Tue, 29 Oct 2024 11:25:12 +0100 Subject: [PATCH 3/3] Black --- apptax/taxonomie/models.py | 4 ++-- apptax/tests/test_models.py | 17 ++++++----------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/apptax/taxonomie/models.py b/apptax/taxonomie/models.py index 5b1d095b..ac7e2663 100644 --- a/apptax/taxonomie/models.py +++ b/apptax/taxonomie/models.py @@ -317,10 +317,10 @@ class TMedias(db.Model): taxon = db.relationship(Taxref, back_populates="medias") @hybrid_property - def media_url(self): + def media_url(self): if self.url: return self.url - elif self.chemin: + elif self.chemin: return url_for("media_taxhub", filename=self.chemin, _external=True) def __repr__(self): diff --git a/apptax/tests/test_models.py b/apptax/tests/test_models.py index e2d9441c..4f307b45 100644 --- a/apptax/tests/test_models.py +++ b/apptax/tests/test_models.py @@ -45,17 +45,12 @@ def test_taxref_comparison(self): assert not cinnamon <= capra_ibex assert not capra_ibex <= cinnamon - - def test_tmedias_media_url_url(self, nom_with_media): - taxon = db.session.execute( - sa.select(Taxref).where(Taxref.cd_nom == 60577) - ).scalar_one() - assert len(taxon.medias) == 1 + def test_tmedias_media_url_url(self, nom_with_media): + taxon = db.session.execute(sa.select(Taxref).where(Taxref.cd_nom == 60577)).scalar_one() + assert len(taxon.medias) == 1 assert taxon.medias[0].media_url == "http://photo.com" - - def test_tmedias_media_url_chemin(self, nom_with_media_chemin): - taxon = db.session.execute( - sa.select(Taxref).where(Taxref.cd_nom == 60577) - ).scalar_one() + + def test_tmedias_media_url_chemin(self, nom_with_media_chemin): + taxon = db.session.execute(sa.select(Taxref).where(Taxref.cd_nom == 60577)).scalar_one() assert len(taxon.medias) == 1 assert taxon.medias[0].media_url.endswith("media/taxhub/mon_image.jpg")