Skip to content

Filters

hypha.apply.api.v1.filters

SubmissionsFilter

SubmissionsFilter(*args, exclude=None, limit_statuses=None, **kwargs)

Bases: FilterSet

Source code in hypha/apply/api/v1/filters.py
def __init__(self, *args, exclude=None, limit_statuses=None, **kwargs):
    if exclude is None:
        exclude = []
    super().__init__(*args, **kwargs)
    self.filters["category_options"].extra["choices"] = [
        (option.id, option.value)
        for option in Option.objects.filter(category__filter_on_dashboard=True)
    ]

    self.fund.queryset = (Page.objects.type(FundType) | Page.objects.type(LabType),)

round class-attribute instance-attribute

round = ModelMultipleChoiceFilter(field_name='round', queryset=get_used_rounds())

status class-attribute instance-attribute

status = MultipleChoiceFilter(choices=PHASES)

active class-attribute instance-attribute

active = BooleanFilter(method='filter_active', label=gettext_lazy('Active'))

submit_date class-attribute instance-attribute

submit_date = DateFromToRangeFilter(field_name='submit_time', label=gettext_lazy('Submit date'))

fund class-attribute instance-attribute

fund = ModelMultipleChoiceFilter(field_name='page', label=gettext_lazy('fund'))

screening_statuses class-attribute instance-attribute

screening_statuses = ModelMultipleChoiceFilter(field_name='screening_statuses', queryset=get_screening_statuses(), null_label=gettext_lazy('No Screening'))

reviewers class-attribute instance-attribute

reviewers = ModelMultipleChoiceFilter(field_name='reviewers', queryset=get_all_reviewers())

lead class-attribute instance-attribute

lead = ModelMultipleChoiceFilter(field_name='lead', queryset=get_round_leads())

category_options class-attribute instance-attribute

category_options = MultipleChoiceFilter(choices=[], label=gettext_lazy('Category'), method='filter_category_options')

id class-attribute instance-attribute

id = ModelMultipleChoiceFilter(field_name='id', queryset=with_latest_update(), method='filter_id')

Meta

model class-attribute instance-attribute
fields class-attribute instance-attribute
fields = ('id', 'status', 'round', 'active', 'submit_date', 'fund', 'screening_statuses', 'reviewers', 'lead')

filter_active

filter_active(qs, name, value)
Source code in hypha/apply/api/v1/filters.py
def filter_active(self, qs, name, value):
    if value is None:
        return qs

    if value:
        return qs.active()
    else:
        return qs.inactive()

filter_category_options

filter_category_options(queryset, name, value)

Filter submissions based on the category options selected.

In order to do that we need to first get all the category fields used in the submission.

And then use those category fields to filter submissions with their form_data.

Source code in hypha/apply/api/v1/filters.py
def filter_category_options(self, queryset, name, value):
    """
    Filter submissions based on the category options selected.

    In order to do that we need to first get all the category fields used in the submission.

    And then use those category fields to filter submissions with their form_data.
    """
    query = Q()
    submission_data = queryset.values("form_fields", "form_data").distinct()
    for submission in submission_data:
        for field in submission["form_fields"]:
            if isinstance(field.block, CategoryQuestionBlock):
                try:
                    category_options = category_ids = submission["form_data"][
                        field.id
                    ]
                except KeyError:
                    include_in_filter = False
                else:
                    if isinstance(category_options, str):
                        category_options = [category_options]
                    include_in_filter = set(category_options) & set(value)
                # Check if filter options has any value in category options
                # If yes then those submissions should be filtered in the list
                if include_in_filter:
                    kwargs = {
                        "{0}__{1}".format("form_data", field.id): category_ids
                    }
                    query |= Q(**kwargs)
    return queryset.filter(query)

filter_id

filter_id(qs, name, value)
Source code in hypha/apply/api/v1/filters.py
def filter_id(self, qs, name, value):
    if not value:
        return qs
    return qs.filter(id__in=[x.id for x in value])

NewerThanFilter

Bases: ModelChoiceFilter

filter

filter(qs, value)
Source code in hypha/apply/api/v1/filters.py
def filter(self, qs, value):
    if not value:
        return qs

    return qs.newer(value)