Beispiel #1
0
    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
Beispiel #2
0
    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']))
Beispiel #3
0
    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']))
Beispiel #4
0
    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
Beispiel #5
0
    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))
Beispiel #6
0
    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))
Beispiel #7
0
    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))
Beispiel #8
0
    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))