Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forum): utiliser l'image du forum dans la vignette opengraph #725

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lacommunaute/forum/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ForumForm(forms.ModelForm):
description = forms.CharField(
widget=forms.Textarea(attrs={"rows": 20}), required=False, label="Contenu (markdown autorisé)"
)
image = forms.ImageField(required=False, label="Banniere de couverture")
image = forms.ImageField(required=False, label="Banniere de couverture, format 1200 x 630 pixels recommandé")

def save(self, commit=True):
forum = super().save(commit=False)
Expand Down
22 changes: 20 additions & 2 deletions lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from faker import Faker
from machina.core.loading import get_class
from taggit.models import Tag

from django.conf import settings
from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory, ForumRatingFactory
from lacommunaute.forum.models import Forum
Expand All @@ -18,7 +18,7 @@
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.users.factories import UserFactory
from lacommunaute.utils.testing import parse_response_to_soup, reset_model_sequence_fixture

from pytest_django.asserts import assertContains

faker = Faker()

Expand Down Expand Up @@ -545,6 +545,24 @@ def test_rated_forum(self, client, db, snapshot):
content = parse_response_to_soup(response, selector="#rating-area1")
assert str(content) == snapshot(name="rated_forum")

def test_opengraph_for_forum_with_image(self, client, db):
forum = ForumFactory(with_public_perms=True, with_image=True)
response = client.get(forum.get_absolute_url())
assertContains(
response,
f'<meta property="og:image" content="{settings.MEDIA_URL}{settings.AWS_STORAGE_BUCKET_NAME}/banner',
)
assertContains(
response,
f'<meta property="twitter:image" content="{settings.MEDIA_URL}{settings.AWS_STORAGE_BUCKET_NAME}/banner',
)

def test_opengraph_for_forum_wo_image(self, client, db):
forum = ForumFactory(with_public_perms=True)
response = client.get(forum.get_absolute_url())
assertContains(response, '<meta property="og:image" content="/static/images/logo-og-communaute')
assertContains(response, '<meta property="og:image" content="/static/images/logo-og-communaute')


reset_forum_sequence = pytest.fixture(reset_model_sequence_fixture(Forum))

Expand Down
3 changes: 3 additions & 0 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def get_context_data(self, **kwargs):

if self.will_render_documentation_variant():
context["sibling_forums"] = forum.get_siblings(include_self=True)

if forum.image:
context["og_image"] = forum.image
return context


Expand Down
4 changes: 2 additions & 2 deletions lacommunaute/templates/layouts/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
<meta property="og:locale" content="fr_FR">
<meta property="og:type" content="website">
<meta property="og:url" content="https://communaute-experimentation.inclusion.beta.gouv.fr/">
<meta property="og:image" content="{% static 'images/logo-og-communaute.png' %}">
<meta property="og:image" content="{% if og_image %}{{ og_image.url }}{% else %}{% static 'images/logo-og-communaute.png' %}{% endif %}">
<meta property="og:image:alt" content="Logo de la communauté de l'inclusion">
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<!-- https://metatags.io Twitter -->
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:url" content="https://communaute-experimentation.inclusion.beta.gouv.fr/">
<meta property="twitter:image" content="{% static 'images/logo-og-communaute.png' %}">
<meta property="twitter:image" content="{% if og_image %}{{ og_image.url }}{% else %}{% static 'images/logo-og-communaute.png' %}{% endif %}">
<meta property="twitter:site" content="inclusion_gouv">
<!-- https://metatags.io Facebook -->
<meta property="fb:page_id" content="inclusion.gouv">
Expand Down
Loading