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 ox.django.shortcuts import render_to_json_response
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, InvalidPage, EmptyPage
@login_required @login_required
def googletranslate(request, model, field): def googletranslate(request, model, field):
@ -78,13 +79,13 @@ def dotranslate(request, model, field):
@login_required @login_required
def translate(request, model, field): def translate(request, model, field, page_no=1):
start = request.GET.get("start", False) # start = request.GET.get("start", False)
end = request.GET.get("end", False) # end = request.GET.get("end", False)
if not start and not end: # if not start and not end:
limit = False # limit = False
else: # else:
limit = True # limit = True
try: try:
m = models.__getattribute__(model) m = models.__getattribute__(model)
except AttributeError: except AttributeError:
@ -95,13 +96,21 @@ def translate(request, model, field):
trans_field = field + "_trans" trans_field = field + "_trans"
q = Q(**{"%s__exact" % trans_field: ''}) q = Q(**{"%s__exact" % trans_field: ''})
qset = m.objects.values('id', field, trans_field).exclude(q).order_by(field).distinct(field) qset = m.objects.values('id', field, trans_field).exclude(q).order_by(field).distinct(field)
if limit: paginator = Paginator(qset, 100)
if not start: try:
start = 0 results = paginator.page(page_no)
if not end: except (EmptyPage, InvalidPage):
end = qset.count() results = paginator.page(paginator.num_pages)
qset = qset[start:end] page_no = paginator.num_pages
for obj in qset:
# 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({ response.append({
'id': obj['id'], 'id': obj['id'],
'string': obj[field], 'string': obj[field],
@ -109,10 +118,10 @@ def translate(request, model, field):
}) })
context = RequestContext(request, { context = RequestContext(request, {
'values': response, 'values': response,
'page_no': page_no,
'num_pages': paginator.num_pages,
'model': model, 'model': model,
'field': field, 'field': field
'start': start,
'end': end
}) })
return render_to_response("trans.html", context) return render_to_response("trans.html", context)

View File

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