def filter_membership(self, qs, name, value): try: complex_ops = decode_complex_ops(value, filter_operators(), True) except ValidationError as exc: raise ValidationError({'membership': exc.detail}) # Collect the individual filtered membership querysets querystrings = [op.querystring for op in complex_ops] ms_queryset = vendors.PoolMembership.objects.all() ms_querysets = [] errors = [] poolIds = [] queryParameters = {} self.logger.error(" one ") for qstring in querystrings: query = qstring.split('=') try: if 'pool__id__in' == query[0]: poolIds = query[1].split(",") query[1] = poolIds queryParameters[query[0]] = query[1] ms_querysets.append(ms_queryset.filter(**{query[0]: query[1]})) except ValidationError as exc: errors[qstring] = exc.detail if (len(poolIds) <= 1): try: ms_queryset = combine_complex_queryset(ms_querysets, complex_ops) ms_ids = list(ms_queryset.values_list('id', flat=True)) self.logger.error(" ms_ids if {} ".format(ms_ids)) self.logger.error(" query if {} ".format(ms_queryset.query)) except ValidationError as exc: errors[qstring] = exc.detail else: try: self.logger.error(" poolIds else {} ".format(poolIds)) ms_queryset = combine_complex_queryset(ms_querysets, complex_ops) ms_ids = self.getMebershipIds(ms_queryset) self.logger.error(" ms_ids else {} ".format(ms_ids)) if len(ms_ids) == 0: qs = qs.filter(pools__id=0) return qs except ValidationError as exc: errors[qstring] = exc.detail if errors: raise ValidationError(errors) if len(querystrings) > 0: qs = qs.filter(pools__id__in=ms_ids) return qs
def filter_membership(self, qs, name, value): # Decode complex membership query try: complex_ops = decode_complex_ops(value, filter_operators(), True) except ValidationError as exc: raise ValidationError({'membership': exc.detail}) # Collect the individual filtered membership querysets querystrings = [op.querystring for op in complex_ops] ms_queryset = vendors.PoolMembership.objects.all() ms_querysets = [] errors = [] for qstring in querystrings: query = qstring.split('=') try: ms_querysets.append(ms_queryset.filter(**{query[0]: query[1]})) except ValidationError as exc: errors[qstring] = exc.detail if errors: raise ValidationError(errors) # Retrieve a list of membership ids matching query ms_queryset = combine_complex_queryset(ms_querysets, complex_ops) ms_ids = list(ms_queryset.values_list('id', flat=True)) # Filter vendors by membership if len(querystrings) > 0: qs = qs.filter(pools__id__in=ms_ids) return qs
def test_single(self): querysets = [models.User.objects.filter(first_name='Bob')] complex_ops = [ComplexOp(None, False, None)] self.assertQuerysetEqual( combine_complex_queryset(querysets, complex_ops), ['u1', 'u3'], attrgetter('username'), False, )
def test_negation(self): querysets = [ models.User.objects.filter(first_name='Bob'), models.User.objects.filter(last_name='Smith'), ] complex_ops = [ ComplexOp(None, False, QuerySet.__and__), ComplexOp(None, True, None), ] self.assertQuerysetEqual( combine_complex_queryset(querysets, complex_ops), ['u1'], attrgetter('username'), False, )