Skip to content

Commit

Permalink
Fix/medias (#572)
Browse files Browse the repository at this point in the history
* Affichage du média originel lors du clic sur thumbnail
* THv2 : Correction migration chemin média - cas des chaines vides
  • Loading branch information
amandine-sahl authored Oct 29, 2024
1 parent dc514ca commit 4cfbc2c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
7 changes: 3 additions & 4 deletions apptax/admin/admin_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
}
Expand All @@ -579,8 +578,8 @@ def _list_thumbnail(view, context, model, name):
html = ""
if model.types.nom_type_media in ("Photo", "Photo_principale"):
html = f"""
<a target='_blank' href='{ url_for("t_media.getThumbnail_tmedias", id_media=model.id_media)}'>
<img width="100" src='{ url_for("t_media.getThumbnail_tmedias", id_media=model.id_media)}' alt="Taxon image">
<a target='_blank' href='{model.media_url}'>
<img width="100" src='{ url_for("t_media.getThumbnail_tmedias", id_media=model.id_media) }' alt="Taxon image">
</a>
"""
elif model.types.nom_type_media in ("Audio"):
Expand Down
2 changes: 1 addition & 1 deletion apptax/admin/templates/admin/details_taxref.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ <h5 class="card-title"> {{theme}} </h5>
<tr>
<td class="col-5">
{% if media.types.nom_type_media in ("Photo", "Photo_principale") %}
<a target='_blank' href='{{ url_for("t_media.getThumbnail_tmedias", id_media=media.id_media)}}'>
<a target='_blank' href=' {{ media.media_url }}'>
<img width="100" src='{{ url_for("t_media.getThumbnail_tmedias", id_media=media.id_media)}}' alt="Taxon image">
</a>
{% elif media.types.nom_type_media in ("Audio")%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
)

Expand Down
2 changes: 1 addition & 1 deletion apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class TMedias(db.Model):
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):
Expand Down
13 changes: 13 additions & 0 deletions apptax/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
10 changes: 10 additions & 0 deletions apptax/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ 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")

0 comments on commit 4cfbc2c

Please sign in to comment.