#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.contrib.auth import authenticate

from piston.handler import BaseHandler as Handler

from opps.api.models import ApiKey


class BaseHandler(Handler):

    def read(self, request):
        base = self.model.objects
        if request.GET.items():
            return base.filter(**request.GET.dict())
        return base.all()

    def appendModel(Model, Filters):
        m = Model.objects.filter(**Filters)
        l = []
        for i in m:
            l.append(i.__dict__)
        return l


class ApiKeyAuthentication(object):
    def __init__(self, auth_func=authenticate, method=['GET']):
        self.auth_func = auth_func
        self.method = method

    def is_authenticated(self, request):
        if request.method == 'GET' and 'GET' in self.method:
            return True

        try:
            method = getattr(request, request.method)
        except:
            method = request.GET

        try:
            ApiKey.objects.get(
                user__username=method.get('api_username'),
                key=method.get('api_key'))
        except ApiKey.DoesNotExist:
            return False

        return True

    def challenge(self):
        resp = HttpResponse("Authorization Required")
        resp.status_code = 401
        return resp

@@ -237,16 +237,114 @@
 andler):
+%0A    limit = 20%0A%0A    def dispatch(self, request):%0A        import pdb; pdb.set_trace()%0A        pass
 %0A%0A    de
@@ -448,74 +448,639 @@
     
-return base.filter(**request.GET.dict())%0A        return base.all()
+items = request.GET.dict()%0A            if request.GET.get('paginate_limit'):%0A                del items%5B'paginate_limit'%5D%0A            if request.GET.get('page'):%0A                del items%5B'page'%5D%0A            return base.filter(**items)%0A        return base.all()%0A%0A    def _limit(self, request):%0A        limit = request.GET.get('paginate_limit', self.limit)%0A        return int(limit)*int(request.GET.get('page', 1))%0A%0A    def _page(self, request):%0A        page = int(request.GET.get('page', 1))%0A        if page == 1:%0A            return 0%0A        limit = int(request.GET.get('paginate_limit', self.limit))%0A        return limit*page-page
 %0A%0A  

add method _limit/_page on api Handler