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
admin.site.register(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 = [
migrations.CreateModel(
name='Gene',
fields=[
('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)
try:
data = paginator.page(page)
except PageNotAnInteger:
data = paginator.page(1)
except EmptyPage:
data = paginator.page(paginator.num_pages)
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': serializer.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(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, 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.
"""
try:
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(serializer.data)
elif request.method == 'PUT':
serializer = GeneSerializer(gene, data=request.data,context={'request': request})
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
gene.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
......@@ -36,12 +36,15 @@ if DEBUG:
# Application definition
INSTALLED_APPS = [
'metagenedb.apps.catalog',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'rest_framework',
]
MIDDLEWARE = [
......
......@@ -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('admin/', admin.site.urls),
path('catalog/', include('metagenedb.apps.catalog.urls'))
]
django>=2.1
psycopg2
\ No newline at end of file
psycopg2
djangorestframework
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