Paginate everything on the list page

This commit is contained in:
2017-02-07 22:07:24 -05:00
parent ebbbf6b4cf
commit ae5250e688
4 changed files with 81 additions and 36 deletions

View File

@@ -1,6 +1,7 @@
from django.shortcuts import HttpResponse, HttpResponseRedirect
from django.template import loader
from django.utils import timezone
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from models import Item
@@ -25,16 +26,33 @@ def myItems(user):
return Item.objects.filter(Q(created_by=user) | Q(committee__in=comms)) \
.order_by('-date_filed', 'desc')
def pageItems(request, items, status):
if status != 'M':
p = Paginator(items.filter(status=status), 8)
else:
p = Paginator(items, 8)
page = request.GET.get(status)
try:
return p.page(page)
except PageNotAnInteger:
return p.page(1)
except EmptyPage:
return p.page(p.num_pages)
@login_required
def list(request):
template = loader.get_template('items/list.html')
items = myItems(request.user)
mine = Item.objects.filter(created_by=request.user).order_by('-date_filed')
context = {
'preapproved': items.filter(status=Item.PREAPPROVED),
'processed': items.filter(status=Item.PROCESSED),
'newitems': items.filter(status=Item.NEW),
'rejected': items.filter(status=Item.REJECTED),
'preapproved': pageItems(request, items, Item.PREAPPROVED),
'processed': pageItems(request, items, Item.PROCESSED),
'newitems': pageItems(request, items, Item.NEW),
'rejected': pageItems(request, items, Item.REJECTED),
'mine': pageItems(request, mine, 'M'),
}
return HttpResponse(template.render(context, request))

View File

@@ -9,6 +9,27 @@ section {
margin-left: auto;
}
.items {
a.page {
display: inline-block;
margin: $pad-m;
text-decoration: none;
color: $midgray;
font-size: 12px;
float: right;
}
a.page:hover {
text-decoration: underline;
color: $purple;
}
a.page.empty:hover {
color: $midgray;
text-decoration: none;
}
}
.item {
padding: $pad-m $pad-xl;
margin: 0px;

View File

@@ -9,49 +9,33 @@
{% endif %}
{% if preapproved %}
<section>Pre-Approved</section>
<div>
{% for I in preapproved %}
{% include "./item.html" %}
{% empty %}
<div class="empty">No Reimbursements</div>
{% endfor %}
</div>
{% with items=preapproved pagination_key="C" name="Pre-Approved" %}
{% include "./section.html" %}
{% endwith %}
{% endif %}
{% if newitems %}
<section>New Items</section>
<div>
{% for I in newitems %}
{% include "./item.html" %}
{% empty %}
<div class="empty">No Reimbursements</div>
{% endfor %}
</div>
{% with items=newitems pagination_key="N" name="New Items" %}
{% include "./section.html" %}
{% endwith %}
{% endif %}
{% if processed %}
<section>Approved</section>
<div>
{% for I in processed %}
{% include "./item.html" %}
{% empty %}
<div class="empty">No Reimbursements</div>
{% endfor %}
</div>
{% with items=processed pagination_key="P" name="Approved" %}
{% include "./section.html" %}
{% endwith %}
{% endif %}
{% if rejected %}
<section>Rejected</section>
<div>
{% for I in rejected %}
{% include "./item.html" %}
{% empty %}
<div class="empty">No Reimbursements</div>
{% endfor %}
</div>
{% with items=rejected pagination_key="R" name="Rejected" %}
{% include "./section.html" %}
{% endwith %}
{% endif %}
{% with items=mine pagination_key="M" name="My Reimbursements" %}
{% include "./section.html" %}
{% endwith %}
{% endblock %}
{% block bottom %}

View File

@@ -0,0 +1,22 @@
<section>{{ name }}</section>
<div class="items">
{% for I in items %}
{% include "./item.html" %}
{% empty %}
<div class="empty">No Reimbursements</div>
{% endfor %}
{% if items.has_next %}
<a class="page" href="?{{ pagination_key }}={{ items.next_page_number }}">
Older &rarr;
</a>
{% else %}
<a class="page empty" href="#">Older &rarr;</a>
{% endif %}
{% if items.has_previous %}
<a class="page" href="?{{ pagination_key }}={{ items.previous_page_number }}">
&larr; Newer
</a>
{% else %}
<a class="page empty" href="#">&larr; Newer</a>
{% endif %}
</div>