def get(self, request, pk): instance = self.get_object(request, pk) data = serialize(instance, **self.template) for i in range(len(data['results'])): data['results'][i]['variant'] = VariantResource.get( request, data['results'][i]['variant_id']) data['results'][i].pop('variant_id') result_id = data['results'][i]['id'] data['results'][i]['num_assessments'] = Assessment.objects.filter( sample_result__id=result_id, sample_result__resultset__id=pk).count() try: assessment = Assessment.objects.get( sample_result__id=result_id, sample_result__resultset__id=pk, user=request.user.id) data['results'][i]['assessment'] = \ serialize(assessment, **api.templates.ResultAssessment) except Assessment.DoesNotExist: data['results'][i]['assessment'] = {} return data
def _cache_data(self, request, pk, key): related = ['sample', 'variant', 'genotype'] try: result = self.model.objects.select_related(*related).get(pk=pk) except self.model.DoesNotExist: raise Http404 data = serialize(result, **self.template) data['_links'] = { 'self': { 'rel': 'self', 'href': reverse('api:samples:variant', kwargs={'pk': data['id']}) }, 'sample': { 'rel': 'related', 'href': reverse('api:samples:sample', kwargs={'pk': data['sample']['id']}) }, 'variant': { 'rel': 'related', 'href': reverse('api:variants:variant', kwargs={'pk': data['variant_id']}), } } # Integrate the Variant resource data data['variant'] = VariantResource.get(request, data['variant_id']) data.pop('variant_id') cache.set(key, data, timeout=api.CACHE_TIMEOUT) return data
def _cache_data(self, request, pk, key): uri = request.build_absolute_uri related = ['sample', 'variant', 'genotype', 'score'] try: result = self.model.objects.select_related(*related).get(pk=pk) except self.model.DoesNotExist: raise Http404 data = serialize(result, **self.template) data['_links'] = { 'self': { 'rel': 'self', 'href': uri(reverse('api:samples:variant', kwargs={'pk': data['id']})) }, 'sample': { 'rel': 'related', 'href': uri( reverse('api:samples:sample', kwargs={'pk': data['sample']['id']})) }, 'variant': { 'rel': 'related', 'href': uri( reverse('api:variants:variant', kwargs={'pk': data['variant_id']})), } } # Integrate the Variant resource data data['variant'] = VariantResource.get(request, data['variant_id']) data.pop('variant_id') try: score = ResultScore.objects.get(result=result) data['score'] = { 'score': score.score, 'rank': score.rank, } except ResultScore.DoesNotExist: pass cache.set(key, data, timeout=api.CACHE_TIMEOUT) return data