From c583a69362f86fcc8e1b35a45a06dd8377d6308f Mon Sep 17 00:00:00 2001 From: mjfernez Date: Thu, 14 Oct 2021 20:14:53 -0400 Subject: Adds RSS auto-generation for files in 'site' This commit adds rss_generator.py which contains the main logic for indexing the site directory and generating a feed on startup. It serves as a sort of ad-hoc database which is accessed when /feed.xml is requested. Also corrects various typos, README nonsense, and expands the config options for RSS. Instances of './templates/site' have been replaced with the general BASE_DIR variable in the siteconfig. --- views.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'views.py') diff --git a/views.py b/views.py index dd37902..7a6f273 100644 --- a/views.py +++ b/views.py @@ -4,7 +4,7 @@ browsing to certain pages """ import os from flask import request, send_from_directory, abort -from flask import render_template, render_template_string +from flask import render_template, render_template_string, make_response from siteconfig import siteconfig from server import app, cache from view_functions import default_context, index_dir, is_hidden_path @@ -19,6 +19,8 @@ CONTENT_BLOCK = ( @app.route("/") @app.route("/site") +@app.route("/site/home.html") +@app.route("/site/index.html") @cache.cached() def home(): """ @@ -48,7 +50,7 @@ def render_file(path): """ if is_hidden_path(path): abort(404) - abs_path = "./templates/site/" + path + abs_path = siteconfig.BASE_DIR + path context = default_context() context.update( { @@ -68,7 +70,7 @@ def render_file(path): else: # not an html file, so don't render it return send_from_directory( - 'templates/site/', + siteconfig.BASE_DIR, path, mimetype=siteconfig.MIMETYPES.get( f".{ path.split('.')[-1] }", @@ -101,7 +103,7 @@ def send_file_from_site(path): as with `render_file`, send the raw file to the user """ return send_from_directory( - 'templates/site/', + siteconfig.BASE_DIR, path, mimetype=siteconfig.MIMETYPES.get( f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE @@ -122,3 +124,16 @@ def send_file_from_static(path): f".{ path.split('.')[-1] }", siteconfig.DEFAULT_MIMETYPE ), ) + + +@app.route("/feed.xml") +@cache.cached() +def render_rss_feed(): + context = { + 'config': siteconfig.rss_channel_config, + 'items': app.config['RSS_CHANNEL'], + } + feed = render_template("feed.xml", **context) + response = make_response(feed) + response.headers['Content-Type'] = siteconfig.MIMETYPES.get(".xml") + return response -- cgit v1.2.3