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

Use ViewSet for Gene API

parent 3b867015
from django.urls import re_path
from . import views
from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from metagenedb.api.catalog.views import GeneViewSet, gene_length
api_router = DefaultRouter()
api_router.register(r'genes', GeneViewSet, 'genes')
urlpatterns = [
re_path(r'^genes/$', views.gene_list, name='genes'),
re_path(r'^genes/(?P<gene_id>.*)$', views.gene_detail),
re_path(r'^gene_length$', views.gene_length, name='gene_length'),
re_path(r'^gene_length$', gene_length, name='gene_length'),
url(r'v1/', include(api_router.urls))
from .gene import gene_detail, gene_list # noqa
from .statistics import gene_length # noqa
\ No newline at end of file
from .gene import GeneViewSet # noqa
from .statistics import gene_length # noqa
__all__ = ['GeneViewSet']
import logging
from rest_framework import status
from rest_framework.decorators import (
from rest_framework.response import Response
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from rest_framework.viewsets import GenericViewSet
from rest_framework import mixins
from metagenedb.apps.catalog.models import Gene
from metagenedb.apps.catalog.serializers import GeneSerializer
_LOGGER = logging.getLogger(__name__)
def gene_list(request):
class GeneViewSet(mixins.ListModelMixin,
List genes
The following endpoints are fully provided by mixins:
* List gene
next_page = 1
previous_page = 1
genes = Gene.objects.all()
page = request.GET.get('page', 1)
paginator = Paginator(genes, 10)
data =
except PageNotAnInteger:
data =
except EmptyPage:
data =
serializer = GeneSerializer(data, context={'request': request}, many=True)
if data.has_next():
next_page = data.next_page_number()
if data.has_previous():
previous_page = data.previous_page_number()
return Response(
{'data':, 'count': paginator.count, 'numpages': paginator.num_pages,
'nextlink': '/api/genes/?page=' + str(next_page),
'prevlink': '/api/genes/?page=' + str(previous_page)}
def gene_detail(request, gene_id):
Retrieve, update or delete a gene instance.
gene = Gene.objects.get(gene_id=gene_id)
except Gene.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = GeneSerializer(gene, context={'request': request})
return Response(
queryset = Gene.objects.all()
serializer_class = GeneSerializer
from rest_framework import serializers
from .models import Gene, Function
class FunctionSerializer(serializers.ModelSerializer):
class Meta:
model = Function
fields = ('function_id', 'source', 'name')
class GeneSerializer(serializers.ModelSerializer):
functions = FunctionSerializer(many=True, read_only=True)
class Meta:
model = Gene
fields = ('gene_id', 'gene_length', 'functions')
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