Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Exceptions break Class Based Views #15

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion example_project/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

from django.http import Http404
from django.shortcuts import render_to_response
from django.views.generic import ListView

from pure_pagination.paginator import Paginator
from pure_pagination.mixins import PaginationMixin

from core.names import names

Expand All @@ -31,4 +33,8 @@ def index(request):
'page': page,
})



class List(PaginationMixin, ListView):
template_name = 'list.html'
paginate_by = 10
queryset = names
3 changes: 2 additions & 1 deletion example_project/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.conf.urls.defaults import patterns, include, url

from core.views import List
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
Expand All @@ -16,4 +16,5 @@
# url(r'^admin/', include(admin.site.urls)),

url(r'^$', 'core.views.index', name="index"),
url(r'^list/$', List.as_view(), name="list"),
)
10 changes: 1 addition & 9 deletions pure_pagination/paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
import functools

from django.template.loader import render_to_string
from django.core.paginator import InvalidPage, PageNotAnInteger, EmptyPage

PAGINATION_SETTINGS = getattr(settings, "PAGINATION_SETTINGS", {})

PAGE_RANGE_DISPLAYED = PAGINATION_SETTINGS.get("PAGE_RANGE_DISPLAYED", 10)
MARGIN_PAGES_DISPLAYED = PAGINATION_SETTINGS.get("MARGIN_PAGES_DISPLAYED", 2)

class InvalidPage(Exception):
pass

class PageNotAnInteger(InvalidPage):
pass

class EmptyPage(InvalidPage):
pass

class Paginator(object):
def __init__(self, object_list, per_page, orphans=0, allow_empty_first_page=True, request=None):
self.object_list = object_list
Expand Down
28 changes: 24 additions & 4 deletions pure_pagination/tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from datetime import datetime
from operator import attrgetter

from pure_pagination import Paginator, InvalidPage, EmptyPage
from pure_pagination import Paginator, InvalidPage, EmptyPage, PaginationMixin

from django.test import TestCase
from django.test.client import Client
from django.test.client import Client, RequestFactory
from django.views.generic import ListView
from django.http import Http404

from django.db import models

Expand All @@ -22,6 +25,11 @@ class LenContainer(object):
def __len__(self):
return 42

class ArticleList(PaginationMixin, ListView):
paginate_by = 2
queryset = Article.objects.all()
template_name = "pure_pagination/pagination.html"

class PaginationTests(TestCase):
def setUp(self):
# Prepare a list of objects for pagination.
Expand Down Expand Up @@ -142,5 +150,17 @@ def test_pagination_containing_percent_char(self):
pass

def test_mixins(self):
pass

factory = RequestFactory()

req = factory.get('/list')
resp = ArticleList(request=req, kwargs={}).get(req)
self.assertEqual(200, resp.status_code)

with self.assertRaises(Http404):
req = factory.get('/list?page=foo')
ArticleList(request=req, kwargs={}).get(req)

with self.assertRaises(Http404):
req = factory.get('/list?page=1000')
ArticleList(request=req, kwargs={}).get(req)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup, find_packages

setup(name='django-pure-pagination',
version='0.2',
version='0.2.2',
author='James Pacileo',
long_description = open('README.rst').read(),
license='BSD',
Expand Down