def test_resulting_dict_all_params(self): self.kwargs['params'] = '*' expected = { 'part': u'Londo', 'callback': u'jquery_callback' } self.assertEqual(QueryParamsKeyBit().get_data(**self.kwargs), expected)
class CustomListKeyConstructor(DefaultKeyConstructor): """ Used to compute cache key for a list of objects. """ list_sql = ListSqlQueryKeyBit() pagination = PaginationKeyBit() updated_at = UpdatedAtKeyBit() user = UserKeyBit() all_query_params = QueryParamsKeyBit()
class TimestampedListKeyConstructor(DefaultListKeyConstructor): timestamp = ApiTimestampKeyBit() # The DefaultListKeyConstructor includes the PaginationKeyBit. While it does # subclass QueryParamsKeyBit, it also bypasses logic which includes all query # params in the cache key, restricting the set of query params that end up in # the cache key to those that are used for page number pagination. This causes # cache collisions when other query params are involved. For more, see: # https://github.com/chibisov/drf-extensions/blob/master/rest_framework_extensions/key_constructor/bits.py#L48-L49 querystring = QueryParamsKeyBit()
class QueryParamsKeyConstructor(DefaultKeyConstructor): """ Example: class ProductViewSet(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet): queryset = Product.objects.filter(withdrawn=False) serializer_class = ProductSerializer pagination_class = LargeResultsSetPagination list_cache_key_func = QueryParamsKeyConstructor() """ all_query_params = QueryParamsKeyBit()
def test_resulting_dict(self): self.kwargs = { 'params': ['part', 'callback', 'not_existing_param'], 'view_instance': None, 'view_method': None, 'request': factory.get('?part=Londo&callback=jquery_callback'), 'args': None, 'kwargs': None } expected = {'part': u'Londo', 'callback': u'jquery_callback'} self.assertEqual(QueryParamsKeyBit().get_data(**self.kwargs), expected)
def test_default_params_is_all_args(self): self.assertEqual(QueryParamsKeyBit().params, '*')
class QueryParamsKeyConstructor(DefaultKeyConstructor): all_query_params = QueryParamsKeyBit()
class RequestKeyConstructor(DefaultKeyConstructor): arguments = ArgsKeyBit() key_arguments = KwargsKeyBit() query_params = QueryParamsKeyBit() user = UserKeyBit() headers = HeadersKeyBit()
class TimestampedObjectKeyConstructor(DefaultObjectKeyConstructor): timestamp = ApiTimestampKeyBit() # The DefaultObjectKeyConstructor doesn't include querystring parameters # in its cache key. querystring = QueryParamsKeyBit()
class ScoreboardKeyConstructor(DefaultKeyConstructor): params_bit = QueryParamsKeyBit() args_bit = ArgsKeyBit() kwargs_bit = KwargsKeyBit()
class UpdatedAtDayForFeed(DefaultKeyConstructor): updated_at = UpdatedAtKeyBit() data_day = UpdatedAtDay() pagination = PaginationKeyBit() query = QueryParamsKeyBit()
class ListKeyConstructor(DefaultListKeyConstructor): query_params = QueryParamsKeyBit()