def get(self, id=None): """Get a single location or a list of all the user's locations.""" if id is not None: location = self._authorize_location_by_id(id) return jsonify(Location.flatten(location)) # can't use jsonify http://flask.pocoo.org/docs/security/#json-security locations = [Location.flatten(loc) for loc in app.db.locations.find({ 'owner': current_user.id })] resp = Response(content_type='application/json') resp.data = json.dumps(locations) return resp
def test_json_to_python(self): """Test converting flattened public data to its Python equivalent. This should expand ids to ObjectId instances, and set the logged in user to the owner of any incoming Location objects. """ # There seems to be a problem with Flask-Login setting the current_user proxy # in api/models.py, which we need t run this test. if False: self.login_test_user() location = { 'address': '123 Main St.', 'lat': '127.0', # forgive numbers coming as strings 'lng': -42, 'name': 'nowhere', 'id': str(ObjectId()) } expanded = Location.from_json(location) # these should all be the same self.assertEqual(expanded['address'], location['address']) self.assertEqual(expanded['lat'], location['lat']) self.assertEqual(expanded['lng'], location['lng']) self.assertEqual(expanded['name'], location['name']) # owner should be set by the currently logged in location self.assertEqual(expanded['owner'], self.test_location.id) # id should be renamed from id to _id, and expanded self.assertTrue(expanded.has_key('_id')) self.assertFalse(expanded.has_key('id')) self.assertEqual(str(expanded['_id']), location['id'])
def test_python_to_json(self): """Test flattening Python data to its JSON equivalent. This should flatten data types to primitives, and remove/rename attributes to make them public. """ location = { 'address': '123 Main St.', 'lat': 127.0, 'lng': -42, 'name': 'nowhere', 'owner': ObjectId(), '_id': ObjectId() } parsed = Location.flatten(location) # these should all be the same self.assertEqual(parsed['address'], location['address']) self.assertEqual(parsed['lat'], location['lat']) self.assertEqual(parsed['lng'], location['lng']) self.assertEqual(parsed['name'], location['name']) # owner should be removed self.assertFalse(parsed.has_key('owner')) # and id should be renamed from _id to id, and flattened self.assertFalse(parsed.has_key('_id')) self.assertTrue(parsed.has_key('id')) self.assertEqual(parsed['id'], str(location['_id']))
def test_json_to_python(self): """Test converting flattened public data to its Python equivalent. This should expand ids to ObjectId instances, and set the logged in user to the owner of any incoming Location objects. """ # There seems to be a problem with Flask-Login setting the current_user proxy # in api/models.py, which we need t run this test. if False: self.login_test_user() location = { 'address' : '123 Main St.', 'lat' : '127.0', # forgive numbers coming as strings 'lng' : -42, 'name' : 'nowhere', 'id' : str(ObjectId()) } expanded = Location.from_json(location) # these should all be the same self.assertEqual(expanded['address'], location['address']) self.assertEqual(expanded['lat'], location['lat']) self.assertEqual(expanded['lng'], location['lng']) self.assertEqual(expanded['name'], location['name']) # owner should be set by the currently logged in location self.assertEqual(expanded['owner'], self.test_location.id) # id should be renamed from id to _id, and expanded self.assertTrue(expanded.has_key('_id')) self.assertFalse(expanded.has_key('id')) self.assertEqual(str(expanded['_id']), location['id'])
def test_python_to_json(self): """Test flattening Python data to its JSON equivalent. This should flatten data types to primitives, and remove/rename attributes to make them public. """ location = { 'address' : '123 Main St.', 'lat' : 127.0, 'lng' : -42, 'name' : 'nowhere', 'owner' : ObjectId(), '_id' : ObjectId() } parsed = Location.flatten(location) # these should all be the same self.assertEqual(parsed['address'], location['address']) self.assertEqual(parsed['lat'], location['lat']) self.assertEqual(parsed['lng'], location['lng']) self.assertEqual(parsed['name'], location['name']) # owner should be removed self.assertFalse(parsed.has_key('owner')) # and id should be renamed from _id to id, and flattened self.assertFalse(parsed.has_key('_id')) self.assertTrue(parsed.has_key('id')) self.assertEqual(parsed['id'], str(location['_id']))
def get(self, id=None): """Get a single location or a list of all the user's locations.""" if id is not None: location = self._authorize_location_by_id(id) return jsonify(Location.flatten(location)) # can't use jsonify http://flask.pocoo.org/docs/security/#json-security locations = [ Location.flatten(loc) for loc in app.db.locations.find({'owner': current_user.id}) ] resp = Response(content_type='application/json') resp.data = json.dumps(locations) return resp
def _location_from_json_or_400(self, data, required_id=None): """Parse the data into a valid location, or abort with a 404.""" location = Location.from_json(data, required_id=required_id) if not location: abort(400) return location
def put(self, id): """Update the location for the given id.""" self._authorize_location_by_id(id) updated = self._location_from_json_or_400(request.json, required_id=id) try: app.db.locations.save(updated, safe=True) return jsonify(Location.flatten(updated)) except OperationFailure as e: return server_error(str(e))
def post(self): """Create a new location.""" location = self._location_from_json_or_400(request.json) try: app.db.locations.insert(location, safe=True) location = Location.flatten(location) resp = jsonify(location) resp.status_code = 201 resp.headers['Location'] = url_for('.location_view', _method='GET', id=location['id']) return resp except OperationFailure as e: return server_error(str(e))