Beispiel #1
0
    def get_mine(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        self.throttle_check(request)

        limit = int(request.GET.get('limit', 20))

        if request.user.id is None:
            return self.create_response(request, {
                'objects': [],
                'meta': {
                    'author': request.user,
                    'limit': limit,
                    'total_count': 0
                }
            })
        else:
            # Do the query.
            results   = self._meta.queryset.filter(_author__contains=request.user.id)
            # For retro compatibility we use the django paginator
            paginator = resource_paginator(self)(request.GET, results, resource_uri=self.get_resource_uri(), limit=limit, collection_name=self._meta.collection_name)
            to_be_serialized = paginator.page()

            # Dehydrate the bundles in preparation for serialization.
            bundles = []

            for obj in to_be_serialized[self._meta.collection_name]:
                bundle = self.build_bundle(obj=obj, request=request)
                bundles.append(self.full_dehydrate(bundle, for_list=True))

            to_be_serialized[self._meta.collection_name] = bundles
            to_be_serialized = self.alter_list_data_to_serialize(request, to_be_serialized)

            return self.create_response(request, to_be_serialized)
    def get_search(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        self.throttle_check(request)

        query     = request.GET.get('q', '').lower()
        query     = re.sub("\"|'|`|;|:|{|}|\|(|\|)|\|", '', query).strip()
        limit     = int( request.GET.get('limit', 20))
        p         = int(request.GET.get('page', 1))
        # Do the query.
        results   = self._meta.queryset.filter(name__icontains=query)
        # For retro compatibility we use the django paginator
        paginator = resource_paginator(self)(request.GET, results, resource_uri=self.get_resource_uri(), limit=limit, collection_name=self._meta.collection_name)
        to_be_serialized = paginator.page()

        # Dehydrate the bundles in preparation for serialization.
        bundles = []

        for obj in to_be_serialized[self._meta.collection_name]:
            bundle = self.build_bundle(obj=obj, request=request)
            bundles.append(self.full_dehydrate(bundle, for_list=True))

        to_be_serialized[self._meta.collection_name] = bundles
        to_be_serialized = self.alter_list_data_to_serialize(request, to_be_serialized)

        return self.create_response(request, to_be_serialized)
Beispiel #3
0
 def __init__(self, api_name=None):
     super(IndividualResource, self).__init__(api_name)
     # Pass the current instance of the resource to the paginator
     self._meta.paginator_class = resource_paginator(self)
     # By default, tastypie detects detail mode globally: it means that
     # even into an embeded resource (through a relationship), Tastypie will
     # serialize it as if we are in it's detail view.
     # We overide 'use_in' for every field with the value "detail"
     for field_name, field_object in self.fields.items():
         if field_object.use_in == 'detail':
             # We use a custom method
             field_object.use_in = self.use_in