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" ))