diff --git a/cppquiz/local_settings_example.py b/cppquiz/local_settings_example.py index 939054a..a908742 100644 --- a/cppquiz/local_settings_example.py +++ b/cppquiz/local_settings_example.py @@ -1,5 +1,6 @@ from . import settings +settings.SITE_URL = "https://cppquiz.org" settings.DEBUG = True settings.ALLOWED_HOSTS += ['127.0.0.1'] settings.ADMINS = ( diff --git a/deployment/dreamhost/cronjobs/dump_published.sh b/deployment/dreamhost/cronjobs/dump_published.sh index 4650475..aa9f672 100755 --- a/deployment/dreamhost/cronjobs/dump_published.sh +++ b/deployment/dreamhost/cronjobs/dump_published.sh @@ -2,4 +2,6 @@ source $HOME/sites/cppquiz.org/venv/bin/activate || exit $? python $HOME/sites/cppquiz.org/cppquiz/manage.py dump_published_questions > $HOME/static.cppquiz.org/published.json || exit $? +python $HOME/sites/cppquiz.org/cppquiz/manage.py generate_feeds rss > $HOME/static.cppquiz.org/rss.xml || exit $? +python $HOME/sites/cppquiz.org/cppquiz/manage.py generate_feeds atom > $HOME/static.cppquiz.org/atom.xml || exit $? diff --git a/quiz/management/commands/generate_feeds.py b/quiz/management/commands/generate_feeds.py new file mode 100644 index 0000000..335f7f6 --- /dev/null +++ b/quiz/management/commands/generate_feeds.py @@ -0,0 +1,87 @@ +import datetime + +from django.core.management.base import BaseCommand +from django.urls import reverse + + +from quiz.util import get_published_questions +from cppquiz import settings + +rss_header = """ + + + CppQuiz - New Questions + https://cppquiz.org + Latest C++ quiz questions from CppQuiz.org + en-us + {{last_build_date}} +""" + +rss_question = """ + + Question {{id}} + {{url}} + Question {{id}} on CppQuiz.org + {{pubdate}} + {{url}} + +""" + +rss_footer = """ + + +""" + + +atom_header = """ + + CppQuiz - New Questions + + {{last_build_date}} + + CppQuiz + + https://cppquiz.org/ +""" + +atom_question = """ + + Question {{id}} + + {{url}} + {{pubdate}} + Question {{id}} on CppQuiz.org + +""" + +atom_footer = """ + +""" + + +class Command(BaseCommand): + help = "Generate an RSS or Atom feed for published questions." + post_count = 20 + + def add_arguments(self, parser): + parser.add_argument( + "format", choices=["rss", "atom"], + help="Choose feed format: 'rss' or 'atom'" + ) + + def handle(self, *args, **options): + feed_format = options["format"] + header = atom_header if feed_format == "atom" else rss_header + question = atom_question if feed_format == "atom" else rss_question + footer = atom_footer if feed_format == "atom" else rss_footer + time_format = "%Y-%m-%dT%H:%M:%SZ" if feed_format == "atom" else "%a, %d %b %Y %H:%M:%S GMT" + + last_build_date = datetime.datetime.now().strftime(time_format) + print(header.replace("{{last_build_date}}", last_build_date)) + + for q in get_published_questions().order_by('-publish_time')[:self.post_count]: + url = settings.SITE_URL + reverse('quiz:question', args=[q.pk]) + print(question.replace("{{id}}", str(q.id)).replace("{{url}}", url).replace( + "{{pubdate}}", q.publish_time.strftime(time_format))) + + print(footer) diff --git a/templates/base.html b/templates/base.html index 14ce783..a68f093 100644 --- a/templates/base.html +++ b/templates/base.html @@ -8,6 +8,8 @@ + + @@ -107,6 +109,8 @@

C++ Quiz

CoC | Mastodon Mastodon | Bluesky Bluesky | +RSS | +Atom | GitHub | © Anders Schau Knatten {% now "Y"%}.