From 669331abd1f2f25b814b41f1980c54e7a13f62a0 Mon Sep 17 00:00:00 2001 From: Sanj Date: Sat, 25 Feb 2012 16:20:37 +0530 Subject: [PATCH] change trans page to use paginator and accept page_no instead of start and end --- manifests/ships/views.py | 43 ++++++++++++++++++++++++---------------- manifests/urls.py | 2 +- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/manifests/ships/views.py b/manifests/ships/views.py index 5538d74..b8d8b82 100644 --- a/manifests/ships/views.py +++ b/manifests/ships/views.py @@ -9,6 +9,7 @@ from django.template import RequestContext from ox.django.shortcuts import render_to_json_response from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required +from django.core.paginator import Paginator, InvalidPage, EmptyPage @login_required def googletranslate(request, model, field): @@ -78,13 +79,13 @@ def dotranslate(request, model, field): @login_required -def translate(request, model, field): - start = request.GET.get("start", False) - end = request.GET.get("end", False) - if not start and not end: - limit = False - else: - limit = True +def translate(request, model, field, page_no=1): +# start = request.GET.get("start", False) +# end = request.GET.get("end", False) +# if not start and not end: +# limit = False +# else: +# limit = True try: m = models.__getattribute__(model) except AttributeError: @@ -95,13 +96,21 @@ def translate(request, model, field): trans_field = field + "_trans" q = Q(**{"%s__exact" % trans_field: ''}) qset = m.objects.values('id', field, trans_field).exclude(q).order_by(field).distinct(field) - if limit: - if not start: - start = 0 - if not end: - end = qset.count() - qset = qset[start:end] - for obj in qset: + paginator = Paginator(qset, 100) + try: + results = paginator.page(page_no) + except (EmptyPage, InvalidPage): + results = paginator.page(paginator.num_pages) + page_no = paginator.num_pages + + +# if limit: +# if not start: +# start = 0 +# if not end: +# end = qset.count() +# qset = qset[start:end] + for obj in results.object_list: response.append({ 'id': obj['id'], 'string': obj[field], @@ -109,10 +118,10 @@ def translate(request, model, field): }) context = RequestContext(request, { 'values': response, + 'page_no': page_no, + 'num_pages': paginator.num_pages, 'model': model, - 'field': field, - 'start': start, - 'end': end + 'field': field }) return render_to_response("trans.html", context) diff --git a/manifests/urls.py b/manifests/urls.py index 8b8a6a7..6f8dc8e 100644 --- a/manifests/urls.py +++ b/manifests/urls.py @@ -15,7 +15,7 @@ urlpatterns = patterns('', # Uncomment the next line to enable the admin: # for human translators - (r'^trans/(?P[A-Za-z].*?)/(?P[A-Za-z].*?)/', 'ships.views.translate'), + (r'^trans/(?P[A-Za-z].*?)/(?P[A-Za-z].*?)/(?P[0-9]?)', 'ships.views.translate'), # (r'^dotrans/(?P[A-Za-z].*?)/(?P[A-Za-z].*?)/', 'ships.views.dotranslate'), # for automated google translation