Hi I am new to Django and working on Pootle project.
I would like to sort by Overall completion by default in the pootle index page. for example, http://pootle.locamotion.org/ lists the languages and sorted by names. you can click sort buttons to see sort by completion. but I would like to show this list sorted by completion whenever load a page.
in pootle/local_apps/pootle_app/templates/index/index.html,
<table class="sortable">
<tr>
<th>{% trans 'Language' %}</th>
<th>{% trans 'Overall Completion' %}</th>
<th>{% trans 'Last Activity' %}</th>
</tr>
{% for item in languages %}
{% ifnotequal item.total 0 %}
<tr class="{% cycle 'even' 'odd' %}">
<td class="language">
<a href="{% filter l %}/{{ item.code }}/{% endfilter %}">{{ item.name }}</a></td>
<td>
<div class="sortkey">{{ item.transper }}</div>
<div class="graph" title="{{ item.completed_title }}" dir="{% if LANGUAGE_BIDI %}rtl{% else %}ltr{% endif %}">
<div class="translated" style="width: {{ item.transper }}px"></div>
{% if item.fuzzy %}
<div class="fuzzy" style="{%if LANGUAGE_BIDI%}right{%else%}left{%endif%}: {{ item.transper }}px; width: {{ item.fuzzyper }}px"></div>
{% endif %}
{% if item.untrans %}
<div class="untranslated" style="{% if LANGUAGE_BIDI %}right{% else %}left{% endif %}: {{ item.transper|add:item.fuzzyper }}px; width: {{ item.untransper }}px"></div>
{% endif %}
</div>
</td>
<td>{{ item.lastactivity }}</td>
</tr>
{% endifnotequal %}
{% endfor %}
</table>
item.transper is the key that I want to sort by.
and this is how item and language is defined:
def get_items(request, model, get_last_action, name_func):
items = []
if not check_permission('view', request):
return items
for item in model.objects.iterator():
stats = item.getquickstats()
stats = add_percentages(stats)
lastact = get_last_action(item)
items.append({
'code': item.code,
'name': name_func(item.fullname),
'lastactivity': lastact,
'trans': stats["translatedsourcewords"],
'fuzzy': stats["fuzzysourcewords"],
'untran开发者_如何学Gos': stats["untranslatedsourcewords"],
'total': stats["totalsourcewords"],
'transper': stats["translatedpercentage"],
'fuzzyper': stats["fuzzypercentage"],
'untransper': stats["untranslatedpercentage"],
'completed_title': _("%(percentage)d%% complete",
{'percentage': stats['translatedpercentage']}),
})
items.sort(lambda x, y: locale.strcoll(x['name'], y['name']))
return items
def getlanguages(request):
def get_last_action(item):
try:
return Submission.objects.filter(translation_project__language=item).latest()
except Submission.DoesNotExist:
return ''
return get_items(request, Language, get_last_action, tr_lang)
what should I change this so that I see sort by Overall Completion by default?
thank you so much for any suggestions
Change
items.sort(lambda x, y: locale.strcoll(x['name'], y['name']))
to
items.sort(lambda x, y: cmp(x['transper'], y['transper']))
(This works well with the builtin cmp
function because the transper
fields are converted to int
s by the nice_percentage
function, which is called as part of add_percentages
)
If that doesn't produce the order you wanted, simply switch objects x
and y
.
If instead you need to sort it in the template (it's typically a bad idea to mess with third-party apps' source code), you can use the dictsort
filter:
{% for item in languages|dictsort:"transper" %}
Again, if that's not the order you wanted, use dictsortreversed
.
The latest development version of Pootle, to become Pootle 2.2, now remembers which column you have selected to sort on. So a page reload will now remember the order you last used.
精彩评论