Commit 7cd9339c authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

Add details when getting taxonomy of function item

parent 93939b4a
Pipeline #17907 passed with stages
in 2 minutes and 8 seconds
from collections import OrderedDict
from rest_framework import serializers
class AsymetricSlugRelatedField(serializers.SlugRelatedField):
def to_representation(self, value):
return self.serializer_class(value).data
# Get choices used by DRF autodoc and expect to_representation() to return an ID
# We overload to use item.pk instead of to_representation()
def get_choices(self, cutoff=None):
queryset = self.get_queryset()
if queryset is None:
return {}
if cutoff is not None:
queryset = queryset[:cutoff]
return OrderedDict([
(
item.pk,
self.display_value(item)
)
for item in queryset
])
# DRF skip validations when it only has id, we deactivate that
def use_pk_only_optimization(self):
return False
@classmethod
def from_serializer(cls, serializer, name=None, *args, **kwargs):
if name is None:
name = f"{serializer.__class__.__name__}AsymetricSlugAutoField"
return type(name, (cls,), {"serializer_class": serializer})(*args, **kwargs)
......@@ -5,7 +5,11 @@ from rest_framework import serializers
from rest_framework.utils import model_meta
from metagenedb.apps.catalog.models import Function, Gene, GeneFunction, Taxonomy
from .asymetricslugrelatedfield import AsymetricSlugRelatedField
from .bulk_list import BulkListSerializer
from .function import FunctionSerializer
from .taxonomy import SimpleTaxonomySerializer
_LOGGER = logging.getLogger(__name__)
......@@ -48,13 +52,15 @@ class GeneListSerializer(BulkListSerializer):
class GeneSerializer(serializers.ModelSerializer):
functions = serializers.SlugRelatedField(
functions = AsymetricSlugRelatedField.from_serializer(
FunctionSerializer,
queryset=Function.objects.all(),
slug_field='function_id',
many=True,
required=False,
)
taxonomy = serializers.SlugRelatedField(
taxonomy = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
required=False,
......
from rest_framework import serializers
from metagenedb.apps.catalog.models import Taxonomy
from .asymetricslugrelatedfield import AsymetricSlugRelatedField
from .bulk_list import BulkListSerializer
class SimpleTaxonomySerializer(serializers.ModelSerializer):
class Meta:
model = Taxonomy
fields = ('tax_id', 'name')
class TaxonomyListSerializer(BulkListSerializer):
class Meta:
......@@ -12,48 +20,57 @@ class TaxonomyListSerializer(BulkListSerializer):
class TaxonomySerializer(serializers.ModelSerializer):
rank = serializers.CharField(required=False)
parent_tax_id = serializers.SlugRelatedField(
parent_tax_id = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
source='parent',
required=False,
)
superkingdom = serializers.SlugRelatedField(
superkingdom = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
required=False
)
kingdom = serializers.SlugRelatedField(
kingdom = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
required=False
)
phylum = serializers.SlugRelatedField(
phylum = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
required=False
)
class_rank = serializers.SlugRelatedField(
class_rank = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
required=False
)
order = serializers.SlugRelatedField(
order = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
required=False
)
family = serializers.SlugRelatedField(
family = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
required=False
)
genus = serializers.SlugRelatedField(
genus = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
required=False
)
species = serializers.SlugRelatedField(
species = AsymetricSlugRelatedField.from_serializer(
SimpleTaxonomySerializer,
queryset=Taxonomy.objects.all(),
slug_field='tax_id',
required=False
......
......@@ -20,7 +20,7 @@
<v-list-tile v-if="item.content" :key="item.title">
<v-list-tile-content :key="item.title">
<v-list-tile-title v-html="item.title"></v-list-tile-title>
<v-list-tile-sub-title v-html="item.content"></v-list-tile-sub-title>
<v-list-tile-sub-title v-html="item.content.name"></v-list-tile-sub-title>
</v-list-tile-content>
</v-list-tile>
</template>
......
......@@ -90,8 +90,8 @@ export default {
content: response.data.length,
},
];
this.kegg_id = response.data.functions[0];
this.taxonomy_id = response.data.taxonomy;
this.kegg_id = response.data.functions[0].function_id;
this.taxonomy_id = response.data.taxonomy.tax_id;
})
.catch((error) => {
console.log(error);
......
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