Archived
1
0

make pagination work

This commit is contained in:
Jeff Becker 2016-11-06 11:17:46 -05:00
parent d81e709827
commit dafa5ca517
No known key found for this signature in database
GPG Key ID: AB950234D6EA286B
2 changed files with 16 additions and 6 deletions

View File

@ -6,7 +6,7 @@ urlpatterns = [
url(r'^ctl-(?P<page>[0-9]+)\.html$', views.modlog, name='old-modlog'), url(r'^ctl-(?P<page>[0-9]+)\.html$', views.modlog, name='old-modlog'),
url(r'^ctl/((?P<page>[0-9]+)/)?$', views.modlog, name='modlog'), url(r'^ctl/((?P<page>[0-9]+)/)?$', views.modlog, name='modlog'),
url(r'^overchan\.(?P<name>[a-zA-z0-9\.]+)-(?P<page>[0-9]+)\.html$', views.BoardView.as_view(), name='old-board'), url(r'^overchan\.(?P<name>[a-zA-z0-9\.]+)-(?P<page>[0-9]+)\.html$', views.BoardView.as_view(), name='old-board'),
url(r'^overchan\.(?P<name>[a-zA-z0-9\.]+)/((?P<page>[0-9]+)/)?$', views.BoardView.as_view(), name='board'), url(r'^overchan\.(?P<name>[a-zA-z0-9\.]+)/', views.BoardView.as_view(), name='board'),
url(r'^thread-(?P<op>[a-fA-F0-9\.]{40})\.html$', views.ThreadView.as_view(), name='old-thread'), url(r'^thread-(?P<op>[a-fA-F0-9\.]{40})\.html$', views.ThreadView.as_view(), name='old-thread'),
url(r'^t/(?P<op>[a-fA-F0-9\.]{40})/$', views.ThreadView.as_view(), name='thread'), url(r'^t/(?P<op>[a-fA-F0-9\.]{40})/$', views.ThreadView.as_view(), name='thread'),
url(r'^$', views.FrontPageView.as_view(), name='index'), url(r'^$', views.FrontPageView.as_view(), name='index'),

View File

@ -1,7 +1,9 @@
from django.core.urlresolvers import reverse
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from django.views import generic from django.views import generic
from .models import Post, Newsgroup from .models import Post, Newsgroup
class BoardView(generic.View): class BoardView(generic.View):
@ -9,9 +11,17 @@ class BoardView(generic.View):
context_object_name = 'threads' context_object_name = 'threads'
model = Post model = Post
def get(self, request, name, page): def get(self, request, name):
page = 0
if 'p' in request.GET:
page = request.GET['p']
newsgroup = 'overchan.{}'.format(name) newsgroup = 'overchan.{}'.format(name)
try:
page = int(page or "0") page = int(page or "0")
except:
page = 0
if page < 0:
page = 0
try: try:
group = Newsgroup.objects.get(name=newsgroup) group = Newsgroup.objects.get(name=newsgroup)
except Newsgroup.DoesNotExist: except Newsgroup.DoesNotExist:
@ -20,11 +30,11 @@ class BoardView(generic.View):
begin = page * group.posts_per_page begin = page * group.posts_per_page
end = begin + group.posts_per_page - 1 end = begin + group.posts_per_page - 1
roots = self.model.objects.filter(newsgroup=group, reference='').order_by('-last_bumped')[begin:end] roots = self.model.objects.filter(newsgroup=group, reference='').order_by('-last_bumped')[begin:end]
ctx = {'threads': roots,'page': page, 'name': newsgroup} ctx = {'threads': roots, 'page': page, 'name': newsgroup}
if page < group.max_pages: if page < group.max_pages:
ctx['nextpage'] = '/{}/{}/'.format(group.name, page + 1) ctx['nextpage'] = reverse('board', args=[name]) + '?p={}'.format(page + 1)
if page > 0: if page > 0:
ctx['prevpage'] = '/{}/{}/'.format(group.name, page - 1) ctx['prevpage'] = reverse('board', args=[name]) + '?p={}'.format(page - 1)
return render(request, self.template_name, ctx) return render(request, self.template_name, ctx)