Esempio n. 1
0
def _user_category_get(request, uuid_):
    context = Context()
    context.parse_request(request)
    context.parse_query_dict(request.GET)

    field_maps = None
    try:
        fields = query_utils.get_fields__query_dict(request.GET)
        field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    user_category = None
    try:
        user_category = models.UserCategory.objects.get(uuid=uuid_,
                                                        user=request.user)
    except models.UserCategory.DoesNotExist:
        return HttpResponseNotFound('user category not found')

    ret_obj = query_utils.generate_return_object(field_maps, user_category,
                                                 context)

    content, content_type = query_utils.serialize_content(ret_obj)

    return HttpResponse(content, content_type)
Esempio n. 2
0
    def test_get_field_maps(self):
        self.assertEqual(query_utils.get_field_maps(
            [], 'feed'), query_utils.fieldutils.get_default_field_maps('feed'))

        self.assertEqual(query_utils.get_field_maps(['uuid'], 'feed'), [
            query_utils.fieldutils.to_field_map('feed', 'uuid')])

        self.assertEqual(query_utils.get_field_maps(['uuid', 'title'], 'feed'),
                         [query_utils.fieldutils.to_field_map('feed', 'uuid'), query_utils.fieldutils.to_field_map('feed', 'title')])

        with self.settings(DEBUG=True):
            self.assertEqual(query_utils.get_field_maps(['_all'], 'feed'),
                             query_utils.fieldutils.get_all_field_maps('feed'))

        self.assertEqual(query_utils.get_field_maps(
            ['badField'], 'feed'), query_utils.fieldutils.get_default_field_maps('feed'))
Esempio n. 3
0
def _feed_get(request):
    context = Context()
    context.parse_request(request)
    context.parse_query_dict(request.GET)

    url = request.GET.get('url')
    if not url:
        return HttpResponseBadRequest('\'url\' missing')

    url = url_normalize(url)

    field_maps = None
    try:
        fields = query_utils.get_fields__query_dict(request.GET)
        field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    feed = None
    try:
        feed = models.Feed.annotate_subscription_data(
            models.Feed.objects.all(), request.user).get(feed_url=url)
    except models.Feed.DoesNotExist:
        try:
            feed = _save_feed(url)
        except QueryException as e:
            return HttpResponse(e.message, status=e.httpcode)

    ret_obj = query_utils.generate_return_object(field_maps, feed, context)

    content, content_type = query_utils.serialize_content(ret_obj)

    return HttpResponse(content, content_type)
Esempio n. 4
0
def _user_category_post(request):
    context = Context()
    context.parse_request(request)
    context.parse_query_dict(request.GET)

    field_maps = None
    try:
        fields = query_utils.get_fields__query_dict(request.GET)
        field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    if not request.body:
        return HttpResponseBadRequest('no HTTP body')  # pragma: no cover

    json_ = None
    try:
        json_ = ujson.loads(request.body)
    except ValueError:  # pragma: no cover
        return HttpResponseBadRequest('HTTP body cannot be parsed')

    if type(json_) is not dict:
        return HttpResponseBadRequest(
            'JSON body must be object')  # pragma: no cover

    if 'text' not in json_:
        return HttpResponseBadRequest('\'text\' missing')

    if type(json_['text']) is not str:
        return HttpResponseBadRequest('\'text\' must be string')

    user_category = models.UserCategory(user=request.user, text=json_['text'])

    try:
        user_category.save()
    except IntegrityError:
        return HttpResponse('user category already exists', status=409)

    ret_obj = query_utils.generate_return_object(field_maps, user_category,
                                                 context)

    content, content_type = query_utils.serialize_content(ret_obj)

    return HttpResponse(content, content_type)
Esempio n. 5
0
def _user_get(request):
    context = Context()
    context.parse_request(request)
    context.parse_query_dict(request.GET)

    user = request.user

    field_maps = None
    try:
        fields = query_utils.get_fields__query_dict(request.GET)
        field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    ret_obj = query_utils.generate_return_object(field_maps, user, context)

    content, content_type = query_utils.serialize_content(ret_obj)

    return HttpResponse(content, content_type)
