Skip to content

Commit

Permalink
Merge pull request #364 from CTPUG/feature/order-paginated-result-sets
Browse files Browse the repository at this point in the history
Add ordering of paginated query results.
  • Loading branch information
hodgestar committed Aug 2, 2017
2 parents 5f095d7 + 4d743f5 commit 6a07234
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions wafer/kv/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from wafer.kv.models import KeyValue
from wafer.kv.serializers import KeyValueSerializer
from wafer.kv.permissions import KeyValueGroupPermission
from wafer.utils import order_results_by


class KeyValueViewSet(viewsets.ModelViewSet):
"""API endpoint that allows key-value pairs to be viewed or edited."""
queryset = KeyValue.objects.none() # Needed for the REST Permissions
serializer_class = KeyValueSerializer
permission_classes = (KeyValueGroupPermission, )

@order_results_by('key', 'id')
def get_queryset(self):
# Restrict the list to only those that match the user's
# groups
Expand Down
2 changes: 1 addition & 1 deletion wafer/schedule/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class ScheduleItemViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = ScheduleItem.objects.all()
queryset = ScheduleItem.objects.all().order_by('id')
serializer_class = ScheduleItemSerializer
permission_classes = (IsAdminUser, )

Expand Down
9 changes: 6 additions & 3 deletions wafer/talks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from wafer.talks.forms import get_talk_form_class
from wafer.talks.serializers import TalkSerializer, TalkUrlSerializer
from wafer.users.models import UserProfile
from wafer.utils import order_results_by


class EditOwnTalksMixin(object):
Expand All @@ -37,13 +38,14 @@ class UsersTalks(ListView):
template_name = 'wafer.talks/talks.html'
paginate_by = 25

@order_results_by('talk_id')
def get_queryset(self):
# self.request will be None when we come here via the static site
# renderer
if (self.request and Talk.can_view_all(self.request.user)):
return Talk.objects.all()
return Talk.objects.filter( Q(status=ACCEPTED) |
Q(status=CANCELLED))
return Talk.objects.filter(Q(status=ACCEPTED) |
Q(status=CANCELLED))


class TalkView(DetailView):
Expand Down Expand Up @@ -162,6 +164,7 @@ class TalksViewSet(viewsets.ModelViewSet, NestedViewSetMixin):
# XXX: Do we want to allow authors to edit talks via the API?
permission_classes = (DjangoModelPermissionsOrAnonReadOnly, )

@order_results_by('talk_id')
def get_queryset(self):
# We override the default implementation to only show accepted talks
# to people who aren't part of the management group
Expand Down Expand Up @@ -191,7 +194,7 @@ def has_permission(self, request, view):

class TalkUrlsViewSet(viewsets.ModelViewSet, NestedViewSetMixin):
"""API endpoint that allows talks to be viewed or edited."""
queryset = TalkUrl.objects.all()
queryset = TalkUrl.objects.all().order_by('id')
serializer_class = TalkUrlSerializer
permission_classes = (DjangoModelPermissions, TalkExistsPermission)

Expand Down
13 changes: 13 additions & 0 deletions wafer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ def normalize_unicode(u):
return unicodedata.normalize('NFKD', u).encode('ascii', 'ignore')


def order_results_by(*fields):
"""A decorator that applies an ordering to the QuerySet returned by a
function.
"""
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
result = f(*args, **kw)
return result.order_by(*fields)
return wrapper
return decorator


def cache_result(cache_key, timeout):
"""A decorator for caching the result of a function."""
def decorator(f):
Expand Down

0 comments on commit 6a07234

Please sign in to comment.