Skip to content

Commit 1ba994b

Browse files
authored
Merge pull request #388 from knatten/rss-atom
Rss atom
2 parents 88a1b15 + 0244780 commit 1ba994b

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

cppquiz/local_settings_example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from . import settings
22

3+
settings.SITE_URL = "https://cppquiz.org"
34
settings.DEBUG = True
45
settings.ALLOWED_HOSTS += ['127.0.0.1']
56
settings.ADMINS = (

deployment/dreamhost/cronjobs/dump_published.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
source $HOME/sites/cppquiz.org/venv/bin/activate || exit $?
44
python $HOME/sites/cppquiz.org/cppquiz/manage.py dump_published_questions > $HOME/static.cppquiz.org/published.json || exit $?
5+
python $HOME/sites/cppquiz.org/cppquiz/manage.py generate_feeds rss > $HOME/static.cppquiz.org/rss.xml || exit $?
6+
python $HOME/sites/cppquiz.org/cppquiz/manage.py generate_feeds atom > $HOME/static.cppquiz.org/atom.xml || exit $?
57

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import datetime
2+
3+
from django.core.management.base import BaseCommand
4+
from django.urls import reverse
5+
6+
7+
from quiz.util import get_published_questions
8+
from cppquiz import settings
9+
10+
rss_header = """<?xml version="1.0" encoding="UTF-8" ?>
11+
<rss version="2.0">
12+
<channel>
13+
<title>CppQuiz - New Questions</title>
14+
<link>https://cppquiz.org</link>
15+
<description>Latest C++ quiz questions from CppQuiz.org</description>
16+
<language>en-us</language>
17+
<lastBuildDate>{{last_build_date}}</lastBuildDate>
18+
"""
19+
20+
rss_question = """
21+
<item>
22+
<title>Question {{id}}</title>
23+
<link>{{url}}</link>
24+
<description>Question {{id}} on CppQuiz.org</description>
25+
<pubDate>{{pubdate}}</pubDate>
26+
<guid>{{url}}</guid>
27+
</item>
28+
"""
29+
30+
rss_footer = """
31+
</channel>
32+
</rss>
33+
"""
34+
35+
36+
atom_header = """<?xml version="1.0" encoding="UTF-8"?>
37+
<feed xmlns="http://www.w3.org/2005/Atom">
38+
<title>CppQuiz - New Questions</title>
39+
<link href="https://cppquiz.org/" />
40+
<updated>{{last_build_date}}</updated>
41+
<author>
42+
<name>CppQuiz</name>
43+
</author>
44+
<id>https://cppquiz.org/</id>
45+
"""
46+
47+
atom_question = """
48+
<entry>
49+
<title>Question {{id}}</title>
50+
<link href="{{url}}" />
51+
<id>{{url}}</id>
52+
<updated>{{pubdate}}</updated>
53+
<summary>Question {{id}} on CppQuiz.org</summary>
54+
</entry>
55+
"""
56+
57+
atom_footer = """
58+
</feed>
59+
"""
60+
61+
62+
class Command(BaseCommand):
63+
help = "Generate an RSS or Atom feed for published questions."
64+
post_count = 20
65+
66+
def add_arguments(self, parser):
67+
parser.add_argument(
68+
"format", choices=["rss", "atom"],
69+
help="Choose feed format: 'rss' or 'atom'"
70+
)
71+
72+
def handle(self, *args, **options):
73+
feed_format = options["format"]
74+
header = atom_header if feed_format == "atom" else rss_header
75+
question = atom_question if feed_format == "atom" else rss_question
76+
footer = atom_footer if feed_format == "atom" else rss_footer
77+
time_format = "%Y-%m-%dT%H:%M:%SZ" if feed_format == "atom" else "%a, %d %b %Y %H:%M:%S GMT"
78+
79+
last_build_date = datetime.datetime.now().strftime(time_format)
80+
print(header.replace("{{last_build_date}}", last_build_date))
81+
82+
for q in get_published_questions().order_by('-publish_time')[:self.post_count]:
83+
url = settings.SITE_URL + reverse('quiz:question', args=[q.pk])
84+
print(question.replace("{{id}}", str(q.id)).replace("{{url}}", url).replace(
85+
"{{pubdate}}", q.publish_time.strftime(time_format)))
86+
87+
print(footer)

templates/base.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<link type="text/css" rel="stylesheet" href="{{STATIC_URL}}highlight.css">
99
<link type="text/css" rel="stylesheet" href="{{STATIC_URL}}quiz.css?v=5">
1010
<link rel="shortcut icon" href="{{STATIC_URL}}favicon.png">
11+
<link rel="alternate" type="application/rss+xml" title="CppQuiz RSS" href="{{STATIC_URL}}rss.xml">
12+
<link rel="alternate" type="application/atom+xml" title="CppQuiz Atom" href="{{STATIC_URL}}atom.xml">
1113

1214
<!-- Google tag (gtag.js) -->
1315
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BSF86HNJZY"></script>
@@ -107,6 +109,8 @@ <h1><a href="/">C++ Quiz</a></h1>
107109
<a href="https://github.com/knatten/cppquiz/blob/master/CODE_OF_CONDUCT.md">CoC</a> |
108110
<a href="https://mastodon.online/@cppquiz"><img src="{{STATIC_URL}}/mastodon.png" height="15" alt="Mastodon"> Mastodon</a> |
109111
<a href="https://bsky.app/profile/cppquiz.bsky.social"><img src="{{STATIC_URL}}/bluesky.png" height="15" alt="Bluesky"> Bluesky</a> |
112+
<a href="{{STATIC_URL}}rss.xml">RSS</a> |
113+
<a href="{{STATIC_URL}}atom.xml">Atom</a> |
110114
<a href="https://github.com/knatten/cppquiz"> GitHub</a> |
111115
© <a href="http://knatten.org">Anders Schau Knatten</a> {% now "Y"%}.
112116
</p>

0 commit comments

Comments
 (0)