Fakultas Ilmu Komputer UI

Commit 9654ba36 authored by Azhar Rais's avatar Azhar Rais
Browse files

[REFACTOR] Move pagination func from ForumHomePage

parent 2d742024
Pipeline #59977 passed with stages
in 12 minutes and 57 seconds
......@@ -14,39 +14,40 @@ URL_FORUM_HOME_PAGE = '/forum'
URL_LOGIN_PAGE = '/login'
def pagination(current, last):
delta = 2
left = current - delta
right = current + delta + 1
pages = []
pages_with_dots = []
for i in range(last + 1):
if i == 0:
continue
if i == 1 or i == last or (left <= i < right):
pages.append(i)
temp = None
for page in pages:
if temp:
if page - temp == 2:
pages_with_dots.append(temp + 1)
elif page - temp != 1:
pages_with_dots.append("...")
pages_with_dots.append(page)
temp = page
return pages_with_dots
class ForumHomePage(ListView):
paginate_by = 10
queryset = Discussion.objects.order_by('-updated_at')
template_name = 'forum/forum_home.html'
def pagination(self, current, last):
delta = 2
left = current - delta
right = current + delta + 1
pages = []
pages_with_dots = []
for i in range(last + 1):
if i == 0:
continue
if i == 1 or i == last or (left <= i < right):
pages.append(i)
temp = None
for page in pages:
if temp:
if page - temp == 2:
pages_with_dots.append(temp + 1)
elif page - temp != 1:
pages_with_dots.append("...")
pages_with_dots.append(page)
temp = page
return pages_with_dots
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
page_context = context.get('page_obj')
context['pages'] = self.pagination(page_context.number, page_context.paginator.num_pages)
context['pages'] = pagination(page_context.number, page_context.paginator.num_pages)
return context
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment