Created
November 11, 2013 21:23
-
-
Save ahwolf/7420658 to your computer and use it in GitHub Desktop.
Pagination error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# django | |
from django.forms import widgets | |
# 3rd party apps | |
from rest_framework import serializers, pagination | |
from base.models import Document, Sentence | |
class DocumentSerializer(serializers.Serializer): | |
_id = serializers.CharField(required=True, max_length=None) | |
text = serializers.CharField(required=True, max_length=None) | |
def restore_object(self, attrs, instance=None): | |
print 'attrs:', attrs | |
print 'instance:', instance | |
# update existing instance | |
if instance: | |
instance._id = attrs.get('_id', instance._id) | |
instance.text = attrs.get('text', instance.text) | |
return instance | |
# Create new instance | |
return Document(**attrs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# django | |
from django.shortcuts import get_object_or_404, render, render_to_response | |
from django.contrib.auth.models import User | |
from django.views.generic.base import RedirectView, View | |
from django.conf import settings | |
from django.template import RequestContext | |
# 3rd party apps | |
from rest_framework import status | |
from rest_framework import viewsets | |
from rest_framework.parsers import JSONParser | |
from rest_framework.response import Response | |
from rest_framework.decorators import link | |
from rest_framework import renderers | |
# local | |
from base.models import Tag, Decision | |
from base.serializers import SentenceSerializer, DocumentSerializer | |
from mongo_connection import mongo_connection | |
class MoneyballViewSet(viewsets.GenericViewSet): | |
"""This is an abstract base class for the ViewSets defined in this | |
module. | |
""" | |
# this is used as the base name by the router in urls.py, and also | |
# can be used for nicely formatting error messages. It should be | |
# overwritten in base classes. | |
base_name = 'moneyball' | |
paginate_by = 20 | |
class DocumentViewSet(MoneyballViewSet): | |
"""This is the view set for dealing with documents. It's what the API | |
does with requests for documents. | |
""" | |
# used by router. see base class comment | |
base_name = 'document' | |
# testing a GET request task with the TaskRouter | |
# @link(renderer_classes=[renderers.StaticHTMLRenderer]) | |
# def textsearch(self, request, *args, **kwargs): | |
# print 'textsearch' | |
# return Response("heyyyyy!", status=status.HTTP_200_OK) | |
def list(self, request, format=None): | |
for k,v in request.DATA.iteritems(): | |
print k, ':', v | |
collection = mongo_connection() | |
# get the query from the request | |
batch_size = request.DATA.get('batch_size', 20) | |
query = request.DATA.get("query",{}) | |
queryset = collection.find(query).batch_size(batch_size) | |
# serialize and respond | |
serializer = DocumentSerializer(queryset, many=True) | |
return Response(serializer.data, status=status.HTTP_200_OK) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment