Skip to content

Commit

Permalink
Add API for social media tags, fix #2084
Browse files Browse the repository at this point in the history
Co-authored-by: Timo Brembeck <[email protected]>
  • Loading branch information
svenseeberg and timobrembeck committed Oct 12, 2023
1 parent f5c1857 commit f625972
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
11 changes: 11 additions & 0 deletions integreat_cms/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from .v3.pdf_export import pdf_export
from .v3.push_notifications import sent_push_notifications
from .v3.regions import region_by_slug, regions
from .v3.socialmedia_headers import socialmedia_headers

#: The namespace for this URL config (see :attr:`django.urls.ResolverMatch.app_name`)
app_name = "api"
Expand Down Expand Up @@ -123,6 +124,16 @@
urlpatterns = [
path("api/regions/", include(region_api_urlpatterns)),
path("wp-json/extensions/v3/sites/", include(region_api_urlpatterns)),
path(
"api/social/<slug:region_slug>/<slug:language_slug>/",
include(
[
path(
"<path:path>/", socialmedia_headers, name="socialmedia_headers"
),
]
),
),
path(
"api/<slug:region_slug>/",
include(
Expand Down
70 changes: 70 additions & 0 deletions integreat_cms/api/v3/socialmedia_headers.py
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,
},
)
12 changes: 12 additions & 0 deletions integreat_cms/cms/templates/socialmedia_headers.html
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 }}" />
7 changes: 7 additions & 0 deletions integreat_cms/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
#: URL to the Integreat wiki
WIKI_URL = os.environ.get("INTEGREAT_CMS_WIKI_URL", "https://wiki.integreat-app.de")

#: Platform Name, used for app users and social media headers

#: Social media preview image
SOCIAL_PREVIEW_IMAGE = os.environ.get(
"INTEGREAT_SOCIAL_PREVIEW_IMAGE", f"{WEBAPP_URL}/social-media-preview.png"
)

#: RSS feed URLs to the Integreat blog
RSS_FEED_URLS = {
"en": f"{WEBSITE_URL}/en/feed/",
Expand Down
8 changes: 8 additions & 0 deletions integreat_cms/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ msgstr ""
msgid "API"
msgstr "API"

#: api/v3/socialmedia_headers.py
msgid ""
"hallo aschaffenburg is your digital companion for the town of Aschaffenburg. "
"Here you can find local information, advice centres and services."
msgstr ""
"hallo aschaffenburg is your digital companion for the town of Aschaffenburg. "
"Here you can find local information, advice centres and services."

#: cms/apps.py
msgid "CMS"
msgstr "CMS"
Expand Down
2 changes: 2 additions & 0 deletions integreat_cms/release_notes/2023/unreleased/2391.yml
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

0 comments on commit f625972

Please sign in to comment.