verwaltungstracker/froide_govplan/signals.py

41 lines
1.6 KiB
Python

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
import html
def strip_html_tags(text):
tag_re = re.compile(r'<[^>]+>')
plain_text = tag_re.sub('', text)
return html.unescape(plain_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"🔗 Primärquelle: {instance.plan.reference}\n"
f"📋 Verwaltungstracker-Eintrag: {instance.plan.get_absolute_domain_url()}\n\n"
f"#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"🔗 Primärquelle: {instance.reference}\n"
f"📋 Verwaltungstracker-Eintrag: {instance.get_absolute_domain_url()}\n\n"
f"#karlsruhe"
))