From 00e32e0f7ced2ce0d5c66e9f3a53857b4c82a268 Mon Sep 17 00:00:00 2001 From: moanos Date: Sat, 7 Feb 2026 21:35:51 +0100 Subject: [PATCH] refactor: Abstract post to fedi component --- .../fellchensammlung/mod-tool-overview.html | 34 +---------------- .../partials/social_media/post-to-fedi.html | 38 +++++++++++++++++++ src/fellchensammlung/tools/fedi.py | 38 ++++++++++++++++++- src/fellchensammlung/views.py | 24 +----------- 4 files changed, 77 insertions(+), 57 deletions(-) create mode 100644 src/fellchensammlung/templates/fellchensammlung/partials/social_media/post-to-fedi.html diff --git a/src/fellchensammlung/templates/fellchensammlung/mod-tool-overview.html b/src/fellchensammlung/templates/fellchensammlung/mod-tool-overview.html index 7b4271f..eca266a 100644 --- a/src/fellchensammlung/templates/fellchensammlung/mod-tool-overview.html +++ b/src/fellchensammlung/templates/fellchensammlung/mod-tool-overview.html @@ -20,39 +20,7 @@
- {% if action_was_posting %} - {% if posted_successfully %} -
-
- {% translate 'Vermittlung gepostet' %} -
-
- {% blocktranslate with post_url=post.url %} - Link zum Post: {{ post_url }} - {% endblocktranslate %} -
- -
- {% else %} -
-
- {% translate 'Vermittlung konnte nicht gepostet werden' %} -
-
- {{ error_message }} -
- -
- {% endif %} - {% endif %} - -
- {% csrf_token %} - - -
+ {% include "fellchensammlung/partials/social_media/post-to-fedi.html" %}
diff --git a/src/fellchensammlung/templates/fellchensammlung/partials/social_media/post-to-fedi.html b/src/fellchensammlung/templates/fellchensammlung/partials/social_media/post-to-fedi.html new file mode 100644 index 0000000..efc64ef --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/partials/social_media/post-to-fedi.html @@ -0,0 +1,38 @@ +{% load i18n %} + +{% if action_was_posting %} + {% if posted_successfully %} +
+
+ {% translate 'Vermittlung gepostet' %} +
+
+ {% blocktranslate with post_url=post.url %} + Link zum Post: {{ post_url }} + {% endblocktranslate %} +
+ +
+ {% else %} +
+
+ {% translate 'Vermittlung konnte nicht gepostet werden' %} +
+
+ {{ error_message }} +
+ +
+ {% endif %} +{% endif %} + +
+ {% csrf_token %} + + {% if adoption_notice %} + + {% endif %} + +
\ No newline at end of file diff --git a/src/fellchensammlung/tools/fedi.py b/src/fellchensammlung/tools/fedi.py index f371363..9b14697 100644 --- a/src/fellchensammlung/tools/fedi.py +++ b/src/fellchensammlung/tools/fedi.py @@ -1,8 +1,9 @@ import logging import requests from django.template.loader import render_to_string +from django.utils.translation import gettext_lazy as _ -from fellchensammlung.models import SocialMediaPost, PlatformChoices +from fellchensammlung.models import SocialMediaPost, PlatformChoices, AdoptionNotice from notfellchen import settings @@ -86,7 +87,10 @@ class FediClient: def post_an_to_fedi(adoption_notice): - client = FediClient(settings.fediverse_access_token, settings.fediverse_api_base_url) + try: + client = FediClient(settings.fediverse_access_token, settings.fediverse_api_base_url) + except AttributeError: + raise ConnectionError("Configuration for connecting to a Fediverse account is missing") context = {"adoption_notice": adoption_notice} status_text = render_to_string("fellchensammlung/misc/fediverse/an-post.md", context) @@ -101,3 +105,33 @@ def post_an_to_fedi(adoption_notice): platform=PlatformChoices.FEDIVERSE, url=response['url'], ) return post + + +def handle_post_fedi_action(adoption_notice: AdoptionNotice = SocialMediaPost.get_an_to_post()): + if adoption_notice is not None: + logging.info(f"Posting adoption notice: {adoption_notice} ({adoption_notice.id})") + try: + post = post_an_to_fedi(adoption_notice) + context = {"action_was_posting": True, "post": post, "posted_successfully": True} + except requests.exceptions.ConnectionError as e: + logging.error(f"Could not post fediverse post: {e}") + context = {"action_was_posting": True, + "posted_successfully": False, + "error_message": _("Verbindungsfehler. Vermittlung wurde nicht gepostet")} + except requests.exceptions.HTTPError as e: + logging.error(f"Could not post fediverse post: {e}") + context = {"action_was_posting": True, + "posted_successfully": False, + "error_message": _("Fehler beim Posten. Vermittlung wurde nicht gepostet. Das kann " + "z.B. an falschen Zugangsdaten liegen. Kontaktieren einen Admin.")} + except ConnectionError as e: + logging.error(f"Could not post fediverse post: {e}") + context = {"action_was_posting": True, + "posted_successfully": False, + "error_message": _( + "Fehler beim Posten, in der Konfiguration fehlen Zugangsdaten zu einem Fediverse Account")} + else: + context = {"action_was_posting": True, + "posted_successfully": False, + "error_message": _("Keine Vermittlung zum Posten gefunden.")} + return context diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 76240aa..6f323c6 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -29,7 +29,7 @@ from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, \ RescueOrgForm from .models import Language, Announcement from .tools import i18n, img -from .tools.fedi import post_an_to_fedi +from .tools.fedi import handle_post_fedi_action from .tools.geo import GeoAPI, zoom_level_for_radius from .tools.metrics import gather_metrics_data, get_rescue_org_check_stats from .tools.admin import clean_locations, get_unchecked_adoption_notices, deactivate_unchecked_adoption_notices, \ @@ -1006,27 +1006,7 @@ def moderation_tools_overview(request): if request.method == "POST": action = request.POST.get("action") if action == "post_to_fedi": - adoption_notice = SocialMediaPost.get_an_to_post() - if adoption_notice is not None: - logging.info(f"Posting adoption notice: {adoption_notice} ({adoption_notice.id})") - try: - post = post_an_to_fedi(adoption_notice) - context = {"action_was_posting": True, "post": post, "posted_successfully": True} - except requests.exceptions.ConnectionError as e: - logging.error(f"Could not post fediverse post: {e}") - context = {"action_was_posting": True, - "posted_successfully": False, - "error_message": _("Verbindungsfehler. Vermittlung wurde nicht gepostet")} - except requests.exceptions.HTTPError as e: - logging.error(f"Could not post fediverse post: {e}") - context = {"action_was_posting": True, - "posted_successfully": False, - "error_message": _("Fehler beim Posten. Vermittlung wurde nicht gepostet. Das kann " - "z.B. an falschen Zugangsdaten liegen. Kontaktieren einen Admin.")} - else: - context = {"action_was_posting": True, - "posted_successfully": False, - "error_message": _("Keine Vermittlung zum Posten gefunden.")} + context = handle_post_fedi_action() return render(request, 'fellchensammlung/mod-tool-overview.html', context=context)