diff options
-rw-r--r-- | apioforum/__init__.py | 7 | ||||
-rw-r--r-- | apioforum/forum.py | 27 | ||||
-rw-r--r-- | apioforum/templates/rss.xml | 17 | ||||
-rw-r--r-- | apioforum/thread.py | 14 | ||||
-rw-r--r-- | apioforum/util.py (renamed from apioforum/fuzzy.py) | 4 |
5 files changed, 60 insertions, 9 deletions
diff --git a/apioforum/__init__.py b/apioforum/__init__.py index 54d18c3..ab96d2a 100644 --- a/apioforum/__init__.py +++ b/apioforum/__init__.py @@ -31,15 +31,16 @@ def create_app(): from . import thread app.register_blueprint(thread.bp) + from .util import fuzzy, rss_datetime + app.jinja_env.filters["fuzzy"]=fuzzy + app.jinja_env.filters["rss_datetime"]=rss_datetime + from . import admin app.register_blueprint(admin.bp) from . import user app.register_blueprint(user.bp) - from .fuzzy import fuzzy - app.jinja_env.filters['fuzzy']=fuzzy - @app.context_processor def path_for_next(): p = request.path diff --git a/apioforum/forum.py b/apioforum/forum.py index 98c71f7..a7f4719 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -3,11 +3,12 @@ from flask import ( Blueprint, render_template, request, - g, redirect, url_for, flash + g, redirect, url_for, flash, Response ) from .db import get_db from .mdrender import render +from .thread import post_jump bp = Blueprint("forum", __name__, url_prefix="/") @@ -87,4 +88,26 @@ def search(): display_thread_id[ix] = False last_thread = result["thread"] rendered_posts = [render(q['content']) for q in results] - return render_template("search_results.html", results=results, query=query, rendered_posts=rendered_posts, display_thread_id=display_thread_id) + return render_template("search_results.html", + results=results, + query=query, + rendered_posts=rendered_posts, + display_thread_id=display_thread_id + ) + +@bp.route("/rss") +def rss_feed(): + db = get_db() + print(request.host_url) + items = db.execute("SELECT * FROM posts ORDER BY updated DESC LIMIT 50") + items = [ { + **item, + "rendered": render(item["content"]), + "link": request.base_url + post_jump(item["thread"], item["id"]), + "updated": item["updated"] or item["created"] + } for item in items ] + return Response(render_template("rss.xml", + title="New posts feed", + description="The latest posts on the Apioforum", + link=request.base_url, items=items), + mimetype="text/xml") diff --git a/apioforum/templates/rss.xml b/apioforum/templates/rss.xml new file mode 100644 index 0000000..8ff7a72 --- /dev/null +++ b/apioforum/templates/rss.xml @@ -0,0 +1,17 @@ +<rss version="2.0"> + <channel> + <title>{{title}}</title> + <link>{{link}}</link> + <description>{{description}}</description> + <generator>Apioforum internal RSS generation</generator> + {% for item in items %} + <item> + <description>{{item.rendered}}</description> + <pubDate>{{item.updated | rss_datetime}}</pubDate> + <link>{{item.link}}</link> + <author>{{item.author}}</author> + <guid>{{item.id}}</guid> + </item> + {% endfor %} + </channel> +</rss>
\ No newline at end of file diff --git a/apioforum/thread.py b/apioforum/thread.py index 292fd21..6f3386b 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -2,7 +2,7 @@ from flask import ( Blueprint, render_template, abort, request, g, redirect, - url_for, flash + url_for, flash, Response ) from .db import get_db from .mdrender import render @@ -108,7 +108,6 @@ def edit_post(post_id): else: flash(err) return render_template("edit_post.html",post=post) - @bp.route("/<int:thread_id>/config",methods=["GET","POST"]) def config_thread(thread_id): db = get_db() @@ -157,7 +156,14 @@ def config_thread(thread_id): flash(e) else: return redirect(url_for("thread.view_thread",thread_id=thread_id)) + return render_template("config_thread.html", thread=thread,thread_tags=thread_tags,avail_tags=avail_tags) - return render_template("config_thread.html", thread=thread,thread_tags=thread_tags,avail_tags=avail_tags) - +@bp.route("/<int:thread_id>/rss") +def rss_feed(thread_id): + db = get_db() + thread = db.execute("SELECT * FROM threads WHERE id = ?", (thread_id,)).fetchone() + items = db.execute("SELECT * FROM posts WHERE thread = ? ORDER BY updated DESC LIMIT 50", (thread_id,)) + items = [ { **item, "rendered": render(item["content"]), "link": request.base_url + post_jump(item["thread"], item["id"]), "updated": item["updated"] or item["created"] } for item in items ] + return Response(render_template("rss.xml", description=f"Posts in thread {thread_id}", title=f'''{thread['title']} - Apioforum''', + link=request.base_url.rstrip("/rss"), items=items), mimetype="text/xml") diff --git a/apioforum/fuzzy.py b/apioforum/util.py index 8396b8f..6384383 100644 --- a/apioforum/fuzzy.py +++ b/apioforum/util.py @@ -9,6 +9,7 @@ units = ( ) from datetime import datetime, timedelta, timezone +import email.utils def fuzzy(seconds, ago=False): if isinstance(seconds, timedelta): @@ -33,3 +34,6 @@ def fuzzy(seconds, ago=False): if not buf: return "now" return fmt.format(buf) + +def rss_datetime(dt): + return email.utils.format_datetime(dt.replace(tzinfo=timezone.utc))
\ No newline at end of file |