diff --git a/rophako/model/blog.py b/rophako/model/blog.py index b1c3cbd..b69456f 100644 --- a/rophako/model/blog.py +++ b/rophako/model/blog.py @@ -41,7 +41,7 @@ def get_index(): # Index doesn't exist? if not JsonDB.exists("blog/index"): - return {} + return rebuild_index() db = JsonDB.get("blog/index") # Hide any private posts if we aren't logged in. @@ -53,6 +53,43 @@ def get_index(): return db +def rebuild_index(): + """Rebuild the index.json if it goes missing.""" + index = {} + + entries = JsonDB.list_docs("blog/entries") + for post_id in entries: + db = JsonDB.get("blog/entries/{}".format(post_id)) + update_index(post_id, db, index, False) + + JsonDB.commit("blog/index", index) + return index + + +def update_index(post_id, post, index=None, commit=True): + """Update a post's meta-data in the index. This is also used for adding a + new post to the index for the first time. + + * post_id: The ID number for the post + * post: The DB object for a blog post + * index: If you already have the index open, you can pass it here + * commit: Write the DB after updating the index (default True)""" + if index is None: + index = get_index() + + index[post_id] = dict( + fid = post["fid"], + time = post["time"] or int(time.time()), + categories = post["categories"], + sticky = False, # TODO + author = post["author"], + privacy = post["privacy"] or "public", + subject = post["subject"], + ) + if commit: + JsonDB.commit("blog/index", index) + + def get_categories(): """Get the blog categories and their popularity.""" index = get_index() @@ -136,8 +173,8 @@ def post_entry(post_id, fid, epoch, author, subject, avatar, categories, break fid = test - # Write the post. - JsonDB.commit("blog/entries/{}".format(post_id), dict( + # DB body for the post. + db = dict( fid = fid, ip = ip, time = epoch or int(time.time()), @@ -151,19 +188,13 @@ def post_entry(post_id, fid, epoch, author, subject, avatar, categories, subject = subject, format = format, body = body, - )) + ) + + # Write the post. + JsonDB.commit("blog/entries/{}".format(post_id), db) # Update the index cache. - index[post_id] = dict( - fid = fid, - time = epoch or int(time.time()), - categories = categories, - sticky = False, # TODO - author = author, - privacy = privacy or "public", - subject = subject, - ) - JsonDB.commit("blog/index", index) + update_index(post_id, db, index) return post_id, fid diff --git a/rophako/modules/admin/__init__.py b/rophako/modules/admin/__init__.py index 2a39a27..2772359 100644 --- a/rophako/modules/admin/__init__.py +++ b/rophako/modules/admin/__init__.py @@ -6,6 +6,7 @@ from __future__ import unicode_literals, absolute_import from flask import g, Blueprint, request, redirect, url_for, session, flash import rophako.model.user as User +import rophako.model.blog as Blog import rophako.model.tracking as Tracking from rophako.modules.account import validate_create_form from rophako.utils import template, admin_required @@ -190,3 +191,11 @@ def rebuild_visitor_counts(): Tracking.rebuild_visitor_stats() flash("Visitor counts recalculated.") return redirect(url_for(".index")) + +@mod.route("/maint/rebuild_blog_index") +@admin_required +def rebuild_blog_index(): + """Rebuild the blog index.""" + Blog.rebuild_index() + flash("Blog index rebuilt.") + return redirect(url_for(".index")) diff --git a/rophako/modules/admin/templates/admin/index.html b/rophako/modules/admin/templates/admin/index.html index 5c64417..904f6ae 100644 --- a/rophako/modules/admin/templates/admin/index.html +++ b/rophako/modules/admin/templates/admin/index.html @@ -12,6 +12,7 @@

Maintenance Tasks

diff --git a/rophako/modules/blog/templates/blog/entry.inc.html b/rophako/modules/blog/templates/blog/entry.inc.html index dadc942..d67d88a 100644 --- a/rophako/modules/blog/templates/blog/entry.inc.html +++ b/rophako/modules/blog/templates/blog/entry.inc.html @@ -38,7 +38,7 @@

Categories: - {% if post["categories"]|length == 0 %} + {% if post["categories"]|length == 0 or (post["categories"]|length == 1 and post["categories"][0] == "") %} Uncategorized{# TODO hardcoded name #} {% else %}