Add a checkbox 'exclude "only if needed" labels' to host lists in AFE, checked by default in the create job view.
-add exclude_only_if_needed_labels argument to get_hosts() and get_num_hosts() RPCs
-move some code from tko.models.TestViewManager up into model_logic, since AFE now needs it to create a custom join for doing many-valued exclusion



git-svn-id: http://test.kernel.org/svn/autotest/trunk@2405 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/frontend/afe/rpc_utils.py b/frontend/afe/rpc_utils.py
index 0e25d05..014e3f3 100644
--- a/frontend/afe/rpc_utils.py
+++ b/frontend/afe/rpc_utils.py
@@ -97,6 +97,22 @@
     return extra_args
 
 
+def get_host_query(multiple_labels, exclude_only_if_needed_labels, filter_data):
+    query = models.Host.valid_objects.all()
+    if exclude_only_if_needed_labels:
+        only_if_needed_labels = models.Label.valid_objects.filter(
+            only_if_needed=True)
+        only_if_needed_ids = ','.join(
+            str(label['id']) for label in only_if_needed_labels.values('id'))
+        query = models.Host.objects.add_join(
+            query, 'hosts_labels', join_key='host_id',
+            join_condition='hosts_labels_exclude.label_id IN (%s)'
+                           % only_if_needed_ids,
+            suffix='_exclude', exclude=True)
+    filter_data['extra_args'] = (extra_host_filters(multiple_labels))
+    return models.Host.query_objects(filter_data, initial_query=query)
+
+
 class InconsistencyException(Exception):
     'Raised when a list of objects does not have a consistent value'