Commits

jgsogo committed 7c1871b Draft

added filter to admin

  • Participants
  • Parent commits 3d78bbc

Comments (0)

Files changed (5)

File linneo/admin.py

+#!/usr/bin/env python
+# encoding: utf-8
+
+from django.contrib import admin
+
+from taxonomy.admin import TaxonAdmin
+from linneo.models import DomainTaxon, KingdomTaxon
+from linneo.filters import RankListFilter, InRankListFilter
+
+class MajorTaxonomicRankAdmin(TaxonAdmin):
+    exclude = ('rank',)
+    list_display = ('name', 'rank',)
+    mptt_level_indent = 20
+    list_filter = (InRankListFilter,)
+
+admin.site.register(DomainTaxon, MajorTaxonomicRankAdmin)
+admin.site.register(KingdomTaxon, MajorTaxonomicRankAdmin)
+
+
+# Override Taxon admin to provide filtering
+from taxonomy.models import Taxon
+admin.site.unregister(Taxon)
+
+class TaxonAdmin(TaxonAdmin):
+    list_display = ('name', 'rank',)
+    list_filter = (RankListFilter,)
+    mptt_level_indent = 20
+
+admin.site.register(Taxon, TaxonAdmin)

File linneo/filters.py

+#!/usr/bin/env python
+# encoding: utf-8
+
+from django.contrib.admin import SimpleListFilter
+from django.utils.translation import ugettext_lazy as _
+
+from taxonomy.models import TaxonRank
+
+class RankListFilter(SimpleListFilter):
+    title = _('taxonomic rank')
+
+    parameter_name = 'taxo_rank'
+
+    def lookups(self, request, model_admin):
+        return (
+            ('0', _('kingdom-group ranks')),
+            ('1', _('phylum-group ranks')),
+            ('2', _('cohort(botany)-group ranks')),
+            ('3', _('class-group ranks')),
+            ('4', _('division(zoology)-group ranks')),
+            ('5', _('legion-group ranks')),
+            ('6', _('cohort(zoology)-group ranks')),
+            ('7', _('order-group ranks')),
+            ('8', _('section-group ranks')),
+            ('9', _('family-group ranks')),
+            ('10', _('tribe-group ranks')),
+            ('11', _('genus-group ranks')),
+            ('12', _('species-group ranks')),
+            )
+
+    def queryset(self, request, queryset):
+        if self.value():
+            return queryset.filter(rank__rank=self.value())
+        else:
+            return queryset.all()
+
+class InRankListFilter(SimpleListFilter):
+    title = _('minorrank filter')
+    parameter_name = 'minortaxo_rank'
+
+    def lookups(self, request, model_admin):
+        rank = model_admin.model.objects._get_rank().rank
+        return ( (it.in_rank_order, it) for it in TaxonRank.objects.filter(rank=rank))
+
+    def queryset(self, request, queryset):
+        if self.value():
+            return queryset.filter(rank__in_rank_order=self.value())
+        else:
+            return queryset.all()

File linneo/managers.py

         except TaxonRank.MultipleObjectsReturned:
             raise ValueError("There are more than one taxon rank named %s (rank=%s)." %\
                              (self.__rank__rank, self.__slug))
+        return self.__rank
 
     def get_query_set(self):
         if not self.__rank:
             self._get_rank()
         return super(MajorTaxonomicRankManager, self).get_query_set().filter(rank=self.__rank)
+
+
+class TaxonomicRankGroupManager(TaxonManager):
+    def __init__(self, rank):
+        super(TaxonomicRankGroupManager, self).__init__()
+        self.__rank= rank
+
+    def get_query_set(self):
+        return super(TaxonomicRankGroupManager, self).get_queryset().filter(rank__rank=self.__rank)

File linneo/mixins.py

     compulsory = models.BooleanField(default=False)
 
     class Meta:
+        ordering = ['rank', 'in_rank_order']
         abstract = True
 
     def __unicode__(self):

File linneo/models.py

 
 from django.db import models
 
-from taxonomy.models import Taxon
+from taxonomy.models import Taxon, TaxonRank
+
+# Major ranks Linneo
 from linneo.managers import MajorTaxonomicRankManager
 
-class DomainTaxon(Taxon):
+class RankFromMixin(object):
+    def save(self, *args, **kwargs):
+        self.rank = self.__class__.objects._get_rank()
+        super(RankFromMixin, self).save(*args, **kwargs)
+
+
+class DomainTaxon(RankFromMixin, Taxon):
     objects = MajorTaxonomicRankManager(0, "domain")
     class Meta:
         proxy = True
 
-class KingdomTaxon(Taxon):
+class KingdomTaxon(RankFromMixin, Taxon):
     objects = MajorTaxonomicRankManager(0, "kingdom")
     class Meta:
         proxy = True
 
-class PhylumTaxon(Taxon):
+class PhylumTaxon(RankFromMixin, Taxon):
     objects = MajorTaxonomicRankManager(1, "phylum-division")
     class Meta:
         proxy = True
 
-class ClassTaxon(Taxon):
+class ClassTaxon(RankFromMixin, Taxon):
     objects = MajorTaxonomicRankManager(3, "class")
     class Meta:
         proxy = True
 
-class OrderTaxon(Taxon):
+class OrderTaxon(RankFromMixin, Taxon):
     objects = MajorTaxonomicRankManager(7, "order")
     class Meta:
         proxy = True
 
-class FamilyTaxon(Taxon):
+class FamilyTaxon(RankFromMixin, Taxon):
     objects = MajorTaxonomicRankManager(9, "family")
     class Meta:
         proxy = True
 
-class GenusTaxon(Taxon):
+class GenusTaxon(RankFromMixin, Taxon):
     objects = MajorTaxonomicRankManager(11, "genus")
     class Meta:
         proxy = True
 
-class SpeciesTaxon(Taxon):
+class SpeciesTaxon(RankFromMixin, Taxon):
     objects = MajorTaxonomicRankManager(12, "species")
     class Meta:
         proxy = True
-