Commit 8e08ff30 authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

Automatically build hiearchy on GET retrieve

parent da536d6d
Pipeline #17539 failed with stages
in 2 minutes and 15 seconds
from rest_framework.response import Response
from metagenedb.api.catalog.filters import TaxonomyFilter
from metagenedb.apps.catalog.models import Taxonomy
from metagenedb.apps.catalog.serializers import TaxonomySerializer
......@@ -10,3 +12,9 @@ class TaxonomyViewSet(BulkViewSet):
serializer_class = TaxonomySerializer
lookup_field = 'tax_id'
filterset_class = TaxonomyFilter
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
instance.build_parental_hierarchy()
serializer = self.get_serializer(instance)
return Response(serializer.data)
......@@ -100,9 +100,11 @@ class Taxonomy(models.Model):
def build_parental_hierarchy(self):
hierarchy = {}
if self.name != 'root' and self.parent is not None:
hierarchy[self.rank] = self.tax_id
hierarchy[self.rank] = self
hierarchy = {**hierarchy, **self.parent.build_parental_hierarchy()}
hierarchy['tax_id'] = self.tax_id
for level, value in hierarchy.items():
setattr(self, level, value)
self.save()
return hierarchy
class Meta:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment