Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
Alter SampleSerializer to not return costly calculated fields, and al…
Browse files Browse the repository at this point in the history
…so offer new slim version with only essential fields
  • Loading branch information
aaronstephenson committed Sep 30, 2019
1 parent 61b15d2 commit 976887b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
4 changes: 2 additions & 2 deletions code.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "lide",
"organization": "U.S. Geological Survey",
"description": "Web services for LILI (LIDE (Laboratory for Infectious Disease and the Environment) Information Management System)",
"version": "v0.114.2",
"version": "v0.115.0",
"status": "Release Candidate",

"permissions": {
Expand Down Expand Up @@ -47,7 +47,7 @@
},

"date": {
"metadataLastUpdated": "2019-09-10"
"metadataLastUpdated": "2019-09-30"
}
}
]
57 changes: 34 additions & 23 deletions lideservices/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,25 +459,25 @@ def validate(self, data):
raise serializers.ValidationError("meter_reading_final must be larger than meter_reading_initial")
return data

# peg_neg_targets_extracted
def get_peg_neg_targets_extracted(self, obj):
targets_extracted = []
peg_neg = obj.peg_neg
if peg_neg is not None:
sample_extractions = peg_neg.sampleextractions.values()

if sample_extractions is not None:
for sample_extraction in sample_extractions:
replicates = sample_extraction.get('pcrreplicates')
if replicates is not None:
for replicate in replicates:
target_id = replicate.get('target_id')

# get the unique target IDs for this peg_neg
if target_id not in targets_extracted:
targets_extracted.append(target_id)

return targets_extracted
# # peg_neg_targets_extracted
# def get_peg_neg_targets_extracted(self, obj):
# targets_extracted = []
# peg_neg = obj.peg_neg
# if peg_neg is not None:
# sample_extractions = peg_neg.sampleextractions.values()
#
# if sample_extractions is not None:
# for sample_extraction in sample_extractions:
# replicates = sample_extraction.get('pcrreplicates')
# if replicates is not None:
# for replicate in replicates:
# target_id = replicate.get('target_id')
#
# # get the unique target IDs for this peg_neg
# if target_id not in targets_extracted:
# targets_extracted.append(target_id)
#
# return targets_extracted

created_by = serializers.StringRelatedField()
modified_by = serializers.StringRelatedField()
Expand All @@ -494,9 +494,9 @@ def get_peg_neg_targets_extracted(self, obj):
dissolution_volume = NullableRStrip10DecimalField()
post_dilution_volume = NullableRStrip10DecimalField()
aliquots = AliquotSerializer(many=True, read_only=True)
peg_neg_targets_extracted = serializers.SerializerMethodField()
# peg_neg_targets_extracted = serializers.SerializerMethodField()
finalconcentratedsamplevolume = FinalConcentratedSampleVolumeSerializer(read_only=True)
finalsamplemeanconcentrations = FinalSampleMeanConcentrationSerializer(many=True, read_only=True)
# finalsamplemeanconcentrations = FinalSampleMeanConcentrationSerializer(many=True, read_only=True)

class Meta:
model = Sample
Expand All @@ -508,8 +508,19 @@ class Meta:
'total_volume_sampled_unit_initial', 'total_volume_or_mass_sampled', 'sample_volume_initial',
'filter_born_on_date', 'filter_flag', 'secondary_concentration_flag', 'elution_notes', 'record_type',
'record_type_string', 'technician_initials', 'dissolution_volume', 'post_dilution_volume', 'peg_neg',
'samplegroups', 'analysisbatches', 'peg_neg_targets_extracted', 'finalconcentratedsamplevolume',
'finalsamplemeanconcentrations', 'aliquots',
'samplegroups', 'analysisbatches', 'finalconcentratedsamplevolume', 'aliquots',
# 'finalsamplemeanconcentrations', 'peg_neg_targets_extracted',
'created_date', 'created_by', 'modified_date', 'modified_by',)


class SampleSlimSerializer(serializers.ModelSerializer):
created_by = serializers.StringRelatedField()
modified_by = serializers.StringRelatedField()
study_string = serializers.StringRelatedField(source='study')

class Meta:
model = Sample
fields = ('id', 'study', 'study_string', 'collaborator_sample_id', 'collection_start_date',
'created_date', 'created_by', 'modified_date', 'modified_by',)


Expand Down
6 changes: 6 additions & 0 deletions lideservices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def perform_update(self, serializer):
class SampleViewSet(HistoryViewSet):
serializer_class = SampleSerializer

def get_serializer_class(self):
if self.request and 'slim' in self.request.query_params:
return SampleSlimSerializer
else:
return SampleSerializer

@action(detail=False)
def finalsamplemeanconcentrations(self, request):
queryset = Sample.objects.prefetch_related('finalsamplemeanconcentrations').distinct()
Expand Down

0 comments on commit 976887b

Please sign in to comment.