def recent_posts(body, number=3): from monblog import db posts = db.find("posts.files").sort([("uploadDate", -1)]).limit(number) rst = [] for post in posts: rst.append(body.format(id=post["_id"], title=post["metadata"]["title"])) return "\n".join(rst)
def export(parser): from monblog import db from monblog.common import json parser.add_argument("-i", "--id", dest="id", help="Post Id") parser.add_argument("-d", "--dir", dest="dir", help="Export dir") parser.add_argument("-n", "--num", type=int, dest="num", help="Number of posts to export") options = parser.parse_known_args()[0] filters = {} if options.id: filters["_id"] = ObjectId(options.id) posts = db.find("posts.files", filters).sort([("uploadDate", -1)]) if options.num > 0: posts = posts.limit(options.num) outputdir = os.getcwd() if options.dir: outputdir = os.path.join(outputdir, options.dir) if options.dir.startswith("/"): outputdir = options.dir for post in posts: path = os.path.join(outputdir, str(post["filename"])) data = ['$"metadata"$'] data.append(json.dumps(post['metadata'])) data.append('$"metadata"$') data.append(db.fs.get(ObjectId(post["_id"])).read()) with open(path, "w") as f: f.write("\n".join(data))