post updates on mastodon

This commit is contained in:
Jonas Heinrich 2025-05-07 08:10:40 +02:00
parent 0b7d568a46
commit 3165e50b9f
7 changed files with 71 additions and 85 deletions

View file

@ -1,7 +1,6 @@
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _
class FroideGovPlanConfig(AppConfig):
name = "froide_govplan"
verbose_name = _("GovPlan App")
@ -18,3 +17,5 @@ class FroideGovPlanConfig(AppConfig):
api_router.register(
r"governmentplan", GovernmentPlanViewSet, basename="governmentplan"
)
from . import signals

35
froide_govplan/signals.py Normal file
View file

@ -0,0 +1,35 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from django.db import transaction
from mastodon import Mastodon
from .models import GovernmentPlanUpdate, GovernmentPlan
import re
def strip_html_tags(text):
tag_re = re.compile(r'<[^>]+>')
return tag_re.sub('', text)
def post_to_mastodon(text):
mastodon = Mastodon(
access_token=settings.MASTODON_ACCESS_TOKEN,
api_base_url=settings.MASTODON_API_BASE_URL
)
mastodon.status_post(text)
@receiver(post_save, sender=GovernmentPlanUpdate)
def send_mastodon_toot_update(sender, instance, created, **kwargs):
if created and instance.public:
transaction.on_commit(lambda: post_to_mastodon(
f"Neue Entwicklung im Verwaltungsvorhaben: {instance.plan.title}!\n\n"
f"{instance.title}: {strip_html_tags(instance.content)}\n\n"
f"Mehr Informationen: {instance.plan.reference} #karlsruhe"
))
@receiver(post_save, sender=GovernmentPlan)
def send_mastodon_toot_plan(sender, instance, created, **kwargs):
if created and instance.public:
transaction.on_commit(lambda: post_to_mastodon(
f"Neues Verwaltungsvorhaben '{instance.title}' wurde aufgenommen.\n\n"
f"Mehr Informationen: {instance.reference} #karlsruhe"
))

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 KiB