post updates on mastodon
This commit is contained in:
parent
0b7d568a46
commit
3165e50b9f
7 changed files with 71 additions and 85 deletions
35
froide_govplan/signals.py
Normal file
35
froide_govplan/signals.py
Normal 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"
|
||||
))
|
||||
Loading…
Add table
Add a link
Reference in a new issue