Skip to content

Commit

Permalink
adds SA to /message. adds 405 error handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhoww committed Mar 28, 2016
1 parent 5b571cb commit 3621589
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ endpoint | usage
*/users/:id* | *GET* one, *PUT* to update, *DELETE* one
*/devices* | *GET* all, *POST* new
*/devices/:id* | *GET* one, *PUT* to update, *DELETE* one
*/messages/:id* | *GET* for user or device

## attributions

Expand Down
45 changes: 16 additions & 29 deletions syncer/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,14 @@ def get_conf(p):
resp['devices'].append(tmp)
return resp

def get_conf_alt(p):
user = models.User.query.get(p['userid'])
devices = models.Device.query.filter_by(user=user).all()
resp = {}
resp['name'] = user.name
resp['devices'] = []
resp['commands'] = []
resp['alerts'] = []
for d in devices:
tmp = {}
tmp['id'] = d.id
tmp['name'] = d.name
tmp['number'] = d.number
resp['devices'].append(tmp)
for c in d.commands():
tmp = {}
tmp['commandname'] = c
tmp['deviceid'] = d.id
tmp['string'] = d.commands()[c]
resp['commands'].append(tmp)
for c in d.alerts():
tmp = {}
tmp['alertname'] = c
tmp['deviceid'] = d.id
tmp['string'] = d.alerts()[c]
resp['alerts'].append(tmp)
resp['status'] = 'OK'
return conf

def msg_exists(p):
if not models.Message.query.get(p['id']) is None:
return True
return False

SENT = 1
RCVD = 0
SENA = 2

def add_record(p):
resp = {
Expand Down Expand Up @@ -121,6 +93,8 @@ def add_record(p):
tmp.direction = RCVD
elif msg['direction'] == 'S':
tmp.direction = SENT
elif msg['direction'] == 'SA':
tmp.direction = SENA
elif msg['direction'] == 'R':
tmp.direction = RCVD
else:
Expand Down Expand Up @@ -217,3 +191,16 @@ def remove_item(id):
db.session.delete(r)
db.session.commit()
return success_remove(id)

def get_messages(id):
msg = []
r = models.User.query.get(id)
if r is None:
r = models.Device.query.get(id)
if r is None:
return not_found(id)
msg = r.messages.all()
else:
msg = r.messages.all()
print msg
return json.jsonify({'messages': msg})
18 changes: 15 additions & 3 deletions syncer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def before_request():
line = """
<div>
<h3 style=\"background-color:lightgreen; padding:5px;\">
""" + asctime() + ' ' + """<span style=\"font-size:.8em; font-weight:normal;\">""" + request.method + ' ' + request.path + ' ' + request.remote_addr + """</span>
""" + asctime() + ' ' + """<span style=\"font-weight:normal;\">""" + request.method + ' ' + request.path + ' ' + request.remote_addr + """</span>
</h3>
<pre style=\"background-color:lightgray; margin:20px; padding:5px;\">""" + str(request.headers) + """</pre>
<pre style=\"background-color:lightgray; margin:20px; padding:5px;\">""" + request.data + """</pre>
Expand All @@ -25,7 +25,7 @@ def after_request(r):
line = """
<div>
<h3 style=\"background-color:lightblue; padding:5px;\">
""" + asctime() + ' ' + """<span style=\"font-size:.8em; font-weight:normal;\">""" + str(r.status_code) + """</span>
""" + asctime() + ' ' + """<span style=\"font-weight:normal;\">""" + str(r.status_code) + """</span>
</h3>
<pre style=\"background-color:lightgray; margin:20px; padding:5px;\">""" + str(r.headers) + """</pre>
<pre style=\"background-color:lightgray; margin:20px; padding:5px;\">""" + r.data + """</pre>
Expand All @@ -37,7 +37,7 @@ def after_request(r):
@app.route('/login', methods=['POST'])
def login():
try:
p = request.get_json(force=True)
p = request.get_json()
except BadRequest:
return helpers.bad_request()
if not controllers.is_correct_login_format(p):
Expand Down Expand Up @@ -93,6 +93,13 @@ def decorated(*args, **kwargs):
return f(*args, **kwargs)
return decorated

@app.errorhandler(405)
def method_not_allowed(e):
js = json.dumps({'error': 'I\'m sorry, Dave. I\'m afraid I can\'t do that.'})
resp = Response(js, status=405, mimetype='application/json')
resp.headers['Allow'] = ", ".join(e.valid_methods)
return resp

@app.route('/users', methods=['GET', 'POST'])
@app.route('/users/<uid>', methods=['GET', 'PUT', 'DELETE'])
@requires_auth
Expand All @@ -118,3 +125,8 @@ def devices(did=None):
return controllers.update_item(did)
if request.method == 'DELETE':
return controllers.remove_item(did)

@app.route('/messages/<id>', methods=['GET'])
@requires_auth
def messages(id):
return controllers.get_messages(id)

0 comments on commit 3621589

Please sign in to comment.