|
| 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) |
0 commit comments