本文整理汇总了Python中translations.query.order_by_translation函数的典型用法代码示例。如果您正苦于以下问题:Python order_by_translation函数的具体用法?Python order_by_translation怎么用?Python order_by_translation使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了order_by_translation函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_sorting_mixed
def test_sorting_mixed(self):
translation.activate('de')
q = TranslatedModel.objects.all()
expected = [1, 4, 3]
eq_(ids(order_by_translation(q, 'name')), expected)
eq_(ids(order_by_translation(q, '-name')), list(reversed(expected)))
示例2: test_sorting_by_field
def test_sorting_by_field(self):
field = TranslatedModel._meta.get_field('default_locale')
TranslatedModel.get_fallback = classmethod(lambda cls: field)
translation.activate('de')
q = TranslatedModel.objects.all()
expected = [3, 1, 4]
eq_(ids(order_by_translation(q, 'name')), expected)
eq_(ids(order_by_translation(q, '-name')), list(reversed(expected)))
del TranslatedModel.get_fallback
示例3: search_tools
def search_tools(request, category=None):
APP, TYPE = request.APP, amo.ADDON_SEARCH
qs = Category.objects.filter(application=APP.id, type=TYPE)
categories = order_by_translation(qs, 'name')
# Feature page should list both extensions and search Add-ons
# but other pages should only list search Add-ons
types = [TYPE]
default = 'featured'
if request.GET.get('sort', default) == 'featured':
types.append(amo.ADDON_EXTENSION)
addons, filter, unreviewed = addon_listing(
request, types, SearchToolsFilter, default)
if category is not None:
category = get_object_or_404(qs, slug=category)
addons = addons.filter(categories__id=category.id)
addons = amo.utils.paginate(request, addons)
base = (Addon.objects.listed(request.APP, amo.STATUS_PUBLIC)
.filter(type=amo.ADDON_EXTENSION))
search_extensions = SearchExtensionsFilter(
request, base, 'sort', 'popular')
return jingo.render(request, 'browse/search_tools.html',
{'categories': categories, 'category': category,
'addons': addons, 'filter': filter,
'search_extensions_filter': search_extensions,
'unreviewed': unreviewed})
示例4: themes
def themes(request, category=None):
q = Category.objects.filter(application=request.APP.id,
type=amo.ADDON_THEME)
categories = order_by_translation(q, 'name')
addons, filter, unreviewed = _listing(request, amo.ADDON_THEME)
if category is not None:
try:
category = dict((c.slug, c) for c in categories)[category]
except KeyError:
raise http.Http404()
addons = addons.filter(categories__id=category.id)
count = addons.with_index(addons='type_status_inactive_idx').count()
themes = amo.utils.paginate(request, addons, count=count)
# Pre-selected category for search form
search_cat = '%s,0' % amo.ADDON_THEME
return jingo.render(request, 'browse/themes.html',
{'categories': categories,
'themes': themes, 'category': category,
'sorting': filter.field,
'sort_opts': filter.opts,
'unreviewed': unreviewed,
'search_cat': search_cat})
示例5: themes
def themes(request, category=None):
q = Category.objects.filter(application=request.APP.id,
type=amo.ADDON_THEME)
categories = order_by_translation(q, 'name')
addons, filter, unreviewed = _listing(request, amo.ADDON_THEME)
total_count = addons.count()
if category is None:
selected = _Category(_('All'), total_count, '')
else:
selected = dict((c.slug, c) for c in categories)[category]
addons = addons.filter(categories__slug=category)
themes = amo.utils.paginate(request, addons)
# Pre-selected category for search form
search_cat = '%s,0' % amo.ADDON_THEME
return jingo.render(request, 'browse/themes.html',
{'categories': categories, 'total_count': total_count,
'themes': themes, 'selected': selected,
'sorting': filter.sorting,
'sort_opts': filter.opts,
'unreviewed': unreviewed,
'search_cat': search_cat})
示例6: personas
def personas(request, category=None):
TYPE = amo.ADDON_PERSONA
q = Category.objects.filter(application=request.APP.id,
type=TYPE)
categories = order_by_translation(q, 'name')
base = Addon.objects.valid().filter(type=TYPE)
featured = base & Addon.objects.featured(request.APP)
is_homepage = category is None and 'sort' not in request.GET
if category is not None:
category = get_object_or_404(q, slug=category)
base = base.filter(categories__id=category.id)
filter = PersonasFilter(request, base, key='sort', default='up-and-coming')
if 'sort' in request.GET:
template = 'grid.html'
else:
template = 'category_landing.html'
# Pass the count from base instead of letting it come from
# filter.qs.count() since that would join against personas.
addons = amo.utils.paginate(request, filter.qs, 30, count=base.count())
search_cat = '%s,%s' % (TYPE, category.id if category else 0)
return jingo.render(request, 'browse/personas/' + template,
{'categories': categories, 'category': category,
'filter': filter, 'addons': addons,
'featured': featured, 'is_homepage': is_homepage,
'search_cat': search_cat})
示例7: _personas
def _personas(request):
"Handle the request for persona searches."
form = SecondarySearchForm(request.GET)
form.is_valid()
query = form.cleaned_data.get('q', '')
search_opts = {}
search_opts['limit'] = form.cleaned_data.get('pp', DEFAULT_NUM_RESULTS)
page = form.cleaned_data.get('page') or 1
search_opts['offset'] = (page - 1) * search_opts['limit']
client = PersonasClient()
q = Category.objects.filter(application=request.APP.id,
type=amo.ADDON_PERSONA)
categories = order_by_translation(q, 'name')
try:
results = client.query(query, **search_opts)
except SearchError:
return jingo.render(request, 'search/down.html', {}, status=503)
pager = amo.utils.paginate(request, results, search_opts['limit'])
c = {
'pager': pager,
'form': form,
'categories': categories,
}
return jingo.render(request, 'search/personas.html', c)
示例8: filter
def filter(self, field):
if field == "added":
return self.base_queryset.order_by("collectionaddon__created")
elif field == "name":
return order_by_translation(self.base_queryset, "name")
elif field == "popular":
return self.base_queryset.order_by("-weekly_downloads").with_index(addons="downloads_type_idx")
示例9: my_addons
def my_addons(self, n=8, with_unlisted=False):
"""Returns n addons"""
addons = self.addons
if with_unlisted:
addons = self.addons.model.with_unlisted.filter(authors=self)
qs = order_by_translation(addons, 'name')
return qs[:n]
示例10: personas_listing
def personas_listing(request, category_slug=None):
# Common pieces using by browse and search.
TYPE = amo.ADDON_PERSONA
q = Category.objects.filter(type=TYPE)
categories = order_by_translation(q, 'name')
frozen = list(FrozenAddon.objects.values_list('addon', flat=True))
base = (Addon.objects.public().filter(type=TYPE)
.exclude(id__in=frozen)
.extra(select={'_app': request.APP.id}))
cat = None
if category_slug is not None:
try:
cat = Category.objects.filter(slug=category_slug, type=TYPE)[0]
except IndexError:
# Maybe it's a Complete Theme?
try:
cat = Category.objects.filter(slug=category_slug,
type=amo.ADDON_THEME)[0]
except IndexError:
raise Http404
else:
# Hey, it was a Complete Theme.
url = reverse('browse.themes', args=[cat.slug])
if 'sort' in request.GET:
url = amo.utils.urlparams(url, sort=request.GET['sort'])
return redirect(url, permanent=not settings.DEBUG)
base = base.filter(categories__id=cat.id)
filter_ = PersonasFilter(request, base, key='sort',
default='up-and-coming')
return categories, filter_, base, cat
示例11: _do_sort_queue_obj
def _do_sort_queue_obj(request, qs, date_sort):
"""
Column sorting logic based on request GET parameters.
Deals with objects with joins on the Addon (e.g. RereviewQueue, Version).
Returns qs of apps.
"""
sort_type, order = clean_sort_param(request, date_sort=date_sort)
sort_str = sort_type
if sort_type not in [date_sort, 'name']:
sort_str = 'addon__' + sort_type
# sort_str includes possible joins when ordering.
# sort_type is the name of the field to sort on without desc/asc markers.
# order_by is the name of the field to sort on with desc/asc markers.
order_by = ('-' if order == 'desc' else '') + sort_str
# Sort.
if sort_type == 'name':
# Sorting by name translation through an addon foreign key.
return order_by_translation(
Webapp.objects.filter(id__in=qs.values_list('addon', flat=True)),
order_by)
elif sort_type == 'num_abuse_reports':
qs = qs.annotate(num_abuse_reports=Count('abuse_reports'))
# Convert sorted queue object queryset to sorted app queryset.
sorted_app_ids = qs.order_by(order_by).values_list('addon', flat=True)
qs = Webapp.objects.filter(id__in=sorted_app_ids)
return manual_order(qs, sorted_app_ids, 'addons.id')
示例12: themes
def themes(request, category=None):
q = Category.objects.filter(application=request.APP.id, type=amo.ADDON_THEME)
categories = order_by_translation(q, "name")
addons, filter, unreviewed = _listing(request, amo.ADDON_THEME)
total_count = addons.count()
if category is None:
selected = _Category(_("All"), total_count, "")
else:
selected = dict((c.slug, c) for c in categories)[category]
addons = addons.filter(categories__slug=category)
themes = amo.utils.paginate(request, addons)
# Pre-selected category for search form
search_cat = "%s,0" % amo.ADDON_THEME
return jingo.render(
request,
"browse/themes.html",
{
"categories": categories,
"total_count": total_count,
"themes": themes,
"selected": selected,
"sorting": filter.sorting,
"sort_opts": filter.opts,
"unreviewed": unreviewed,
"search_cat": search_cat,
},
)
示例13: personas
def personas(request, category=None):
TYPE = amo.ADDON_PERSONA
q = Category.objects.filter(application=request.APP.id, type=TYPE)
categories = order_by_translation(q, "name")
base = Addon.objects.valid().filter(type=TYPE)
if category is not None:
category = get_object_or_404(q, slug=category)
base = base.filter(categories__id=category.id)
filter = PersonasFilter(request, base, key="sort", default="up-and-coming")
if "sort" in request.GET:
template = "grid.html"
else:
template = "category_landing.html"
addons = amo.utils.paginate(request, filter.qs, 30)
search_cat = "%s,%s" % (TYPE, category.id if category else 0)
return jingo.render(
request,
"browse/personas/" + template,
{"categories": categories, "category": category, "filter": filter, "addons": addons, "search_cat": search_cat},
)
示例14: developers
def developers(request, addon, page):
if 'version' in request.GET:
qs = addon.versions.filter(files__status__in=amo.VALID_STATUSES)
version = get_list_or_404(qs, version=request.GET['version'])[0]
else:
version = addon.current_version
if 'src' in request.GET:
contribution_src = src = request.GET['src']
else:
# Download src and contribution_src are be different.
src = {'developers': 'developers',
'installed': 'meet-the-developer-post-install',
'roadblock': 'meetthedeveloper_roadblock'}.get(page, None)
contribution_src = {'developers': 'meet-developers',
'installed': 'post-download',
'roadblock': 'roadblock'}.get(page, None)
if addon.is_persona():
raise http.Http404()
author_addons = order_by_translation(addon.authors_other_addons, 'name')
return jingo.render(request, 'addons/developers.html',
{'addon': addon, 'author_addons': author_addons,
'page': page, 'src': src,
'contribution_src': contribution_src,
'version': version})
示例15: themes
def themes(request, category=None):
APP, THEME = request.APP, amo.ADDON_THEME
status = [amo.STATUS_PUBLIC]
experimental = 'on' if request.GET.get('experimental', False) else None
if experimental:
status.append(amo.STATUS_SANDBOX)
q = Category.objects.filter(application=APP.id, type=THEME)
categories = order_by_translation(q, 'name')
addons = Addon.objects.listed(APP, *status).filter(type=THEME).distinct()
total_count = addons.count()
sorting, sort_opts, addons = AddonSorter(request, addons, 'downloads')
if category is None:
selected = _Category(_('All'), total_count, '')
else:
selected = dict((c.slug, c) for c in categories)[category]
addons = addons.filter(categories__slug=category)
themes = amo.utils.paginate(request, addons)
return jingo.render(request, 'browse/themes.html',
{'categories': categories, 'total_count': total_count,
'themes': themes, 'selected': selected,
'sorting': sorting, 'sort_opts': sort_opts,
'experimental': experimental})