diff --git a/integreat_cms/api/urls.py b/integreat_cms/api/urls.py index 1d4337667a..ae9e510f29 100644 --- a/integreat_cms/api/urls.py +++ b/integreat_cms/api/urls.py @@ -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" @@ -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///", + include( + [ + path( + "/", socialmedia_headers, name="socialmedia_headers" + ), + ] + ), + ), path( "api//", include( diff --git a/integreat_cms/api/v3/socialmedia_headers.py b/integreat_cms/api/v3/socialmedia_headers.py new file mode 100644 index 0000000000..96e4ae9ce1 --- /dev/null +++ b/integreat_cms/api/v3/socialmedia_headers.py @@ -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, + }, + ) diff --git a/integreat_cms/cms/templates/socialmedia_headers.html b/integreat_cms/cms/templates/socialmedia_headers.html new file mode 100644 index 0000000000..982eb9e3fe --- /dev/null +++ b/integreat_cms/cms/templates/socialmedia_headers.html @@ -0,0 +1,12 @@ +{{ platform }} | {{ title }} + + + + + + + + + + diff --git a/integreat_cms/core/settings.py b/integreat_cms/core/settings.py index f45661da4c..e9d2934332 100644 --- a/integreat_cms/core/settings.py +++ b/integreat_cms/core/settings.py @@ -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/", diff --git a/integreat_cms/locale/de/LC_MESSAGES/django.po b/integreat_cms/locale/de/LC_MESSAGES/django.po index 23e1d3cbdf..b1ca96e7a8 100644 --- a/integreat_cms/locale/de/LC_MESSAGES/django.po +++ b/integreat_cms/locale/de/LC_MESSAGES/django.po @@ -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" diff --git a/integreat_cms/release_notes/2023/unreleased/2391.yml b/integreat_cms/release_notes/2023/unreleased/2391.yml new file mode 100644 index 0000000000..7592fbe49c --- /dev/null +++ b/integreat_cms/release_notes/2023/unreleased/2391.yml @@ -0,0 +1,2 @@ +en: Add social media headers API endpoint +de: Neuer API Endpunkt für Social-Media-Header