Skip to content

Commit

Permalink
Mail admins after auto publishing a question
Browse files Browse the repository at this point in the history
  • Loading branch information
knatten committed Sep 23, 2024
1 parent 080bebf commit 0b58268
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions quiz/management/commands/auto_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

import tweepy
from django.core.mail import mail_admins
from django.core.management.base import BaseCommand
from django.utils import timezone

Expand All @@ -21,11 +22,13 @@ def handle(self, *args, **options):
print(f"Publishing question {q}")
q.state = 'PUB'
q.save()
socials_message = "No posts to social media"
if (q.socials_text):
if skip_socials:
print("Skipping posting to social media!")
else:
self.post_to_x(q.socials_text)
socials_message = self.post_to_x(q.socials_text)
mail_admins(f"Published question {q}", "{socials_message}")

def post_to_x(self, content):
print(f"Posting to X: '{content}'")
Expand All @@ -41,7 +44,9 @@ def post_to_x(self, content):
response = client.create_tweet(
text=content
)
print(f"Posted https://x.com/user/status/{response.data['id']}")
post_url = f"https://x.com/user/status/{response.data['id']}"
print(f"Posted {post_url}")
return post_url
except Exception as e:
print(f"Failed to post '{content}' to X due to exception '{e}'")
sys.exit(1)

0 comments on commit 0b58268

Please sign in to comment.