def getfiltereddatatypes(self, name, parent_div, tree_session_key, filters): # TODO: fix this use-case """ Given the name from the input tree, the dataType required and a number of filters, return the available dataType that satisfy the conditions imposed. """ previous_tree = self.context.get_session_tree_for_key(tree_session_key) if previous_tree is None: common.set_error_message("Adapter Interface not in session for filtering!") raise cherrypy.HTTPRedirect("/tvb?error=True") current_node = self._get_node(previous_tree, name) if current_node is None: raise Exception("Could not find node :" + name) datatype = current_node[ABCAdapter.KEY_DATATYPE] filters = json.loads(filters) availablefilter = json.loads(FilterChain.get_filters_for_type(datatype)) for i, filter_ in enumerate(filters[FILTER_FIELDS]): # Check for filter input of type 'date' as these need to be converted if filter_ in availablefilter and availablefilter[filter_][FILTER_TYPE] == 'date': try: temp_date = string2date(filters[FILTER_VALUES][i], False) filters[FILTER_VALUES][i] = temp_date except ValueError: raise # In order for the filter object not to "stack up" on multiple calls to # this method, create a deepCopy to work with if constants.ELEM_CONDITIONS in current_node: new_filter = copy.deepcopy(current_node[constants.ELEM_CONDITIONS]) else: new_filter = FilterChain() new_filter.fields.extend(filters[FILTER_FIELDS]) new_filter.operations.extend(filters[FILTER_OPERATIONS]) new_filter.values.extend(filters[FILTER_VALUES]) # Get dataTypes that match the filters from DB then populate with values values, total_count = [], 0 # Create a dictionary that matches what the template expects parameters = {ABCAdapter.KEY_NAME: name, ABCAdapter.KEY_FILTERABLE: availablefilter, ABCAdapter.KEY_TYPE: constants.TYPE_SELECT, ABCAdapter.KEY_OPTIONS: values, ABCAdapter.KEY_DATATYPE: datatype} if total_count > MAXIMUM_DATA_TYPES_DISPLAYED: parameters[KEY_WARNING] = WARNING_OVERFLOW if constants.ATT_REQUIRED in current_node: parameters[constants.ATT_REQUIRED] = current_node[constants.ATT_REQUIRED] if len(values) > 0 and string2bool(str(parameters[constants.ATT_REQUIRED])): parameters[ABCAdapter.KEY_DEFAULT] = str(values[-1][ABCAdapter.KEY_VALUE]) previous_selected = self.context.get_current_default(name) if previous_selected in [str(vv['value']) for vv in values]: parameters[ABCAdapter.KEY_DEFAULT] = previous_selected template_specification = {"inputRow": parameters, "disabled": False, "parentDivId": parent_div, common.KEY_SESSION_TREE: tree_session_key} return self.fill_default_attributes(template_specification)
def fill_input_tree_with_options(self, attributes_list, project_id, category_key): """ For a datatype node in the input tree, load all instances from the db that fit the filters. """ result = [] for param in attributes_list: if param.get(KEY_UI_HIDE): continue transformed_param = copy(param) if KEY_TYPE in param and param[KEY_TYPE] not in STATIC_ACCEPTED_TYPES: if KEY_CONDITION in param: filter_condition = param[KEY_CONDITION] else: filter_condition = FilterChain('') filter_condition.add_condition(FilterChain.datatype + ".visible", "==", True) values, total_count = self.populate_option_values_for_dtype(project_id, param[KEY_TYPE], filter_condition, category_key) if param.get(KEY_ATTRIBUTES): # copy complex datatype attributes to all options complex_dt_attributes = self.fill_input_tree_with_options(param[KEY_ATTRIBUTES], project_id, category_key) for value in values: if value[KEY_NAME] != 'All': value[KEY_ATTRIBUTES] = complex_dt_attributes if total_count > MAXIMUM_DATA_TYPES_DISPLAYED: transformed_param[KEY_WARNING] = WARNING_OVERFLOW if param.get(KEY_REQUIRED) and len(values) > 0 and param.get(KEY_DEFAULT) is None: transformed_param[KEY_DEFAULT] = str(values[-1][KEY_VALUE]) transformed_param[KEY_FILTERABLE] = FilterChain.get_filters_for_type(param[KEY_TYPE]) transformed_param[KEY_TYPE] = TYPE_SELECT # If Portlet dynamic parameter, don't add the options instead # just add the default value. if KEY_DYNAMIC in param: dynamic_param = {KEY_NAME: param[KEY_DEFAULT], KEY_VALUE: param[KEY_DEFAULT]} transformed_param[KEY_OPTIONS] = [dynamic_param] else: transformed_param[KEY_OPTIONS] = values if type(param[KEY_TYPE]) == str: transformed_param[KEY_DATATYPE] = param[KEY_TYPE] else: data_type = param[KEY_TYPE] transformed_param[KEY_DATATYPE] = data_type.__module__ + '.' + data_type.__name__ ### DataType-attributes are no longer necessary, they were already copied on each OPTION transformed_param[KEY_ATTRIBUTES] = [] else: if param.get(KEY_OPTIONS) is not None: transformed_param[KEY_OPTIONS] = self.fill_input_tree_with_options(param[KEY_OPTIONS], project_id, category_key) if param.get(KEY_REQUIRED) and len(param[KEY_OPTIONS]) > 0 and param.get(KEY_DEFAULT) is None: transformed_param[KEY_DEFAULT] = str(param[KEY_OPTIONS][-1][KEY_VALUE]) if param.get(KEY_ATTRIBUTES) is not None: transformed_param[KEY_ATTRIBUTES] = self.fill_input_tree_with_options(param[KEY_ATTRIBUTES], project_id, category_key) result.append(transformed_param) return result