Esempio n. 6
0
def _feed_entries_query_stable_post(request):
    cache = caches['stable_query']

    context = Context()
    context.parse_request(request)
    context.parse_query_dict(request.GET)

    if not request.body:
        return HttpResponseBadRequest('no HTTP body')  # pragma: no cover

    json_ = None
    try:
        json_ = ujson.loads(request.body)
    except ValueError:  # pragma: no cover
        return HttpResponseBadRequest('HTTP body cannot be parsed')

    if type(json_) is not dict:
        return HttpResponseBadRequest(
            'JSON body must be object')  # pragma: no cover

    token = None
    try:
        token = json_['token']
    except KeyError:
        return HttpResponseBadRequest('\'token\' missing')

    if type(token) is not str:
        return HttpResponseBadRequest('\'token\' must be string')

    if re.search(r'^feedentry-\d+$', token) is None:
        return HttpResponseBadRequest('\'token\' malformed')

    count = None
    try:
        count = query_utils.get_count(json_)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    skip = None
    try:
        skip = query_utils.get_skip(json_)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    field_maps = None
    try:
        fields = query_utils.get_fields__json(json_)
        field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    return_objects = None
    try:
        return_objects = query_utils.get_return_objects(json_)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    return_total_count = None
    try:
        return_total_count = query_utils.get_return_total_count(json_)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    cache.touch(token)
    uuids = cache.get(token, [])

    ret_obj = {}

    if return_objects:
        current_uuids = uuids[skip:skip + count]

        feed_entries = {
            feed_entry.uuid: feed_entry
            for feed_entry in models.FeedEntry.objects.filter(
                uuid__in=current_uuids)
        }

        objs = []
        if len(current_uuids) == len(feed_entries):
            for uuid_ in current_uuids:
                feed_entry = feed_entries[uuid_]
                obj = query_utils.generate_return_object(
                    field_maps, feed_entry, context)
                objs.append(obj)

        ret_obj['objects'] = objs

    if return_total_count:
        ret_obj['totalCount'] = len(uuids)

    content, content_type = query_utils.serialize_content(ret_obj)
    return HttpResponse(content, content_type)
Esempio n. 7
0
def _feed_entries_query_post(request):
    context = Context()
    context.parse_request(request)
    context.parse_query_dict(request.GET)

    if not request.body:
        return HttpResponseBadRequest('no HTTP body')  # pragma: no cover

    json_ = None
    try:
        json_ = ujson.loads(request.body)
    except ValueError:  # pragma: no cover
        return HttpResponseBadRequest('HTTP body cannot be parsed')

    if type(json_) is not dict:
        return HttpResponseBadRequest(
            'JSON body must be object')  # pragma: no cover

    count = None
    try:
        count = query_utils.get_count(json_)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    skip = None
    try:
        skip = query_utils.get_skip(json_)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    sort = None
    try:
        sort = query_utils.get_sort(json_, _OBJECT_NAME)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    search = None
    try:
        search = query_utils.get_search(context, json_, _OBJECT_NAME)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    field_maps = None
    try:
        fields = query_utils.get_fields__json(json_)
        field_maps = query_utils.get_field_maps(fields, _OBJECT_NAME)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    return_objects = None
    try:
        return_objects = query_utils.get_return_objects(json_)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    return_total_count = None
    try:
        return_total_count = query_utils.get_return_total_count(json_)
    except QueryException as e:  # pragma: no cover
        return HttpResponse(e.message, status=e.httpcode)

    feed_entries = models.FeedEntry.annotate_search_vectors(
        models.FeedEntry.objects.all()).filter(*search)

    ret_obj = {}

    if return_objects:
        objs = []
        for feed_entry in feed_entries.order_by(*sort)[skip:skip + count]:
            obj = query_utils.generate_return_object(field_maps, feed_entry,
                                                     context)
            objs.append(obj)

        ret_obj['objects'] = objs

    if return_total_count:
        ret_obj['totalCount'] = feed_entries.count()

    content, content_type = query_utils.serialize_content(ret_obj)
    return HttpResponse(content, content_type)