refactor: Abstract post to fedi component
This commit is contained in:
@@ -20,39 +20,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="block">
|
<div class="block">
|
||||||
{% if action_was_posting %}
|
{% include "fellchensammlung/partials/social_media/post-to-fedi.html" %}
|
||||||
{% if posted_successfully %}
|
|
||||||
<div class="message is-success">
|
|
||||||
<div class="message-header">
|
|
||||||
{% translate 'Vermittlung gepostet' %}
|
|
||||||
</div>
|
|
||||||
<div class="message-body">
|
|
||||||
{% blocktranslate with post_url=post.url %}
|
|
||||||
Link zum Post: <a href={{ post_url }}>{{ post_url }}</a>
|
|
||||||
{% endblocktranslate %}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
{% else %}
|
|
||||||
<div class="message is-danger">
|
|
||||||
<div class="message-header">
|
|
||||||
{% translate 'Vermittlung konnte nicht gepostet werden' %}
|
|
||||||
</div>
|
|
||||||
<div class="message-body">
|
|
||||||
{{ error_message }}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
{% endif %}
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<form class="cell" method="post">
|
|
||||||
{% csrf_token %}
|
|
||||||
<input type="hidden" name="action" value="post_to_fedi">
|
|
||||||
<button class="button is-fullwidth is-warning is-primary" type="submit" id="submit">
|
|
||||||
<i class="fa-solid fa-bullhorn fa-fw"></i> {% translate "Vermittlung ins Fediverse posten" %}
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
{% load i18n %}
|
||||||
|
|
||||||
|
{% if action_was_posting %}
|
||||||
|
{% if posted_successfully %}
|
||||||
|
<div class="message is-success">
|
||||||
|
<div class="message-header">
|
||||||
|
{% translate 'Vermittlung gepostet' %}
|
||||||
|
</div>
|
||||||
|
<div class="message-body">
|
||||||
|
{% blocktranslate with post_url=post.url %}
|
||||||
|
Link zum Post: <a href={{ post_url }}>{{ post_url }}</a>
|
||||||
|
{% endblocktranslate %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
|
<div class="message is-danger">
|
||||||
|
<div class="message-header">
|
||||||
|
{% translate 'Vermittlung konnte nicht gepostet werden' %}
|
||||||
|
</div>
|
||||||
|
<div class="message-body">
|
||||||
|
{{ error_message }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<form class="cell" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
<input type="hidden" name="action" value="post_to_fedi">
|
||||||
|
{% if adoption_notice %}
|
||||||
|
<input type="hidden" name="adoption_notice_pk" value="{{ adoption_notice.pk }}">
|
||||||
|
{% endif %}
|
||||||
|
<button class="button is-fullwidth is-warning is-primary" type="submit" id="submit">
|
||||||
|
<i class="fa-solid fa-bullhorn fa-fw"></i> {% translate "Vermittlung ins Fediverse posten" %}
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
from django.template.loader import render_to_string
|
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
|
from notfellchen import settings
|
||||||
|
|
||||||
|
|
||||||
@@ -86,7 +87,10 @@ class FediClient:
|
|||||||
|
|
||||||
|
|
||||||
def post_an_to_fedi(adoption_notice):
|
def post_an_to_fedi(adoption_notice):
|
||||||
|
try:
|
||||||
client = FediClient(settings.fediverse_access_token, settings.fediverse_api_base_url)
|
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}
|
context = {"adoption_notice": adoption_notice}
|
||||||
status_text = render_to_string("fellchensammlung/misc/fediverse/an-post.md", context)
|
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,
|
platform=PlatformChoices.FEDIVERSE,
|
||||||
url=response['url'], )
|
url=response['url'], )
|
||||||
return post
|
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
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, \
|
|||||||
RescueOrgForm
|
RescueOrgForm
|
||||||
from .models import Language, Announcement
|
from .models import Language, Announcement
|
||||||
from .tools import i18n, img
|
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.geo import GeoAPI, zoom_level_for_radius
|
||||||
from .tools.metrics import gather_metrics_data, get_rescue_org_check_stats
|
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, \
|
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":
|
if request.method == "POST":
|
||||||
action = request.POST.get("action")
|
action = request.POST.get("action")
|
||||||
if action == "post_to_fedi":
|
if action == "post_to_fedi":
|
||||||
adoption_notice = SocialMediaPost.get_an_to_post()
|
context = handle_post_fedi_action()
|
||||||
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.")}
|
|
||||||
return render(request, 'fellchensammlung/mod-tool-overview.html', context=context)
|
return render(request, 'fellchensammlung/mod-tool-overview.html', context=context)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user