Add feature to rebuild blog index

pull/2/head
Noah 2015-07-20 12:16:52 -07:00
parent ea2ef9876d
commit a43db105b2
4 changed files with 56 additions and 15 deletions

View File

@ -41,7 +41,7 @@ def get_index():
# Index doesn't exist? # Index doesn't exist?
if not JsonDB.exists("blog/index"): if not JsonDB.exists("blog/index"):
return {} return rebuild_index()
db = JsonDB.get("blog/index") db = JsonDB.get("blog/index")
# Hide any private posts if we aren't logged in. # Hide any private posts if we aren't logged in.
@ -53,6 +53,43 @@ def get_index():
return db 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(): def get_categories():
"""Get the blog categories and their popularity.""" """Get the blog categories and their popularity."""
index = get_index() index = get_index()
@ -136,8 +173,8 @@ def post_entry(post_id, fid, epoch, author, subject, avatar, categories,
break break
fid = test fid = test
# Write the post. # DB body for the post.
JsonDB.commit("blog/entries/{}".format(post_id), dict( db = dict(
fid = fid, fid = fid,
ip = ip, ip = ip,
time = epoch or int(time.time()), time = epoch or int(time.time()),
@ -151,19 +188,13 @@ def post_entry(post_id, fid, epoch, author, subject, avatar, categories,
subject = subject, subject = subject,
format = format, format = format,
body = body, body = body,
)) )
# Write the post.
JsonDB.commit("blog/entries/{}".format(post_id), db)
# Update the index cache. # Update the index cache.
index[post_id] = dict( update_index(post_id, db, index)
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)
return post_id, fid return post_id, fid

View File

@ -6,6 +6,7 @@ from __future__ import unicode_literals, absolute_import
from flask import g, Blueprint, request, redirect, url_for, session, flash from flask import g, Blueprint, request, redirect, url_for, session, flash
import rophako.model.user as User import rophako.model.user as User
import rophako.model.blog as Blog
import rophako.model.tracking as Tracking import rophako.model.tracking as Tracking
from rophako.modules.account import validate_create_form from rophako.modules.account import validate_create_form
from rophako.utils import template, admin_required from rophako.utils import template, admin_required
@ -190,3 +191,11 @@ def rebuild_visitor_counts():
Tracking.rebuild_visitor_stats() Tracking.rebuild_visitor_stats()
flash("Visitor counts recalculated.") flash("Visitor counts recalculated.")
return redirect(url_for(".index")) 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"))

View File

@ -12,6 +12,7 @@
<h1>Maintenance Tasks</h1> <h1>Maintenance Tasks</h1>
<ul> <ul>
<li><a href="{{ url_for('admin.rebuild_blog_index') }}">Rebuild Blog Index</a></li>
<li><a href="{{ url_for('admin.rebuild_visitor_counts') }}">Rebuild Visitor Counts</a></li> <li><a href="{{ url_for('admin.rebuild_visitor_counts') }}">Rebuild Visitor Counts</a></li>
</ul> </ul>

View File

@ -38,7 +38,7 @@
<p> <p>
<div class="clear"> <div class="clear">
<strong>Categories:</strong> <strong>Categories:</strong>
{% if post["categories"]|length == 0 %} {% if post["categories"]|length == 0 or (post["categories"]|length == 1 and post["categories"][0] == "") %}
<a href="{{ url_for('blog.category', category=Uncategorized) }}">Uncategorized</a>{# TODO hardcoded name #} <a href="{{ url_for('blog.category', category=Uncategorized) }}">Uncategorized</a>{# TODO hardcoded name #}
{% else %} {% else %}
<ul class="blog-categories"> <ul class="blog-categories">