-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 1983-new_reco_pages
- Loading branch information
Showing
18 changed files
with
331 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,4 +48,5 @@ | |
SpectacularSwaggerView.as_view(url_name="schema"), | ||
name="swagger-ui", | ||
), | ||
path("ssr/", include("ssr.urls")), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class SsrConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "ssr" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% for key, value in meta_tags.items %} | ||
<meta property="{{ key }}" content="{{ value }}"> | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from unittest.mock import patch | ||
|
||
from django.test import Client, TestCase | ||
|
||
from tournesol.tests.factories.entity import VideoFactory | ||
|
||
|
||
def mock_get_static_index_html(): | ||
return """ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Tournesol</title> | ||
<!--DJANGO_META_TAGS--> | ||
</head> | ||
<body> | ||
<h1>Mocked html page</h1> | ||
</body> | ||
</html> | ||
""" | ||
|
||
|
||
@patch("ssr.views.get_static_index_html", new=mock_get_static_index_html) | ||
class RenderedHtmlTestCase(TestCase): | ||
def setUp(self): | ||
self.client = Client() | ||
|
||
def test_index_html_root(self): | ||
response = self.client.get("/ssr/") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertContains(response, '<meta property="og:title" content="Tournesol">') | ||
|
||
def test_index_html_arbitrary_path(self): | ||
response = self.client.get("/ssr/faq") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertContains(response, '<meta property="og:title" content="Tournesol">') | ||
|
||
def test_index_html_video_entity(self): | ||
video = VideoFactory() | ||
|
||
response = self.client.get(f"/ssr/entities/{video.uid}") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertContains( | ||
response, f'<meta property="og:title" content="{video.metadata["name"]}">' | ||
) | ||
self.assertContains(response, '<meta property="og:type" content="video">') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.urls import path, re_path | ||
|
||
from . import views | ||
|
||
|
||
urlpatterns = [ | ||
path( | ||
"entities/<str:uid>", | ||
views.render_tournesol_html_with_dynamic_tags, | ||
name="ssr_entities", | ||
), | ||
re_path( | ||
r".*", | ||
views.render_tournesol_html_with_dynamic_tags, | ||
name="ssr_default", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import typing as tp | ||
|
||
import requests | ||
from django.http import HttpResponse, HttpRequest | ||
from django.conf import settings | ||
from django.template.loader import render_to_string | ||
|
||
from tournesol.models import Entity | ||
from tournesol.models.entity import TYPE_VIDEO | ||
|
||
|
||
def get_static_index_html() -> str: | ||
if settings.DEBUG: | ||
try: | ||
# Try to get index.html from dev-env frontend container | ||
resp = requests.get("http://tournesol-dev-front:3000") | ||
except requests.ConnectionError: | ||
resp = requests.get(settings.TOURNESOL_MAIN_URL) | ||
resp.raise_for_status() | ||
return resp.text | ||
return (settings.FRONTEND_STATIC_FILES_PATH / "index.html").read_text() | ||
|
||
|
||
def get_default_meta_tags(request: HttpRequest) -> dict[str, str]: | ||
full_frontend_path = request.get_full_path().removeprefix("/ssr/") | ||
return { | ||
"og:site_name": "Tournesol", | ||
"og:type": "website", | ||
"og:title": "Tournesol", | ||
"og:description": ( | ||
"Compare online content and contribute to the development of " | ||
"responsible content recommendations." | ||
), | ||
"og:image": f"{settings.MAIN_URL}preview/{full_frontend_path}", | ||
"og:url": f"{settings.TOURNESOL_MAIN_URL}{full_frontend_path}", | ||
"twitter:card": "summary_large_image", | ||
} | ||
|
||
|
||
def get_entity_meta_tags(uid: str) -> dict[str, str]: | ||
try: | ||
entity: Entity = Entity.objects.get(uid=uid) | ||
except Entity.DoesNotExist: | ||
return {} | ||
|
||
if entity.type != TYPE_VIDEO: | ||
return {} | ||
|
||
meta_tags = { | ||
"og:type": "video", | ||
"og:video:url": f"https://youtube.com/embed/{entity.video_id}", | ||
"og:video:type": "text/html", | ||
} | ||
|
||
if video_title := entity.metadata.get("name"): | ||
meta_tags["og:title"] = video_title | ||
|
||
if video_channel_name := entity.metadata.get("uploader"): | ||
meta_tags["og:description"] = video_channel_name | ||
|
||
return meta_tags | ||
|
||
|
||
def render_tournesol_html_with_dynamic_tags(request: HttpRequest, uid: tp.Optional[str] = None): | ||
index_html = get_static_index_html() | ||
meta_tags = get_default_meta_tags(request) | ||
if uid is not None: | ||
meta_tags |= get_entity_meta_tags(uid) | ||
|
||
rendered_html = index_html.replace( | ||
"<!--DJANGO_META_TAGS-->", | ||
render_to_string( | ||
"opengraph/meta_tags.html", | ||
{ | ||
"meta_tags": meta_tags, | ||
}, | ||
), | ||
1, | ||
) | ||
return HttpResponse(rendered_html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.