From 45da820a0bca69d3a0320c2eed155ce9fa51ad8e Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Sun, 14 Jan 2018 16:57:52 -0700 Subject: [PATCH] ENH: Collection environmental measurements (#44) --- ccdb/api/urls.py | 2 ++ ccdb/collections_ccdb/admin.py | 9 +++-- .../0007_collection_measurements.py | 34 +++++++++++++++++++ ccdb/collections_ccdb/models.py | 12 +++++++ ccdb/collections_ccdb/serializers.py | 22 ++++++++++-- ccdb/collections_ccdb/viewsets.py | 10 ++++-- 6 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 ccdb/collections_ccdb/migrations/0007_collection_measurements.py diff --git a/ccdb/api/urls.py b/ccdb/api/urls.py index 859e432..bdfd6ad 100644 --- a/ccdb/api/urls.py +++ b/ccdb/api/urls.py @@ -22,6 +22,8 @@ router.register(r'collection-types', collections_viewsets.CollectionTypeViewSet) router.register(r'collection-flaws', collections_viewsets.FlawViewSet) +router.register(r'collection-measurements', + collections_viewsets.CollectionMeasurementViewSet) router.register(r'adfg-permits', collections_viewsets.ADFGPermitViewSet) router.register(r'datasheet-attachments', collections_viewsets.DatasheetAttachmentViewSet) diff --git a/ccdb/collections_ccdb/admin.py b/ccdb/collections_ccdb/admin.py index 3630885..ab3469b 100644 --- a/ccdb/collections_ccdb/admin.py +++ b/ccdb/collections_ccdb/admin.py @@ -1,7 +1,8 @@ from django.contrib import admin from .models import (CollectionType, CollectionMethod, Flaw, ADFGPermit, - DatasheetAttachment, CollectionTrap, Collection) + DatasheetAttachment, CollectionTrap, Collection, + CollectionMeasurement) from ..species.models import CollectionSpecies @@ -57,8 +58,12 @@ class CollectionSpeciesInlineAdmin(admin.TabularInline): model = CollectionSpecies +class CollectionMeasurementInlineAdmin(admin.TabularInline): + model = CollectionMeasurement + + class CollectionAdmin(admin.ModelAdmin): - inlines = (CollectionSpeciesInlineAdmin, ) + inlines = (CollectionSpeciesInlineAdmin, CollectionMeasurementInlineAdmin) list_display = ('project', 'study_location', 'collection_end_date', 'collection_type', 'collection_method') list_display_links = ('project', 'study_location', 'collection_end_date', diff --git a/ccdb/collections_ccdb/migrations/0007_collection_measurements.py b/ccdb/collections_ccdb/migrations/0007_collection_measurements.py new file mode 100644 index 0000000..383907f --- /dev/null +++ b/ccdb/collections_ccdb/migrations/0007_collection_measurements.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.8 on 2018-01-04 13:57 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('collections_ccdb', '0006_collection_notes'), + ] + + operations = [ + migrations.CreateModel( + name='CollectionMeasurement', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('date_measured', models.DateField()), + ('time_measured', models.TimeField()), + ('water_temp_c', models.FloatField(null=True)), + ('air_temp_c', models.FloatField(null=True)), + ('collection', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='env_measurements', to='collections_ccdb.Collection')), + ], + options={ + 'ordering': ['date_measured', 'time_measured'], + }, + ), + migrations.AlterUniqueTogether( + name='collectionmeasurement', + unique_together=set([('collection', 'date_measured', 'time_measured')]), + ), + ] diff --git a/ccdb/collections_ccdb/models.py b/ccdb/collections_ccdb/models.py index 41be456..bfd39e7 100644 --- a/ccdb/collections_ccdb/models.py +++ b/ccdb/collections_ccdb/models.py @@ -123,3 +123,15 @@ class CollectionTrap(models.Model): class Meta: unique_together = ('collection', 'date_opened', 'time_opened', 'date_closed', 'time_closed') + + +class CollectionMeasurement(models.Model): + collection = models.ForeignKey(Collection, related_name='env_measurements') + date_measured = models.DateField() + time_measured = models.TimeField() + water_temp_c = models.FloatField(null=True) + air_temp_c = models.FloatField(null=True) + + class Meta: + unique_together = ('collection', 'date_measured', 'time_measured') + ordering = ['date_measured', 'time_measured'] diff --git a/ccdb/collections_ccdb/serializers.py b/ccdb/collections_ccdb/serializers.py index 44b9a88..5bfda20 100644 --- a/ccdb/collections_ccdb/serializers.py +++ b/ccdb/collections_ccdb/serializers.py @@ -1,7 +1,7 @@ from rest_framework_json_api import serializers from .models import (ADFGPermit, Collection, CollectionMethod, CollectionType, - Flaw, DatasheetAttachment) + Flaw, DatasheetAttachment, CollectionMeasurement) class CollectionSerializer(serializers.ModelSerializer): @@ -20,6 +20,9 @@ class CollectionSerializer(serializers.ModelSerializer): 'ccdb.species.serializers.CollectionSpeciesSerializer', 'datasheets': 'ccdb.collections_ccdb.serializers.DatasheetAttachmentSerializer', + 'env_measurements': + 'ccdb.collections_ccdb.serializers.' + 'CollectionMeasurementSerializer', } class Meta: @@ -30,8 +33,10 @@ class CollectionSerializer(serializers.ModelSerializer): 'collection_end_date', 'collection_end_time', 'storage_location', 'specimen_state', 'process_type', 'reagent', 'adfg_permit', 'collection_flaw', 'display_name', - 'collection_species', 'datasheets', 'notes') - read_only_fields = ('collection_species', 'datasheets') + 'collection_species', 'datasheets', 'notes', + 'env_measurements') + read_only_fields = ('collection_species', 'datasheets', + 'env_measurements') class ADFGPermitSerializer(serializers.ModelSerializer): @@ -67,3 +72,14 @@ class DatasheetAttachmentSerializer(serializers.ModelSerializer): class Meta: model = DatasheetAttachment fields = ('id', 'collection', 'datasheet') + + +class CollectionMeasurementSerializer(serializers.ModelSerializer): + included_serializers = { + 'collection': 'ccdb.collections_ccdb.serializers.CollectionSerializer', + } + + class Meta: + model = CollectionMeasurement + fields = ('id', 'collection', 'date_measured', 'time_measured', + 'water_temp_c', 'air_temp_c') diff --git a/ccdb/collections_ccdb/viewsets.py b/ccdb/collections_ccdb/viewsets.py index 1508fe0..d20d2a0 100644 --- a/ccdb/collections_ccdb/viewsets.py +++ b/ccdb/collections_ccdb/viewsets.py @@ -3,10 +3,11 @@ from django_filters import rest_framework as filters from .filters import CollectionFilter from .models import (ADFGPermit, Collection, CollectionMethod, CollectionType, - Flaw, DatasheetAttachment) + Flaw, DatasheetAttachment, CollectionMeasurement) from .serializers import (CollectionSerializer, CollectionMethodSerializer, CollectionTypeSerializer, FlawSerializer, - ADFGPermitSerializer, DatasheetAttachmentSerializer) + ADFGPermitSerializer, DatasheetAttachmentSerializer, + CollectionMeasurementSerializer) class CollectionViewSet(viewsets.ModelViewSet): @@ -39,3 +40,8 @@ class ADFGPermitViewSet(viewsets.ModelViewSet): class DatasheetAttachmentViewSet(viewsets.ModelViewSet): queryset = DatasheetAttachment.objects.all() serializer_class = DatasheetAttachmentSerializer + + +class CollectionMeasurementViewSet(viewsets.ModelViewSet): + queryset = CollectionMeasurement.objects.all() + serializer_class = CollectionMeasurementSerializer