|
@ -7,7 +7,7 @@ from flask import jsonify |
|
|
from flask_marshmallow import Marshmallow |
|
|
from flask_marshmallow import Marshmallow |
|
|
|
|
|
|
|
|
class Posts: |
|
|
class Posts: |
|
|
def __init__(self, host=None, port=None): |
|
|
def __init__(self, user, password, host=None, port=None): |
|
|
if host is None: |
|
|
if host is None: |
|
|
host = "localhost" |
|
|
host = "localhost" |
|
|
if port is None: |
|
|
if port is None: |
|
@ -15,6 +15,10 @@ class Posts: |
|
|
|
|
|
|
|
|
self.client = couchdb.Server("http://%s:%s" % (host, port)) |
|
|
self.client = couchdb.Server("http://%s:%s" % (host, port)) |
|
|
|
|
|
|
|
|
|
|
|
self.client.credentials = (user, password) |
|
|
|
|
|
|
|
|
|
|
|
# FIXME check for pooling / concurrency issues |
|
|
|
|
|
|
|
|
self.db = self.client["blog"] |
|
|
self.db = self.client["blog"] |
|
|
|
|
|
|
|
|
def savepost(self, title="", content="", author="", category="programming", _id=False): |
|
|
def savepost(self, title="", content="", author="", category="programming", _id=False): |
|
@ -36,14 +40,14 @@ class Posts: |
|
|
return jsonify(self.db.save(doc)) |
|
|
return jsonify(self.db.save(doc)) |
|
|
|
|
|
|
|
|
def getpost(self, _id, category="programming"): |
|
|
def getpost(self, _id, category="programming"): |
|
|
results = self.db.iterview("blogPosts/blog-posts", 1, include_docs=True, startkey=[category, _id]) |
|
|
results = self.db.iterview("blogPosts/blog-posts", 1, include_docs=True, startkey=_id) |
|
|
return jsonify([result.doc for result in results][0]) |
|
|
return jsonify([result.doc for result in results][0]) |
|
|
|
|
|
|
|
|
def iterpost(self, endkey=False, startkey=False, category="programming"): |
|
|
def iterpost(self, endkey=False, startkey=False, category="programming"): |
|
|
if startkey and not endkey: |
|
|
if startkey and not endkey: |
|
|
results = self.db.iterview("blogPosts/blog-posts", 2, include_docs=True, startkey=[category, startkey]) |
|
|
results = self.db.iterview("blogPosts/blog-posts", 2, include_docs=True, startkey=startkey) |
|
|
elif endkey and not startkey: |
|
|
elif endkey and not startkey: |
|
|
results = self.db.iterview("blogPosts/blog-posts", 1, include_docs=True, endkey=[category, endkey]) |
|
|
results = self.db.iterview("blogPosts/blog-posts", 1, include_docs=True, endkey=endkey) |
|
|
else: |
|
|
else: |
|
|
results = self.db.iterview("blogPosts/blog-posts", 2, include_docs=True) |
|
|
results = self.db.iterview("blogPosts/blog-posts", 2, include_docs=True) |
|
|
|
|
|
|
|
@ -94,3 +98,16 @@ class Posts: |
|
|
print(e) |
|
|
print(e) |
|
|
return jsonify(False) |
|
|
return jsonify(False) |
|
|
|
|
|
|
|
|
|
|
|
def categories(self): |
|
|
|
|
|
return jsonify( |
|
|
|
|
|
[ |
|
|
|
|
|
c["key"][1] for c in |
|
|
|
|
|
self.db.view("blogPosts/categories", |
|
|
|
|
|
startkey=["category"], |
|
|
|
|
|
endkey=["category", {}], |
|
|
|
|
|
inclusive_end=False, |
|
|
|
|
|
group_level=2, |
|
|
|
|
|
group=True) |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|