Permalink
Browse files

Alias admin with 2-char prefixes

  • Loading branch information...
1 parent 06ab3e5 commit 4ab92fdda98d707be5f3d05141f580d66380dc06 @guaq guaq committed Feb 24, 2013
Showing with 39 additions and 1 deletion.
  1. +38 −1 services/admin.py
  2. +1 −0 services/models.py
View
@@ -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)
View
@@ -79,6 +79,7 @@ class Meta:
("manage_aliases", "Can manage aliases"),
)
ordering = ["name"]
+ verbose_name_plural = "aliases"
def expire(self, time=None):
if time == None:

0 comments on commit 4ab92fd

Please sign in to comment.