Commit 20479da3 authored by Giorgos Verigakis's avatar Giorgos Verigakis

Add paging support

New entry added in settings.

Refs #1237
parent df82b3cf
This diff is collapsed.
......@@ -28,6 +28,28 @@
</tbody>
</table>
{% if pages|length > 1 %}
<div class="pagination">
<ul>
{% if prev %}
<li class="prev"><a href="?page={{ prev }}">&larr; Previous</a></li>
{% else %}
<li class="prev disabled"><a href="#">&larr; Previous</a></li>
{% endif %}
{% for p in pages %}
<li{% if page == p %} class="active"{% endif %}><a href="?page={{ p }}">{{ p }}</a></li>
{% endfor %}
{% if next %}
<li class="next"><a href="?page={{ next }}">&rarr; Next</a></li>
{% else %}
<li class="next disabled"><a href="#">&rarr; Next</a></li>
{% endif %}
</ul>
</div>
{% endif %}
<a class="btn success" href="{% url admin.views.users_create %}">Create a user</a>
<br /><br />
{% endblock body %}
......@@ -32,6 +32,7 @@
# or implied, of GRNET S.A.
from functools import wraps
from math import ceil
from django.http import HttpResponse, HttpResponseRedirect
from django.utils.http import urlencode
......@@ -42,10 +43,12 @@ from pithos import settings
from pithos.aai.models import PithosUser
def render(template, tab, **kwargs):
def render_response(template, tab=None, status=200, **kwargs):
if tab is None:
tab = template.partition('_')[0]
kwargs.setdefault('tab', tab)
return render_to_string(template, kwargs)
html = render_to_string(template, kwargs)
return HttpResponse(html, status=status)
def requires_admin(func):
......@@ -65,22 +68,34 @@ def requires_admin(func):
def index(request):
stats = {}
stats['users'] = PithosUser.objects.count()
html = render('index.html', 'home', stats=stats)
return HttpResponse(html)
return render_response('index.html', tab='home', stats=stats)
@requires_admin
def users_list(request):
try:
page = int(request.GET.get('page', 1))
except ValueError:
page = 1
offset = max(0, page - 1) * settings.ADMIN_PAGE_LIMIT
limit = offset + settings.ADMIN_PAGE_LIMIT
users = PithosUser.objects.order_by('id')
html = render('users_list.html', 'users', users=users)
return HttpResponse(html)
npages = int(ceil(1.0 * users.count() / settings.ADMIN_PAGE_LIMIT))
prev = page - 1 if page > 1 else None
next = page + 1 if page < npages else None
return render_response('users_list.html',
users=users[offset:limit],
pages=range(1, npages + 1),
page=page,
prev=prev,
next=next)
@requires_admin
def users_create(request):
if request.method == 'GET':
html = render('users_create.html', 'users')
return HttpResponse(html)
return render_response('users_create.html')
if request.method == 'POST':
user = PithosUser()
user.uniq = request.POST.get('uniq')
......@@ -96,8 +111,7 @@ def users_create(request):
@requires_admin
def users_info(request, user_id):
user = PithosUser.objects.get(id=user_id)
html = render('users_info.html', 'users', user=user)
return HttpResponse(html)
return render_response('users_info.html', user=user)
@requires_admin
......
......@@ -164,3 +164,5 @@ AUTH_TOKEN_DURATION = 30 * 24
# Default quota for new users.
DEFAULT_QUOTA = 10 * 1024 * 1024 * 1024
ADMIN_PAGE_LIMIT = 10
Markdown is supported
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