From 04fd45e5def26119e0be65dfa587661d5da099ca Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Sat, 9 Sep 2017 17:15:39 -0700 Subject: [PATCH] ENH: Collections DRF (#25) --- ccdb/api/urls.py | 17 ++++++++++- ccdb/collections_ccdb/serializers.py | 43 ++++++++++++++++++++++++++++ ccdb/collections_ccdb/viewsets.py | 25 ++++++++++++++++ ccdb/locations/serializers.py | 10 +++++++ ccdb/locations/viewsets.py | 9 ++++++ ccdb/projects/serializers.py | 9 ++++++ ccdb/projects/viewsets.py | 9 ++++++ config/settings/base.py | 2 ++ 8 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 ccdb/collections_ccdb/serializers.py create mode 100644 ccdb/collections_ccdb/viewsets.py create mode 100644 ccdb/locations/serializers.py create mode 100644 ccdb/locations/viewsets.py create mode 100644 ccdb/projects/serializers.py create mode 100644 ccdb/projects/viewsets.py diff --git a/ccdb/api/urls.py b/ccdb/api/urls.py index 38ff3d4..70e301d 100644 --- a/ccdb/api/urls.py +++ b/ccdb/api/urls.py @@ -4,12 +4,27 @@ from rest_framework import routers from . import views as api_v from ..utils import viewsets as utils_viewsets +from ..collections_ccdb import viewsets as collections_viewsets +from ..projects import viewsets as projects_viewsets +from ..locations import viewsets as locations_viewsets -router = routers.DefaultRouter() +router = routers.DefaultRouter(trailing_slash=False) router.register(r'admin-sections', utils_viewsets.AdminSectionViewSet) router.register(r'admin-entries', utils_viewsets.AdminEntryViewSet) +# Collections +router.register(r'collections', collections_viewsets.CollectionViewSet) +router.register(r'collection-methods', + collections_viewsets.CollectionMethodViewSet) +router.register(r'collection-types', + collections_viewsets.CollectionTypeViewSet) +router.register(r'collection-flaws', + collections_viewsets.FlawViewSet) +# Projects +router.register(r'projects', projects_viewsets.ProjectViewSet) +# Locations +router.register(r'study-locations', locations_viewsets.StudyLocationViewSet) urlpatterns = [ url(r'^auth/login/', api_v.Login.as_view()), diff --git a/ccdb/collections_ccdb/serializers.py b/ccdb/collections_ccdb/serializers.py new file mode 100644 index 0000000..54308b7 --- /dev/null +++ b/ccdb/collections_ccdb/serializers.py @@ -0,0 +1,43 @@ +from rest_framework import serializers + +from .models import Collection, CollectionMethod, CollectionType, Flaw + + +class CollectionSerializer(serializers.ModelSerializer): + included_serializers = { + 'project': 'ccdb.projects.serializers.ProjectSerializer', + 'study_location': 'ccdb.locations.serializers.StudyLocationSerializer', + 'collection_method': + 'ccdb.collections_ccdb.serializers.CollectionMethodSerializer', + 'collection_type': + 'ccdb.collections_ccdb.serializers.CollectionTypeSerializer', + 'flaw': 'ccdb.collections_ccdb.serializers.FlawSerializer', + } + + class Meta: + model = Collection + fields = ('id', 'project', 'study_location', 'collection_type', + 'collection_method', 'number_of_traps', + 'collection_start_date', 'collection_start_time', + 'collection_end_date', 'collection_end_time', + 'storage_location', 'specimen_state', 'process_type', + 'reagent', 'adfg_permit', 'flaw', 'display_name') + + +class CollectionMethodSerializer(serializers.ModelSerializer): + class Meta: + model = CollectionMethod + fields = ('id', 'name', 'code', 'collection_method_class', + 'sort_order') + + +class CollectionTypeSerializer(serializers.ModelSerializer): + class Meta: + model = CollectionType + fields = ('id', 'name', 'code', 'sort_order') + + +class FlawSerializer(serializers.ModelSerializer): + class Meta: + model = Flaw + fields = ('id', 'name', 'description', 'sort_order') diff --git a/ccdb/collections_ccdb/viewsets.py b/ccdb/collections_ccdb/viewsets.py new file mode 100644 index 0000000..6001dee --- /dev/null +++ b/ccdb/collections_ccdb/viewsets.py @@ -0,0 +1,25 @@ +from rest_framework import viewsets + +from .models import Collection, CollectionMethod, CollectionType, Flaw +from .serializers import (CollectionSerializer, CollectionMethodSerializer, + CollectionTypeSerializer, FlawSerializer) + + +class CollectionViewSet(viewsets.ModelViewSet): + queryset = Collection.objects.all() + serializer_class = CollectionSerializer + + +class CollectionMethodViewSet(viewsets.ModelViewSet): + queryset = CollectionMethod.objects.all() + serializer_class = CollectionMethodSerializer + + +class CollectionTypeViewSet(viewsets.ModelViewSet): + queryset = CollectionType.objects.all() + serializer_class = CollectionTypeSerializer + + +class FlawViewSet(viewsets.ModelViewSet): + queryset = Flaw.objects.all() + serializer_class = FlawSerializer diff --git a/ccdb/locations/serializers.py b/ccdb/locations/serializers.py new file mode 100644 index 0000000..008e199 --- /dev/null +++ b/ccdb/locations/serializers.py @@ -0,0 +1,10 @@ +from rest_framework import serializers + +from .models import StudyLocation + + +class StudyLocationSerializer(serializers.ModelSerializer): + class Meta: + model = StudyLocation + fields = ('name', 'code', 'study_location_type', 'treatment_type', + 'collecting_location', 'description', 'sort_order') diff --git a/ccdb/locations/viewsets.py b/ccdb/locations/viewsets.py new file mode 100644 index 0000000..06d99dd --- /dev/null +++ b/ccdb/locations/viewsets.py @@ -0,0 +1,9 @@ +from rest_framework import viewsets + +from .models import StudyLocation +from .serializers import StudyLocationSerializer + + +class StudyLocationViewSet(viewsets.ModelViewSet): + queryset = StudyLocation.objects.all() + serializer_class = StudyLocationSerializer diff --git a/ccdb/projects/serializers.py b/ccdb/projects/serializers.py new file mode 100644 index 0000000..dd5d13d --- /dev/null +++ b/ccdb/projects/serializers.py @@ -0,0 +1,9 @@ +from rest_framework import serializers + +from .models import Project + + +class ProjectSerializer(serializers.ModelSerializer): + class Meta: + model = Project + fields = ('name', 'code', 'iacuc_number', 'description', 'sort_order') diff --git a/ccdb/projects/viewsets.py b/ccdb/projects/viewsets.py new file mode 100644 index 0000000..9340cf5 --- /dev/null +++ b/ccdb/projects/viewsets.py @@ -0,0 +1,9 @@ +from rest_framework import viewsets + +from .models import Project +from .serializers import ProjectSerializer + + +class ProjectViewSet(viewsets.ModelViewSet): + queryset = Project.objects.all() + serializer_class = ProjectSerializer diff --git a/config/settings/base.py b/config/settings/base.py index 01e885b..36f94d5 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -159,6 +159,8 @@ REST_FRAMEWORK = { 'TEST_REQUEST_DEFAULT_FORMAT': 'json', } +JSON_API_FORMAT_KEYS = 'dasherize' + SITE_ID = 1 DJOSER = {