Skip to content

Commit

Permalink
Merge pull request #363 from knatten/twitter-x
Browse files Browse the repository at this point in the history
Twitter x
  • Loading branch information
knatten authored Sep 22, 2024
2 parents 90af545 + bf5a0cc commit 4f85cea
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 28 deletions.
20 changes: 10 additions & 10 deletions quiz/management/commands/auto_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
class Command(BaseCommand):

def add_arguments(self, parser):
parser.add_argument('--skip-tweet', action='store_true')
parser.add_argument('--skip-socials', action='store_true')

def handle(self, *args, **options):
skip_tweet = options["skip_tweet"]
skip_socials = options["skip_socials"]

for q in Question.objects.filter(state='SCH', publish_time__lte=timezone.now()):
print(f"Publishing question {q}")
q.state = 'PUB'
q.save()
if (q.tweet_text):
print(f"Posting to Twitter: '{q.tweet_text}'")
if skip_tweet:
print("Skipping!")
if (q.socials_text):
if skip_socials:
print("Skipping posting to social media!")
else:
self.tweet(q.tweet_text)
self.post_to_x(q.socials_text)

def tweet(self, content):
def post_to_x(self, content):
print(f"Posting to X: '{content}'")
try:
secrets_file = Path.home() / ".cppquiz-secrets.json"
with secrets_file.open() as f:
Expand All @@ -41,7 +41,7 @@ def tweet(self, content):
response = client.create_tweet(
text=content
)
print(f"Posted https://twitter.com/user/status/{response.data['id']}")
print(f"Posted https://x.com/user/status/{response.data['id']}")
except Exception as e:
print(f"Failed to tweet '{content}' due to exception '{e}'")
print(f"Failed to post '{content}' to X due to exception '{e}'")
sys.exit(1)
2 changes: 1 addition & 1 deletion quiz/management/commands/text_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def get_result_display(question):
- Other ideas for help are also welcome, please get in touch (see [Questions](#questions) below).
### Questions
If you have any questions, either file an issue in this repo, contact [@knatten on Twitter](https://twitter.com/knatten), or email me at anders@knatten.org.
If you have any questions, either file an issue in this repo, contact [@knatten on X](https://x.com/knatten), or email me at anders@knatten.org.
"""

pull_request_template = """ Fixes #<issue number> (Make sure to enter the issue number, not the question number! If this PR fixes multiple issues, just duplicate this line.)
Expand Down
18 changes: 18 additions & 0 deletions quiz/migrations/0023_rename_tweet_text_question_socials_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.15 on 2024-09-22 14:19

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('quiz', '0022_alter_question_result_alter_usersanswer_result'),
]

operations = [
migrations.RenameField(
model_name='question',
old_name='tweet_text',
new_name='socials_text',
),
]
6 changes: 3 additions & 3 deletions quiz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class Question(models.Model):
help_text='This question is reserved for an event, do not publish yet')
reservation_message = models.CharField(blank=True, max_length=100,
help_text='Which event the question is reserved for')
tweet_text = models.CharField(blank=True, max_length=280,
help_text='What to tweet when question gets posted on Twitter')
socials_text = models.CharField(blank=True, max_length=280,
help_text='What to post when question gets posted on social media')

def __str__(self):
return str(self.pk)
Expand All @@ -69,7 +69,7 @@ def clean(self):
raise ValidationError(f'Cannot {verbs[self.state]} a question without a difficulty setting')
if self.state in ('PUB', 'SCH') and self.reserved:
raise ValidationError(f'Cannot {verbs[self.state]} a reserved question')
if self.tweet_text and not re.search("https?://", self.tweet_text):
if self.socials_text and not re.search("https?://", self.socials_text):
raise ValidationError('Tweets must contain a url!')

def save(self, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions quiz/static/quiz.css
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ h4.quiz_mode
{
color:#999;
}
.twitter-follow {
.x-follow {
background-color: #1da1f2;
border-radius: 3px;
padding: 1px 8px 1px 6px;
font-family: "Helvetica Neue", Arial, sans-serif;
}
#footer a.twitter-follow {
#footer a.x-follow {
color: #fff;
}
#info_page {
Expand Down
File renamed without changes
6 changes: 3 additions & 3 deletions quiz/tests/unit/test_auto_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ class AutoPublishTest(TestCase):
def test_publishes_scheduled_question_from_the_past(self):
past = timezone.now() - timedelta(hours=1)
[q] = create_questions(1, state="SCH", publish_time=past)
call_command("auto_publish", "--skip-tweet")
call_command("auto_publish", "--skip-socials")
self.assertEqual("PUB", Question.objects.get(pk=q.pk).state)

def test_does_not_publish_unscheduled_question_from_the_past(self):
past = timezone.now() - timedelta(hours=1)
[q] = create_questions(1, state="ACC", publish_time=past)
call_command("auto_publish", "--skip-tweet")
call_command("auto_publish", "--skip-socials")
self.assertEqual("ACC", Question.objects.get(pk=q.pk).state)

def test_does_not_publish_scheduled_question_from_the_future(self):
future = timezone.now() + timedelta(hours=1)
[q] = create_questions(1, state="SCH", publish_time=future)
call_command("auto_publish", "--skip-tweet")
call_command("auto_publish", "--skip-socials")
self.assertEqual("SCH", Question.objects.get(pk=q.pk).state)
8 changes: 4 additions & 4 deletions quiz/tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def test_questions_get_a_random_preview_key(self):
q2 = Question.objects.create()
self.assertNotEqual(q.preview_key, q2.preview_key)

def test_requires_url_in_tweet_text(self):
q = Question(state="SCH", hint='hint', tweet_text="hi", difficulty=1)
def test_requires_url_in_socials_text(self):
q = Question(state="SCH", hint='hint', socials_text="hi", difficulty=1)
with self.assertRaises(ValidationError) as cm:
q.save()
self.assertIn('Tweets must contain a url!', str(cm.exception))
Question(state="SCH", hint='hint', tweet_text="See http://example.com", difficulty=1).save()
Question(state="SCH", hint='hint', tweet_text="See https://example.com", difficulty=1).save()
Question(state="SCH", hint='hint', socials_text="See http://example.com", difficulty=1).save()
Question(state="SCH", hint='hint', socials_text="See https://example.com", difficulty=1).save()
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ <h1><a href="/">C++ Quiz</a></h1>
<a href="https://github.com/knatten/cppquiz/blob/master/CODE_OF_CONDUCT.md">CoC</a> |
<a href="https://mastodon.online/@cppquiz"><img src="{{STATIC_URL}}/mastodon.png" height="15" alt="Mastodon"> Mastodon</a> |
<a href="https://bsky.app/profile/cppquiz.bsky.social"><img src="{{STATIC_URL}}/bluesky.png" height="15" alt="Bluesky"> Bluesky</a> |
<a href="https://twitter.com/intent/follow?screen_name=CppQuiz" class="twitter-follow"><img src="{{STATIC_URL}}/twitter.png" height="9" alt="Twitter"> Twitter</a> |
<a href="https://x.com/intent/follow?screen_name=CppQuiz" class="x-follow"><img src="{{STATIC_URL}}/x.png" height="9" alt="X"> X</a> |
© <a href="http://knatten.org">Anders Schau Knatten</a> {% now "Y"%}.
</p>
<p>
Expand Down
4 changes: 2 additions & 2 deletions templates/quiz/finished.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ <h3>All right!</h3>
<p>You finished with {{quiz_in_progress.score|floatformat:2}} out of {{quiz_in_progress.get_total_nof_questions|floatformat:1}} <abbr title="1 point per correct question, but -0.5 points if you used a hint, and /2 for each wrong attempt.".>possible</abbr> points.</p>

<p>
Now <a href="https://twitter.com/intent/tweet?hashtags=cpp,cplusplus&text=I got {{quiz_in_progress.score|floatformat:2}} of {{quiz_in_progress.get_total_nof_questions}} points on this @CppQuiz https://{{request.get_host}}{{request.get_full_path}} ! Can you match that?">boast about it on Twitter</a>!
Now <a href="https://x.com/intent/tweet?hashtags=cpp,cplusplus&text=I got {{quiz_in_progress.score|floatformat:2}} of {{quiz_in_progress.get_total_nof_questions}} points on this @CppQuiz https://{{request.get_host}}{{request.get_full_path}} ! Can you match that?">boast about it on X</a>!
<br>
(This link takes you to Twitter.com, we would never post on your behalf).
(This link takes you to x.com, we would never post on your behalf).
</p>

<p>
Expand Down
4 changes: 2 additions & 2 deletions templates/quiz/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<a href="/">Continue quiz</a>
<h2>About</h2>
<p>
C++ Quiz is written by <a href="http://knatten.org">Anders Schau Knatten</a> with valuable input from <a href="http://www.pvv.org/~oma/cv.html">Olve Maudal</a>, <a href="http://fcacciola.50webs.com/">Fernando Cacciola</a>, other members of the <a href="http://accu.org/">ACCU</a>, and several other contributors. If you discover any errors, or would like to contribute, please contact me at anders AT knatten.org, on <a href="https://twitter.com/CppQuiz">Twitter</a>, or on <a href="https://github.com/knatten/cppquiz/issues">GitHub</a>.
C++ Quiz is written by <a href="http://knatten.org">Anders Schau Knatten</a> with valuable input from <a href="http://www.pvv.org/~oma/cv.html">Olve Maudal</a>, <a href="http://fcacciola.50webs.com/">Fernando Cacciola</a>, other members of the <a href="http://accu.org/">ACCU</a>, and several other contributors. If you discover any errors, or would like to contribute, please contact me at anders AT knatten.org, on <a href="https://x.com/CppQuiz">X</a>, or on <a href="https://github.com/knatten/cppquiz/issues">GitHub</a>.
</p>
<h2>Help/FAQ</h2>
<h3>What is this?</h3>
Expand All @@ -21,7 +21,7 @@ <h3>What features are planned?</h3>
<p>See <a href="https://github.com/knatten/cppquiz/issues?state=open">open issues on GitHub</a>.</p>
</ol>
<h3>How can I help?</h3>
<p>If you know C++, by adding more questions, or joining our mailing-list <a href="mailto:cppquiz-discuss@cppquiz.org">cppquiz-discuss@cppquiz.org</a>. If you know Python/Django/JavaScript, by extending functionality. If you know web-design, by improving the design. Please contact me at anders AT knatten.org or <a href="https://twitter.com/knatten">Twitter</a>.</p>
<p>If you know C++, by adding more questions, or joining our mailing-list <a href="mailto:cppquiz-discuss@cppquiz.org">cppquiz-discuss@cppquiz.org</a>. If you know Python/Django/JavaScript, by extending functionality. If you know web-design, by improving the design. Please contact me at anders AT knatten.org or <a href="https://x.com/knatten">X</a>.</p>

<h3 id="cookies">Cookies</h3>
<p>
Expand Down

0 comments on commit 4f85cea

Please sign in to comment.