def newMessage():
    data = request.data
    db = CouchdbUtils().get_db()
    dataDict = simplejson.loads(data)
    dataDict.update({"type": "message"})
    message = jsonify(dataDict)
    doc_id, doc_rev = db.save(dataDict)
    if doc_rev is None:
        res = {"status": "error"}
    else:
        res = {"status": "success"}
    return jsonify(results=res), 201
def getContentShareBySlug(slug):
    db = CouchdbUtils().get_db()
    map_fun = '''function(doc) {
        if(doc.slug=="'''+slug+'''"){
            emit(doc.type, doc);
        }
    }'''
    results = db.query(map_fun, limit=1)
    docs = []
    for body in results:
        docs.append(body.value)
    return simplejson.dumps(docs)
def getAllShares():
    db = CouchdbUtils().get_db()
    map_fun = '''function(doc) {
        if(doc.type=="post"){
            emit(doc.type, doc);
        }
    }'''
    results = db.query(map_fun, descending=True)
    docs = []
    for body in results:
        docs.append(body.value)
    return simplejson.dumps(docs)
def index():
    db = CouchdbUtils().get_db()
    map_fun = '''function(doc) {
        if(doc.type=="post"){
            emit(doc.type, doc);
        }
    }'''
    results = db.query(map_fun)
    docs = []
    for body in results:
        docs.append(body.value)
    log.info("length:"+len(docs).__str__())
    return render_template('index.html', documents=docs)
def getLastShares():
    db = CouchdbUtils().get_db()
    map_fun = '''function(doc) {
        if(doc.type=="post"){
            emit(doc.type, doc);
        }
    }'''
    # I promise I will not post more than 8 times per day
    results = db.query(map_fun, limit=8, descending=True)
    docs = []
    for body in results:
        docs.append(body.value)
    return simplejson.dumps(docs)
def getPostBySlug(slug):
    db = CouchdbUtils().get_db()
    map_fun = '''function(doc) {
        if(doc.type=="post" && doc.slug=="'''+slug+'''"){
            emit(doc.type, doc);
        }
    }'''
    results = db.query(map_fun)
    docs = []
    for body in results:
        docs.append(body.value)
    if len(results) == 0:
        return render_template('404.html')
    return render_template('share.html')