Skip to content

Commit

Permalink
improved location views readability
Browse files Browse the repository at this point in the history
  • Loading branch information
zackdever committed Nov 22, 2012
1 parent 92ba101 commit 992e58a
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions uber/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,20 @@ def index():
@app.route('/locations/', methods = ['GET', 'POST'])
def locations():
if request.method == 'POST':
content = request.headers['Content-Type']
if not is_content_json(): return require_json()

if content != 'application/json':
return bad_request("Expected 'application/json', got '%s'" % content)
else:
location = Location.parse_json(request.json, has_id=False)
if location == None:
return bad_request()
location = Location.parse_json(request.json, has_id=False)
if not location: return bad_request()

app.db.locations.insert(location)
location = Location.flatten(location)
app.db.locations.insert(location)
location = Location.flatten(location)

resp = jsonify(location)
resp.status_code = 201
resp.headers['Location'] = url_for('locations') + location['id']

return resp
resp = jsonify(location)
resp.status_code = 201
resp.headers['Location'] = url_for('locations') + location['id']
return resp

# GET /locations/ - show all
return jsonify({
'locations' : [Location.flatten(loc) for loc in app.db.locations.find()]
})
Expand All @@ -38,33 +34,35 @@ def location(id):
if not ObjectId.is_valid(id):
return bad_request("'id' is either missing or is of the wrong type")

# ok, the id's format is valid, but does it exist?
oid = ObjectId(id)
location = app.db.locations.find_one({ '_id' : oid })
if not location:
return not_found()
if not location: return not_found()

if request.method == 'PUT':
if not is_content_json(): return require_json()

updated = Location.parse_json(request.json, has_id=True)
if not updated or updated['_id'] != oid:
return bad_request()
if not updated or updated['_id'] != oid: return bad_request()

app.db.locations.save(updated)
return jsonify(Location.flatten(updated))

elif request.method == 'DELETE':
app.db.locations.remove({ '_id' : oid })
return Response(status=204)
return Response(status=204, content_type='application/json')

# GET /location/<id> - show location with id
return jsonify(Location.flatten(location))

### Error handlers
@app.errorhandler(400)
def bad_request(message=None):
message = message if message != None else 'Bad Request: ' + request.url
message = message if message != None else request.url
resp = jsonify({
'error': {
'code': 400,
'message': message,
'message': 'Bad Request: ' + message,
}
})

Expand All @@ -73,13 +71,33 @@ def bad_request(message=None):

@app.errorhandler(404)
def not_found(message=None):
message = message if message != None else request.url
resp = jsonify({
'error': {
'code': 404,
'message': 'Not Found: ' + request.url,
'message': 'Not Found: ' + message,
}
})

resp.status_code = 404
return resp

@app.errorhandler(500)
def server_error(message=None):
message = message if message != None else "It's me, not you."
resp = jsonify({
'error': {
'code': 500,
'message': message,
}
})

resp.status_code = 404
return resp

def is_content_json():
return request.headers['Content-Type'] == 'application/json'

def require_json():
return bad_request("Expected 'application/json', got '%s'" %
request.headers['Content-Type'])

0 comments on commit 992e58a

Please sign in to comment.