コード例 #1
0
ファイル: ledger.py プロジェクト: dalanmiller/ledger
def log_dump():
	apps_logs = {}
	for appname in dbh.collection_names():
		# serialize all logs for 'appname' app
		apps_logs[appname] = [x for x in getattr(dbh, appname).find()]

	return jsonify( **apps_logs )
コード例 #2
0
ファイル: ledger.py プロジェクト: dalanmiller/ledger
def app_log(app_name = None):
	"""
	This route will accept POST and GET requests for json
	"""
	if app_name and request.method == 'POST': #Code to handle incoming JSON 

		if request.json or request.headers['Content-Type'] == 'application/json':
			
			if request.json: #Not sure if this request object .json always works
				inc_json = request.json
			else:
				inc_json = json.loads(request.data) #Else grab .json from normal request.data

			dbh.__getattr__(app_name).insert(inc_json) #Put the incoming json into the db.
			return jsonify(dict(Success=True))
		else:
			
			return "Content-Type header not set to application/json"

	elif app_name: #Displays logs for a given app name
		app = dbh.__getattr__(app_name)
		# get the full log history
		logs = [x for x in app.find()]

		if 'json' in request.url or request.headers['Content-Type'] == 'application/json': #If true return json version

			return jsonify( logs=logs )
			
		else: #Return html version
			
			headers = set()
			for doc in logs:
				for key in doc.iterkeys():
					headers.add(key)
			headers = sorted(list(headers))

			return render_template(
				'log.html', 
				logs = logs, 
				headers = headers,
				appname = app_name)