from flask import Blueprint, request, jsonify
import pymongo

client = pymongo.MongoClient('localhost',27017)
db = client.TCGA_Slide_DB
dsa = Blueprint('dsa', __name__)

@dsa.route('/annotations', methods=['POST','OPTIONS'])
def add_annotation():
	ants = db.annotations
	layers = db.layers
	layer_ids = []

	if request.json == None:
		return jsonify({"status": "failure", "code": 404})

	for a in request.json:
		layer_id = ants.insert_one(a).inserted_id
		layer_ids.append(layer_id)

	layers.insert_one({
		"createdBy": request.json[0]['createdBy'],
		"imageId": request.json[0]['imageId'],
		"layers": layer_ids
	})

	return jsonify({"status": "created", "code": 201})

@dsa.route('/annotations', methods=['GET','OPTIONS'])
def get_annotation():
	created_by = request.args.get("user_id")
	image_id = request.args.get("image_id")
	data = db.layers.find({"createdBy": created_by, "imageId": image_id}, {"_id": 0}).sort([("_id", 1)]).limit(1)[0]
	layers = []

	for layer_id in data['layers']:
		ant = db.annotations.find_one({"_id": layer_id}, {"_id": 0})
		layers.append(ant)

	return jsonify({"status": "success", "code": 200, "layers": layers})

@@ -824,16 +824,30 @@
 ge_id%22)%0A
+%09layers = %5B%5D%0A%0A
 %09data = 
@@ -952,26 +952,52 @@
 t(1)
-%5B0%5D%0A%09layers = %5B
+%0A%09%0A%09if data.count() %3E 0:%0A%09%09data = data%5B0
 %5D%0A%0A
+%09
 %09for
@@ -1025,16 +1025,17 @@
 yers'%5D:%0A
+%09
 %09%09ant = 
@@ -1089,16 +1089,17 @@
 d%22: 0%7D)%0A
+%09
 %09%09layers
@@ -1112,16 +1112,17 @@
 d(ant)%0A%0A
+%09
 %09return 
@@ -1183,8 +1183,62 @@
 ayers%7D)%0A
+%09%0A%09return jsonify(%7B%22status%22: %22success%22, %22code%22: 404%7D)%0A

Handle empty responses