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

add DRF and first app

parent d0543bd1
from django.contrib import admin
from .models import Gene
from django.apps import AppConfig
class CatalogConfig(AppConfig):
name = 'catalog'
# Generated by Django 2.2.1 on 2019-06-04 15:05
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
operations = [
('gene_id', models.CharField(max_length=100, primary_key=True, serialize=False)),
from django.db import models
class Gene(models.Model):
gene_id = models.CharField(max_length=100, primary_key=True)
def __str__(self):
return self.gene_id
from rest_framework import serializers
from .models import Gene
class GeneSerializer(serializers.ModelSerializer):
class Meta:
model = Gene
fields = ('gene_id',)
from django.test import TestCase
# Create your tests here.
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', views.index, name='index'),
re_path(r'^api/genes/$', views.gene_list),
re_path(r'^api/genes/(?P<gene_id>.*)$', views.gene_detail),
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponse
from .models import Gene
from .serializers import GeneSerializer
def index(request):
nb_genes = len(Gene.objects.all())
return HttpResponse(f"There is currently {nb_genes} genes in the catalog.")
@api_view(['GET', 'POST'])
def gene_list(request):
List genes, or create new gene
if request.method == 'GET':
data = []
nextPage = 1
previousPage = 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():
nextPage = data.next_page_number()
if data.has_previous():
previousPage = data.previous_page_number()
return Response({'data':, 'count': paginator.count, 'numpages': paginator.num_pages,
'nextlink': '/api/genes/?page=' + str(nextPage),
'prevlink': '/api/genes/?page=' + str(previousPage)})
elif request.method == 'POST':
serializer = GeneSerializer(
if serializer.is_valid():
return Response(, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['GET', 'PUT', 'DELETE'])
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)
if request.method == 'GET':
serializer = GeneSerializer(gene,context={'request': request})
return Response(
elif request.method == 'PUT':
serializer = GeneSerializer(gene,,context={'request': request})
if serializer.is_valid():
return Response(
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
return Response(status=status.HTTP_204_NO_CONTENT)
......@@ -36,12 +36,15 @@ if DEBUG:
# Application definition
......@@ -14,8 +14,11 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
from django.contrib import admin
from django.urls import path
from django.urls import include, path
from rest_framework import routers
urlpatterns = [
path('catalog/', include('metagenedb.apps.catalog.urls'))
\ No newline at end of file
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