Skip to content

Support multiple fields for a single parameter for filtering #5

@brendanreardon

Description

@brendanreardon

When passing parameters to a route, you have to specify what fields that they should map to within your model. These are then joined by an OR statement for each parameter. I was unsuccessful getting this to work and it may be worthwhile visiting in the future. Alternatively, we can add a step that maps parameter values based on aliases.

The in progress code was this, under app/blueprints/main/handlers.py within the BaseHandler class.

    @classmethod
    def apply_filters(cls, query: Query, parameters: ImmutableMultiDict) -> Query:
        filter_map = {
            'biomarker_type': [models.Biomarkers.biomarker_type],
            'biomarker': [models.Biomarkers.name],
            'gene': [models.Genes.name, models.Codings.id],
            'disease': [models.Diseases.name, models.Codings.id],
            'therapy': [models.Therapies.name, models.Codings.id]
        }

        parameter_filters = []
        filter_criteria = parameters.to_dict(flat=False)
        for filter_name, filter_values in filter_criteria.items():
            filter_name = filter_name.lower()
            if filter_name in filter_map:
                fields = filter_map[filter_name]

                # Create conditions for each value
                value_conditions = []
                for value in filter_values:
                    # OR across fields for each value
                    field_conditions = [field == value for field in fields]
                    value_conditions.append(sqlalchemy.or_(*field_conditions))

                # Combine all value conditions with OR
                parameter_filters.append(sqlalchemy.or_(*value_conditions))

        if parameter_filters:
            # Combine all parameter filters with AND
            query = query.filter(sqlalchemy.and_(*parameter_filters))

        return query

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions