Ejemplo n.º 1
0
def check_query_form(form_values, template_id, request=None):
    """Checks that values entered by the user match each element type

    Args:
        form_values:
        template_id:
        request:

    Returns:

    """
    template = template_api.get(template_id, request=request)
    namespaces = get_namespaces(template.content)
    default_prefix = get_default_prefix(namespaces)

    # check if there are no errors in the query
    errors = []

    if len(form_values) == 0:
        errors.append("The query is empty.")

    for field in form_values:
        element_value = get_element_value(field)
        element_name = field.get("name", "Unnamed field")
        element_type = field.get("type", None)
        # If there is a type to check
        if element_type:
            error = validate_element_value(
                element_name, element_type, element_value, default_prefix
            )
            if error is not None:
                errors.append(error)

    return errors
def _fields_to_query(form_values, template_id,
                     get_dot_notation_to_element_func, use_wildcard):
    """Takes values from the html tree and creates a query from them

    Args:
        form_values:
        template_id:
        use_wildcard:
        get_dot_notation_to_element_func:

    Returns:

    """
    # get template
    template = template_api.get(template_id)
    # get namespaces
    namespaces = get_namespaces(template.content)
    # get default prefix
    default_prefix = get_default_prefix(namespaces)

    query = dict()
    for field in form_values:
        bool_comp = field['operator']
        is_not = bool_comp == 'NOT'
        element_type = field.get('type', None)

        # get element value
        value = get_element_value(field)
        # get comparison operator
        comparison = get_element_comparison(field)

        element_id = field['id']

        if element_type == "query":
            try:
                saved_query = saved_query_api.get_by_id(element_id)
            except DoesNotExist:
                raise MongoQueryException(
                    "The saved query does not exist anymore.")
            criteria = build_query_criteria(json.loads(saved_query.query),
                                            is_not)
        else:
            data_structure_element = data_structure_element_api.get_by_id(
                element_id)
            element = get_dot_notation_to_element_func(data_structure_element,
                                                       namespaces)
            criteria = build_criteria(element, comparison, value, element_type,
                                      default_prefix, is_not, use_wildcard)

        if bool_comp == 'OR':
            query = build_or_criteria(query, criteria)
        elif bool_comp == 'AND':
            query = build_and_criteria(query, criteria)
        else:
            if form_values.index(field) == 0:
                query.update(criteria)
            else:
                query = build_and_criteria(query, criteria)

    return query
Ejemplo n.º 3
0
def sub_elements_to_query(form_values, namespaces, default_prefix, request):
    """Transforms HTML fields in a query on sub-elements

    Args:
        form_values:
        namespaces:
        default_prefix:
        request:

    Returns:

    """
    elem_match = []

    # get the parent path using the first element of the list
    parent_path = get_parent_path(form_values[0]["id"], namespaces, request)

    for i in range(0, len(form_values)):
        field = form_values[i]
        if field["selected"] is True:
            bool_comp = field["operator"]
            if bool_comp == "NOT":
                is_not = True
            else:
                is_not = False

            data_structure_element = data_structure_element_api.get_by_id(
                field["id"], request
            )
            element_type = data_structure_element.options["type"]
            element_name = data_structure_element.options["name"]
            value = get_element_value(field)
            comparison = get_element_comparison(field)

            criteria = build_criteria(
                element_name, comparison, value, element_type, default_prefix, is_not
            )

            elem_match.append(criteria)

    query = {parent_path: {"$elemMatch": {"$and": elem_match}}}

    return query
Ejemplo n.º 4
0
def fields_to_pretty_query(form_values):
    """Transforms fields from the HTML form into pretty representation

    Args:
        form_values:

    Returns:

    """

    query = ""

    for field in form_values:
        bool_comp = field["operator"]
        if bool_comp == "NOT":
            is_not = True
        else:
            is_not = False

        # get element value
        value = get_element_value(field)
        # get comparison operator
        comparison = get_element_comparison(field)

        element_type = field.get("type", None)
        if element_type == "query":
            criteria = build_query_pretty_criteria(field["name"], is_not)
        else:
            criteria = build_pretty_criteria(field["name"], comparison, value,
                                             is_not)

        if bool_comp == "OR":
            query = build_or_pretty_criteria(query, criteria)
        elif bool_comp == "AND":
            query = build_and_pretty_criteria(query, criteria)
        else:
            if form_values.index(field) == 0:
                query += criteria
            else:
                query = build_and_pretty_criteria(query, criteria)

    return query
Ejemplo n.º 5
0
def sub_elements_to_pretty_query(form_values, namespaces, request):
    """Transforms HTML fields in a user readable query

    Args:
        form_values:
        namespaces:
        request:

    Returns:

    """
    # get the parent path using the first element of the list
    parent_name = get_parent_name(form_values[0]["id"], namespaces, request)

    list_criteria = []
    for i in range(0, len(form_values)):
        field = form_values[i]
        if field["selected"] is True:
            bool_comp = field["operator"]
            if bool_comp == "NOT":
                is_not = True
            else:
                is_not = False

            data_structure_element = data_structure_element_api.get_by_id(
                field["id"], request)
            element_name = data_structure_element.options["name"]
            value = get_element_value(field)
            comparison = get_element_comparison(field)

            criteria = build_pretty_criteria(element_name, comparison, value,
                                             is_not)

            list_criteria.append(criteria)

    query = "{0}({1})".format(parent_name, ", ".join(list_criteria))

    return query