Permalink
Browse files

Added pagination support for lists and zsets.

  • Loading branch information...
1 parent 8b79ebc commit ecf979badf26fd3546463c627342e45fa099b83e @ionelmc committed Nov 8, 2011
@@ -46,3 +46,10 @@ td.error {
font-weight: bold;
color: #B22222;
}
+.inspect-form .paginator a,
+.inspect-form .paginator span {
+ display: inline-block;
+ padding: 5px;
+ border: 1px solid gray;
+ margin: 5px;
+}
@@ -134,6 +134,18 @@
<tr><td>{{ key }}</td><td>{{ value }}</td></tr>
{% endfor %}
</table>
+ {% if key_details.data.paginator %}
+ <p class="paginator">
+ {% for nr in key_details.data.paginator.page_range %}
+ {% if key_details.data.number == nr %}
+ <span class="this-page">{{ nr }}</span>
+ {% else %}
+ <a href="?key={{ key_details.name|escape }}&db={{ key_details.db }}&page={{ nr }}">{{ nr }}</a>
+ {% endif %}
+ {% endfor %}
+ {{ key_details.data.paginator.count }} items
+ </p>
+ {% endif %}
</fieldset>
{% endif %}
</div>
View
@@ -1,5 +1,23 @@
# shamelessly taken from kombu.utils
+class LazySlicingIterable(object):
+ def __init__(self, length_getter, items_getter):
+ self.length_getter = length_getter
+ self.items_getter = items_getter
+
+ def __len__(self):
+ return self.length_getter()
+
+ def __getitem__(self, k):
+ if isinstance(k, int):
+ return self.items_getter(k, k)
+ elif isinstance(k, slice):
+ if k.step:
+ raise RuntimeError("Can't use steps for slicing.")
+ return self.items_getter(k.start, k.stop)
+ else:
+ raise TypeError("Must be int or slice.")
+
class cached_property(object):
"""Property descriptor that caches the return value
of the get function.
@@ -65,4 +83,4 @@ def setter(self, fset):
return self.__class__(self.__get, fset, self.__del)
def deleter(self, fdel):
- return self.__class__(self.__get, self.__set, fdel)
+ return self.__class__(self.__get, self.__set, fdel)
View
@@ -1,12 +1,18 @@
from logging import getLogger
logger = getLogger(__name__)
+from django.core.paginator import Paginator
from django.shortcuts import render
from django.utils.datastructures import SortedDict
from django.conf import settings
+from django.utils.functional import curry
from redis.exceptions import ResponseError
+from .utils import LazySlicingIterable
+
+REDISBOARD_ITEMS_PER_PAGE = getattr(settings, 'REDISBOARD_ITEMS_PER_PAGE', 1000)
+
def safeint(value):
try:
return int(value)
@@ -50,17 +56,29 @@ def _get_key_info(conn, key):
}
VALUE_GETTERS = {
- 'list': lambda conn, key, start=0, end=-1: enumerate(conn.lrange(key, start, end)),
+ 'list': lambda conn, key, start=0, end=-1: [(pos+start, val) for pos, val in enumerate(conn.lrange(key, start, end))],
'string': lambda conn, key, *args: [('string', conn.get(key))],
- 'set': lambda conn, key, *args: enumerate(conn.smembers(key)),
- 'zset': lambda conn, key, start=0, end=-1: enumerate(conn.zrange(key, start, end)),
- 'hash': lambda conn, key, *args: conn.hgetall(key).iteritems(),
+ 'set': lambda conn, key, *args: list(enumerate(conn.smembers(key))),
+ 'zset': lambda conn, key, start=0, end=-1: [(pos+start, val) for pos, val in enumerate(conn.zrange(key, start, end))],
+ 'hash': lambda conn, key, *args: conn.hgetall(key).items(),
}
-def _get_key_details(conn, db, key):
+def _get_key_details(conn, db, key, page):
conn.execute_command('SELECT', db)
details = _get_key_info(conn, key)
- details['data'] = VALUE_GETTERS[details['type']](conn, key)
+ details['db'] = db
+ if details['type'] in ('list', 'zset'):
+ details['data'] = Paginator(
+ LazySlicingIterable(
+ lambda: details['length'],
+ curry(VALUE_GETTERS[details['type']], conn, key)
+ ),
+ REDISBOARD_ITEMS_PER_PAGE
+ ).page(page)
+ else:
+ details['data'] = VALUE_GETTERS[details['type']](conn, key)
+
+
return details
@@ -83,7 +101,8 @@ def inspect(request, server):
if 'key' in request.GET:
key = request.GET['key']
db = request.GET.get('db', 0)
- key_details = _get_key_details(conn, db, key)
+ page = request.GET.get('page', 1)
+ key_details = _get_key_details(conn, db, key, page)
else:
databases = sorted(name[2:] for name in conn.info() if name.startswith('db'))

0 comments on commit ecf979b

Please sign in to comment.