def get_queryset(self): """When the parent instance of the host query set has a `kind=smart` and a `host_filter` set. Use the `host_filter` to generate the queryset for the hosts. """ qs = super(HostManager, self).get_queryset().defer( 'last_job__extra_vars', 'last_job_host_summary__job__extra_vars', 'last_job__artifacts', 'last_job_host_summary__job__artifacts', ) if (hasattr(self, 'instance') and hasattr(self.instance, 'host_filter') and hasattr(self.instance, 'kind')): if self.instance.kind == 'smart' and self.instance.host_filter is not None: q = SmartFilter.query_from_string(self.instance.host_filter) if self.instance.organization_id: q = q.filter(inventory__organization=self.instance.organization_id) # If we are using host_filters, disable the core_filters, this allows # us to access all of the available Host entries, not just the ones associated # with a specific FK/relation. # # If we don't disable this, a filter of {'inventory': self.instance} gets automatically # injected by the related object mapper. self.core_filters = {} qs = qs & q return qs.order_by('name', 'pk').distinct('name') return qs
def test_host_distinctness(self, setup_inventory_groups, organization): """ two criteria would both yield the same host, check that we only get 1 copy here """ assert (list( SmartFilter.query_from_string( 'name=single_host or name__startswith=single_')) == [ Host.objects.get(name='single_host') ])
def get_prep_value(self, value): # Change any false value to none. # https://docs.python.org/2/library/stdtypes.html#truth-value-testing if not value: return None try: SmartFilter().query_from_string(value) except RuntimeError, e: raise models.base.ValidationError(e)
def test_does_not_invoke_db(self, filter_string, q_expected): q = SmartFilter.query_from_string(filter_string) assert str(q.query) == str(Host.objects.filter(q_expected).query)
def test_query_generated(self, mock_get_host_model, filter_string, q_expected): q = SmartFilter.query_from_string(filter_string) assert str(q) == str(q_expected)
def test_boolean_parenthesis(self, mock_get_host_model, filter_string, q_expected): q = SmartFilter.query_from_string(filter_string) assert str(q) == str(q_expected)
def test_search_related_fields(self, mock_get_host_model, filter_string, q_expected): q = SmartFilter.query_from_string(filter_string) assert str(q) == str(q_expected)
def test_forbidden_filter_string(self, mock_get_host_model, filter_string): with pytest.raises(Exception) as e: SmartFilter.query_from_string(filter_string) "Filtering on password is not allowed." in str(e)
def test_invalid_filter_strings(self, mock_get_host_model, filter_string): with pytest.raises(RuntimeError) as e: SmartFilter.query_from_string(filter_string) assert str(e.value) == u"Invalid query " + filter_string
def test_unicode(self, mock_get_host_model, filter_string, q_expected): q = SmartFilter.query_from_string(filter_string) assert unicode(q) == unicode(q_expected)
def test_contains_query_generated_null(self, mock_get_host_model, filter_string, q_expected): q = SmartFilter.query_from_string(filter_string) assert unicode(q) == unicode(q_expected)
def test_contains_query_generated(self, mock_get_host_model, filter_string, q_expected): q = SmartFilter.query_from_string(filter_string) assert six.text_type(q) == six.text_type(q_expected)