-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhdl_pybus.py
45 lines (41 loc) · 1.32 KB
/
hdl_pybus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from datetime import timedelta, datetime
from bottle import run, debug, route, template, request, abort
import json, time
import pymongo
#################################
# Handlers to manage requests
#################################
# - add - Adds a deploy record
def add(connection):
db = connection.dtrack
postBody = request.body.read()
if not postBody:
abort(400, 'No data received')
entity = json.loads(postBody)
entity["_id"] = int(round(time.time() * 1000)) # current time in milli
for k in ["_id", "key", "action", "data"]:
if not entity.has_key(k):
abort(400, 'No %s specified' % k)
if (entity['key'] != "awesomeness"):
abort(400, 'Bad Key! Reporting')
try:
db['pyBus'].save(entity)
return "saved"
except Exception, e:
abort(500, e)
# - get_all - Gets all deploy records within a number of days (default 7),
# from an optionally specified user
def get_all(connection, numDays):
try:
db = connection.dtrack
#entities = db['pyBus'].find({created_on: {$gte: start, $lt: end}});
entities = db['pyBus'].find()
if not entities:
abort(404, 'No documents found')
returnData = []
for entity in entities:
entity['key'] = "Ssshhh.. its a secret"
returnData.append(entity)
return json.dumps(returnData)
except Exception, e:
print e