A full featured blog in RiotJS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.8 KiB

#! /usr/bin/python
import couchdb
from couchdb.http import ResourceConflict, ResourceNotFound
from flask import jsonify
from flask_marshmallow import Marshmallow
class Posts:
def __init__(self, host=None, port=None):
if host is None:
host = "localhost"
if port is None:
port = "5984"
self.client = couchdb.Server("http://%s:%s" % (host, port))
self.db = self.client["blog"]
def savepost(self, title="", content="", author="", _id=False):
if _id:
doc = self.db[_id]
doc["title"] = title
doc["content"] = content
doc["author"] = author
else:
doc = {
"title" : title,
"content" : content,
"author" : author
}
7 years ago
print("post was saved %s" % doc)
return jsonify(self.db.save(doc))
7 years ago
def getposts(self, limit, start):
result = self.db.iterview("blogPosts/blog-posts", 10, include_docs=True, limit=limit, skip=start)
return jsonify(list(result))
def allposts(self):
result = self.db.iterview("blogPosts/blog-posts", 10, include_docs=True)
posts = []
for item in result:
posts.append({
"_id" : item.doc["_id"],
"title" : item.doc["title"],
"author" : item.doc["author"]
})
return jsonify(posts)
def getpost(self, _id):
return jsonify(self.db[_id])
def delete(self, _id):
doc = self.db[_id]
try:
self.db.delete(doc)
return jsonify(True)
except (ResourceNotFound, ResourceConflict) as e:
print(e)
return jsonify(False)