change trans page to use paginator and accept page_no instead of start and end

This commit is contained in:
Sanj 2012-02-25 16:20:37 +05:30
parent b8bafb4d08
commit 669331abd1
2 changed files with 27 additions and 18 deletions

View File

@ -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)

View File

@ -15,7 +15,7 @@ urlpatterns = patterns('',
# Uncomment the next line to enable the admin:
# for human translators
(r'^trans/(?P<model>[A-Za-z].*?)/(?P<field>[A-Za-z].*?)/', 'ships.views.translate'),
(r'^trans/(?P<model>[A-Za-z].*?)/(?P<field>[A-Za-z].*?)/(?P<page_no>[0-9]?)', 'ships.views.translate'),
# (r'^dotrans/(?P<model>[A-Za-z].*?)/(?P<field>[A-Za-z].*?)/', 'ships.views.dotranslate'),
# for automated google translation