Ejemplo n.º 1
0
 def get_collection_counts(self, request):
     """Returns a hash of collections associated with the account,
     Along with the total number of items for each collection.
     """
     user_id = request.user['userid']
     counts = self._get_storage(request).get_collection_counts(user_id)
     response = convert_response(request, counts)
     response.headers['X-Weave-Records'] = str(len(counts))
     return response
    def test_response_conversions(self):
        data = {'some': 'data'}
        resp = text_response(data)
        self.assertEquals(resp.body, "{'some': 'data'}")
        self.assertEquals(resp.content_type, 'text/plain')

        data = "abc"
        resp = whoisi_response(data)
        self.assertEquals(
            resp.body,
            '\x00\x00\x00\x03"a"\x00\x00\x00\x03"b"\x00\x00\x00\x03"c"')
        self.assertEquals(resp.content_type, 'application/whoisi')

        request = Request({})
        request.accept = 'application/whoisi'
        resp = convert_response(request, data)
        self.assertEquals(
            resp.body,
            '\x00\x00\x00\x03"a"\x00\x00\x00\x03"b"\x00\x00\x00\x03"c"')
        self.assertEquals(resp.content_type, 'application/whoisi')

        resp = newlines_response(data)
        self.assertEquals(resp.body, '"a"\n"b"\n"c"\n')
        self.assertEquals(resp.content_type, 'application/newlines')

        request = Request({})
        request.accept = 'application/newlines'
        resp = convert_response(request, data)
        self.assertEquals(resp.body, '"a"\n"b"\n"c"\n')
        self.assertEquals(resp.content_type, 'application/newlines')

        data = {'some': 'data'}
        resp = json_response(data)
        self.assertEquals(resp.body, '{"some": "data"}')
        self.assertEquals(resp.content_type, 'application/json')

        request = Request({})
        resp = convert_response(request, data)
        self.assertEquals(resp.body, '{"some": "data"}')
        self.assertEquals(resp.content_type, 'application/json')
Ejemplo n.º 3
0
    def test_response_conversions(self):
        data = {'some': 'data'}
        resp = text_response(data)
        self.assertEquals(resp.body, "{'some': 'data'}")
        self.assertEquals(resp.content_type, 'text/plain')

        data = "abc"
        resp = whoisi_response(data)
        self.assertEquals(resp.body,
                '\x00\x00\x00\x03"a"\x00\x00\x00\x03"b"\x00\x00\x00\x03"c"')
        self.assertEquals(resp.content_type, 'application/whoisi')

        request = Request({})
        request.accept = 'application/whoisi'
        resp = convert_response(request, data)
        self.assertEquals(resp.body,
                '\x00\x00\x00\x03"a"\x00\x00\x00\x03"b"\x00\x00\x00\x03"c"')
        self.assertEquals(resp.content_type, 'application/whoisi')

        resp = newlines_response(data)
        self.assertEquals(resp.body, '"a"\n"b"\n"c"\n')
        self.assertEquals(resp.content_type, 'application/newlines')

        request = Request({})
        request.accept = 'application/newlines'
        resp = convert_response(request, data)
        self.assertEquals(resp.body, '"a"\n"b"\n"c"\n')
        self.assertEquals(resp.content_type, 'application/newlines')

        data = {'some': 'data'}
        resp = json_response(data)
        self.assertEquals(resp.body, '{"some": "data"}')
        self.assertEquals(resp.content_type, 'application/json')

        request = Request({})
        resp = convert_response(request, data)
        self.assertEquals(resp.body, '{"some": "data"}')
        self.assertEquals(resp.content_type, 'application/json')
Ejemplo n.º 4
0
    def get_collections(self, request, **metrics):
        """Returns a hash of collections associated with the account,
        Along with the last modified timestamp for each collection
        """
        # metrics are additional parameters used by the various clients
        # to mark the logs for stats. We also want to send a CEF log when
        # this happens
        #
        # Disabled for now, since CEF doesn't need them at the moment
        #
        #if metrics != {}:
        #    username = request.user['username']
        #    values = ['%s=%s' % (key, value)
        #              for key, value in metrics.items()]
        #    log_cef('Daily metric call', 5, request.environ, self.app.config,
        #            username=username, msg=','.join(values))

        user_id = request.user['userid']
        storage = self._get_storage(request)
        collections = storage.get_collection_timestamps(user_id)
        response = convert_response(request, collections)
        response.headers['X-Weave-Records'] = str(len(collections))
        return response
Ejemplo n.º 5
0
    def get_collection(self, request, **kw):
        """Returns a list of the WBO ids contained in a collection."""
        kw = self._convert_args(kw)
        collection_name = request.sync_info['collection']
        user_id = request.user['userid']
        full = kw['full']

        if not full:
            fields = ['id']
        else:
            fields = _WBO_FIELDS

        storage = self._get_storage(request)
        res = storage.get_items(user_id, collection_name, fields,
                                kw['filters'],
                                kw.get('limit'), kw.get('offset'),
                                kw.get('sort'))
        if not full:
            res = [line['id'] for line in res]

        response = convert_response(request, res)
        response.headers['X-Weave-Records'] = str(len(res))
        return response