Exemplo n.º 1
0
 def get_variable_aggregates(self, base_location, report_level=1):
     indicator = self.indicator
     ikwargs = {'question_set__pk': indicator.question_set.pk,
                'survey__pk': indicator.survey.pk}
     parent_loc = 'ea__locations'
     hierachy_count = Location.country().type.get_descendant_count()
     for i in range(hierachy_count - report_level):    # fetches direct descendants from ea__locs
         parent_loc = '%s__parent' % parent_loc
     # exploiting mptt artributes to speed up this query
     lowest_level = base_location.type.get_descendants(include_self=False).last().level
     left = base_location.lft
     right = base_location.rght
     if report_level <= base_location.level:     # if you want to include base_location (anything above becomes self
         left += 1
         right -= 1
     ikwargs.update({'ea__locations__lft__gte': left, 'ea__locations__lft__lte': right,
                     'ea__locations__level': lowest_level})
     interviews = Interview.objects.filter(**ikwargs)
     for criterion in self.criteria.all():
         kwargs = dict()
         kwargs['answer__question__identifier__iexact'] = criterion.test_question.identifier
         # be careful here regarding multiple validation tests with same name (e.g a__gt=2, a__gt=10)
         kwargs.update(Answer.get_validation_queries(criterion.validation_test, 'as_value',
                                                     namespace='answer__', *criterion.prepped_args))
         interviews = interviews.filter(**kwargs)
     parent_loc = '%s__name' % parent_loc
     return dict(interviews.values_list(parent_loc).annotate(total=Count('id', distinct=True)))
Exemplo n.º 2
0
 def get_valid_qs(self, locations):
     """Return the queryset valid according to this indicator variable
     :param locations:
     :return:
     """
     indicator = self.indicator
     ikwargs = {'ea__locations__in': locations,
                'question_set__pk': indicator.question_set.pk,
                'survey__pk': indicator.survey.pk}
     interviews = Interview.objects.filter(**ikwargs)
     for criterion in self.criteria.all():
         kwargs = dict()
         kwargs['answer__question__identifier__iexact'] = criterion.test_question.identifier
         # be careful here regarding multiple validation tests with same name (e.g a__gt=2, a__gt=10)
         kwargs.update(Answer.get_validation_queries(criterion.validation_test, 'as_value',
                                                     namespace='answer__', *criterion.prepped_args))
         interviews = interviews.filter(**kwargs)
     return interviews.distinct('id')