# HG changeset patch # User neko259 # Date 2017-02-17 16:19:38 # Node ID b4c069739c1fe0a14918380a630d1cccbc08e488 # Parent dcebe626a135d42b21efef95658f5b060240ddf8 Moved pagination related stuff to the mixin diff --git a/boards/views/all_threads.py b/boards/views/all_threads.py --- a/boards/views/all_threads.py +++ b/boards/views/all_threads.py @@ -89,7 +89,7 @@ class AllThreadsView(PostMixin, FileUplo params[PARAMETER_MAX_FILES] = settings.get_int('Forms', 'MaxFileCount') paginator.set_url(self.get_reverse_url(), request.GET.dict()) - self.get_page_context(paginator, params, page) + params.update(self.get_page_context(paginator, page)) return render(request, TEMPLATE, params) @@ -112,16 +112,6 @@ class AllThreadsView(PostMixin, FileUplo return self.get(request, form) - def get_page_context(self, paginator, params, page): - """ - Get pagination context variables - """ - - params[PARAMETER_PAGINATOR] = paginator - current_page = paginator.page(int(page)) - params[PARAMETER_CURRENT_PAGE] = current_page - self.set_page_urls(paginator, params) - def get_reverse_url(self): return reverse('index') diff --git a/boards/views/feed.py b/boards/views/feed.py --- a/boards/views/feed.py +++ b/boards/views/feed.py @@ -7,17 +7,13 @@ from boards.abstracts.settingsmanager im from boards.models import Post from boards.views.base import BaseBoardView from boards.views.posting_mixin import PostMixin +from boards.views.mixins import PaginatedMixin POSTS_PER_PAGE = settings.get_int('View', 'PostsPerPage') -PARAMETER_CURRENT_PAGE = 'current_page' -PARAMETER_PAGINATOR = 'paginator' PARAMETER_POSTS = 'posts' PARAMETER_QUERIES = 'queries' -PARAMETER_PREV_LINK = 'prev_page_link' -PARAMETER_NEXT_LINK = 'next_page_link' - TEMPLATE = 'boards/feed.html' DEFAULT_PAGE = 1 @@ -96,7 +92,7 @@ class ImageFilter(FeedFilter): return 'File: {}'.format(image) -class FeedView(PostMixin, BaseBoardView): +class FeedView(PostMixin, PaginatedMixin, BaseBoardView): filters = ( TripcodeFilter, FavoritesFilter, @@ -129,23 +125,7 @@ class FeedView(PostMixin, BaseBoardView) paginator.set_url(reverse('feed'), request.GET.dict()) - self.get_page_context(paginator, params, page) + params.update(self.get_page_context(paginator, page)) return render(request, TEMPLATE, params) - # TODO Dedup this into PagedMixin - def get_page_context(self, paginator, params, page): - """ - Get pagination context variables - """ - - params[PARAMETER_PAGINATOR] = paginator - current_page = paginator.page(int(page)) - params[PARAMETER_CURRENT_PAGE] = current_page - if current_page.has_previous(): - params[PARAMETER_PREV_LINK] = paginator.get_page_url( - current_page.previous_page_number()) - if current_page.has_next(): - params[PARAMETER_NEXT_LINK] = paginator.get_page_url( - current_page.next_page_number()) - diff --git a/boards/views/mixins.py b/boards/views/mixins.py --- a/boards/views/mixins.py +++ b/boards/views/mixins.py @@ -4,6 +4,11 @@ import boards PARAM_NEXT = 'next' PARAMETER_METHOD = 'method' +PARAMETER_CURRENT_PAGE = 'current_page' +PARAMETER_PAGINATOR = 'paginator' + +PARAMETER_PREV_LINK = 'prev_page_link' +PARAMETER_NEXT_LINK = 'next_page_link' class DispatcherMixin: """ @@ -35,6 +40,22 @@ class FileUploadMixin: class PaginatedMixin: - def set_page_urls(self, paginator, params): - params['prev_page_link'] = paginator.get_prev_page_url() - params['next_page_link'] = paginator.get_next_page_url() + def get_page_context(self, paginator, page): + """ + Get pagination context variables + """ + + params = {} + + params[PARAMETER_PAGINATOR] = paginator + current_page = paginator.page(int(page)) + params[PARAMETER_CURRENT_PAGE] = current_page + if current_page.has_previous(): + params[PARAMETER_PREV_LINK] = paginator.get_page_url( + current_page.previous_page_number()) + if current_page.has_next(): + params[PARAMETER_NEXT_LINK] = paginator.get_page_url( + current_page.next_page_number()) + + return params +