-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API for social media tags, fix #2084
Co-authored-by: Timo Brembeck <[email protected]>
- Loading branch information
1 parent
f5c1857
commit f625972
Showing
6 changed files
with
110 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
This module includes functions related to the social media headers API endpoint. | ||
""" | ||
import logging | ||
from html import unescape | ||
|
||
from django.conf import settings | ||
from django.http import Http404 | ||
from django.shortcuts import render | ||
from django.utils.html import strip_tags | ||
from django.utils.translation import activate | ||
from django.utils.translation import gettext as _ | ||
|
||
from ...cms.models import PageTranslation | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# pylint: disable=unused-argument | ||
def socialmedia_headers(request, region_slug, language_slug, path): | ||
""" | ||
Renders the social media headers for a single page | ||
:param request: The request that has been sent to the Django server | ||
:type request: ~django.http.HttpRequest | ||
:param region_slug: Slug defining the region | ||
:type region_slug: str | ||
:param language_slug: Code to identify the desired language | ||
:type language_slug: str | ||
:param path: Should be a page translation slug | ||
:type path: str | ||
:return: HTML meta headers required by social media platforms | ||
:rtype: ~django.template.response.TemplateResponse | ||
""" | ||
region = request.region | ||
page_translation = PageTranslation.search(region, language_slug, path).first() | ||
activate(language_slug) | ||
|
||
if region.slug != "hallo": | ||
if not page_translation: | ||
raise Http404 | ||
excpert = unescape(strip_tags(page_translation.content))[:100] | ||
title = page_translation.title | ||
else: | ||
excerpt = _( | ||
"hallo aschaffenburg is your digital companion for the town of Aschaffenburg. Here" | ||
" you can find local information, advice centres and services." | ||
if region == "hallo" | ||
else "Your local information guide with over 100 regions in Germany." | ||
) | ||
title = "Local Information for You" | ||
return render( | ||
request, | ||
"socialmedia_headers.html", | ||
{ | ||
"title": title, | ||
"excerpt": excerpt, | ||
"platform": "hallo aschaffenburg" | ||
if region.slug == "hallo" | ||
else settings.PLATFORM_NAME, | ||
"path": f"https://halloaschaffenburg.de/{path}" | ||
if region.slug == "hallo" | ||
else f"{settings.WEBAPP_URL}/{path}", | ||
"image": settings.SOCIAL_PREVIEW_IMAGE, | ||
}, | ||
) |
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,12 @@ | ||
<title>{{ platform }} | {{ title }}</title> | ||
<meta name="apple-mobile-web-app-title" | ||
content="{{ platform }} | {{ title }}"> | ||
<meta name="twitter:title " content="{{ platform }} | {{ title }}"> | ||
<meta name="twitter:description" content="{{ excerpt }}"> | ||
<meta name="twitter:card" content="summary"> | ||
<meta property="og:title" content="{{ platform }} | {{ title }}"> | ||
<meta property="og:description" content="{{ excerpt }}"> | ||
<meta property="og:image" content="{{ image }}" /> | ||
<meta property="og:image:width" content="1200" /> | ||
<meta property="og:image:height" content="630" /> | ||
<meta property='og:url' content="{{ path }}" /> |
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
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,2 @@ | ||
en: Add social media headers API endpoint | ||
de: Neuer API Endpunkt für Social-Media-Header |