Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,43 @@ | ||
from django.contrib import admin | ||
from django.contrib.admin import SimpleListFilter | ||
from django.utils.translation import ugettext_lazy as _ | ||
from services.models import Service, ServiceType, Alias | ||
|
||
|
||
# See | ||
# <http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter> | ||
# for documentation | ||
class StartsWithListFilter(SimpleListFilter): | ||
title = _('Starts with') | ||
|
||
parameter_name = 'starts_with' | ||
|
||
def lookups(self, request, model_admin): | ||
def first_two(s): | ||
s = unicode(s) | ||
if len(s) < 2: | ||
return s | ||
else: | ||
return s[:2] | ||
|
||
prefixes = [first_two(alias.name) | ||
for alias in model_admin.model.objects.only('name')] | ||
|
||
prefixes = sorted(set(prefixes)) | ||
|
||
return [(prefix, prefix) for prefix in prefixes] | ||
|
||
def queryset(self, request, queryset): | ||
if self.value(): | ||
return queryset.filter(name__istartswith=self.value()) | ||
else: | ||
return queryset | ||
|
||
|
||
class AliasAdmin(admin.ModelAdmin): | ||
list_filter = (StartsWithListFilter,) | ||
|
||
|
||
admin.site.register(Service) | ||
admin.site.register(ServiceType) | ||
admin.site.register(Alias) | ||
admin.site.register(Alias, AliasAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters