From 8cd2af8fea3eede626b6f61f329cbc0afa0f02e8 Mon Sep 17 00:00:00 2001 From: Veni Kunche Date: Thu, 21 Sep 2017 08:59:17 -0400 Subject: [PATCH] Add Analysis Batch Summary endpoint --- lideservices/serializers.py | 68 +++++++++++++++++++++++++++++++++++++ lideservices/urls.py | 1 + lideservices/views.py | 4 +++ 3 files changed, 73 insertions(+) diff --git a/lideservices/serializers.py b/lideservices/serializers.py index 1ea45ef..86e202c 100644 --- a/lideservices/serializers.py +++ b/lideservices/serializers.py @@ -296,6 +296,74 @@ class Meta: model = AnalysisBatch fields = ('id', 'analysis_batch_description', 'analysis_batch_notes','samples','studies','extractions','created_date','created_by','modified_date','modified_by',) +class AnalysisBatchSummarySerializer(serializers.ModelSerializer): + + #studies + def get_studies(self, obj): + studies = [] + vals = obj.samples.values() + for val in vals: + study_id = val.get('study_id') + studies.append(study_id) + return studies + studies = serializers.SerializerMethodField() + + #summary: extraction count, inhibition count, reverse transcription count, target count + def get_summary(self, obj): + summary = {} + inhibition_count = 0 + reverse_transcription_count = 0 + targets = [] + + extractions = obj.extractions.values() + + #extraction count + extraction_count = len(extractions) + + for val in extractions: + extraction_id = val.get('id') + + #inhibition count + inhibition_count += len(Inhibition.objects.filter(extraction=extraction_id)) + + #reverse transcription count + reverse_transcription_count += len(ReverseTranscription.objects.filter(extraction=extraction_id)) + + #target count + replicates = PCRReplicate.objects.filter(extraction=extraction_id) + for replicate in replicates: + target = replicate.target + if target not in targets: + targets.append(replicate.target) + + summary['extraction_count'] = extraction_count + summary['inhibition_count'] = inhibition_count + summary['reverse_transcription_count'] = reverse_transcription_count + summary['target_count'] = len(targets) + + return summary + summary = serializers.SerializerMethodField() + + #created by username + def get_created_by(self, obj): + username = None + if obj.created_by is not None: + username = obj.created_by.username + return username + created_by = serializers.StringRelatedField() + + #modified by username + def get_modified_by(self, obj): + username = None + if obj.modified_by is not None: + username = obj.modified_by.username + return username + modified_by = serializers.StringRelatedField() + + class Meta: + model = AnalysisBatch + fields = ('id', 'analysis_batch_description', 'analysis_batch_notes','studies','summary','created_date','created_by','modified_date','modified_by',) + ###### # diff --git a/lideservices/urls.py b/lideservices/urls.py index 9b03969..96ec894 100644 --- a/lideservices/urls.py +++ b/lideservices/urls.py @@ -19,6 +19,7 @@ router.register(r'samplegroups', views.SampleGroupViewSet, 'samplegroups') router.register(r'analysisbatches', views.AnalysisBatchViewSet, 'analysisbatches') router.register(r'analysisbatchdetail', views.AnalysisBatchDetailViewSet, 'analysisbatchdetail') +router.register(r'analysisbatchsummary', views.AnalysisBatchSummaryViewSet, 'analysisbatchsummary') router.register(r'analysisbatchtemplatess', views.AnalysisBatchTemplateViewSet, 'analysisbatchtemplates') router.register(r'extractions', views.ExtractionViewSet, 'extractions') router.register(r'extractionmethods', views.ExtractionMethodViewSet, 'extractionmethods') diff --git a/lideservices/views.py b/lideservices/views.py index 6bd50ab..0ac566d 100644 --- a/lideservices/views.py +++ b/lideservices/views.py @@ -160,6 +160,10 @@ def get_queryset(self): if batch is not None: queryset = queryset.filter(id__exact=batch) return queryset + +class AnalysisBatchSummaryViewSet(HistoryViewSet): + queryset = AnalysisBatch.objects.all() + serializer_class = AnalysisBatchSummarySerializer class AnalysisBatchTemplateViewSet(HistoryViewSet):