Example #1
0
class WorkItemFilter(django_filters.FilterSet):

    assigned_to__email = django_filters.CharFilter(lookup_expr='icontains')
    assigned_to__id = django_filters.CharFilter(lookup_expr='exact')
    created = django_filters.DateFromToRangeFilter(widget=DateRangeWidget(attrs={'type': 'date'}))
    completed = django_filters.DateFromToRangeFilter(widget=DateRangeWidget(attrs={'type': 'date'}))
    task__title = django_filters.CharFilter(lookup_expr="icontains")

    class Meta:
        model = WorkItem
        fields = ('workitem_id', 'assigned_to', 'created', 'completed', 'task')
Example #2
0
class PortfolioFilter(django_filters.FilterSet):

    sections__workitems__assigned_to__email = django_filters.CharFilter(lookup_expr='icontains')
    sections__workitems__assigned_to__id = django_filters.CharFilter(lookup_expr='exact')
    sections__workitems__title = django_filters.CharFilter(lookup_expr='icontains')
    portfolio_id = django_filters.CharFilter(lookup_expr='exact')
    created = django_filters.DateFromToRangeFilter(widget=DateRangeWidget(attrs={'type': 'date'}))
    completed = django_filters.DateFromToRangeFilter(widget=DateRangeWidget(attrs={'type': 'date'}))

    class Meta:
        model = Portfolio
        fields = ('portfolio_id', 'sections')
Example #3
0
class SectionFilter(django_filters.FilterSet):

    workitems__assigned_to__email = django_filters.CharFilter(lookup_expr='icontains')
    workitems__assigned_to__id = django_filters.CharFilter(lookup_expr='exact')
    workitems__title = django_filters.CharFilter(lookup_expr="icontains")
    section_id = django_filters.CharFilter(lookup_expr='exact')
    created = django_filters.DateFromToRangeFilter(widget=DateRangeWidget(attrs={'type': 'date'}))
    completed = django_filters.DateFromToRangeFilter(widget=DateRangeWidget(attrs={'type': 'date'}))

    class Meta:
        model = Section
        fields = ('section_id', 'workitems')
Example #4
0
class GovernmentProjectFilter(django_filters.FilterSet):
    title = django_filters.CharFilter(lookup_expr="icontains",
                                      widget=HiddenInput())
    id = django_filters.CharFilter(method="filter_id", widget=HiddenInput())
    implementation_dates = django_filters.DateFromToRangeFilter(
        label="Implementation dates",
        method="filter_implementation_dates",
        widget=DateRangeWidget(attrs={"placeholder": "YYYY/MM/DD"}),
    )

    class Meta:
        model = GovernmentProject
        fields = [
            "title",
            "total_project_cost",
            "administrative_area",
            "implementing_agency",
            "id",
        ]

    def filter_implementation_dates(self, queryset, name, value):
        filters = []
        if value:
            if value.start is not None:
                filters.append(Q(implementation_from__gte=value.start))
            if value.stop is not None:
                filters.append(Q(implementation_to__lte=value.stop))
        return queryset.filter(*filters)

    def filter_id(self, queryset, name, value):
        return queryset.filter(id__in=value.split(","))
Example #5
0
class InsurancePolicyFilter(django_filters.FilterSet):
    customer = django_filters.CharFilter(field_name='customer',
                                         method='filter_customer')

    car = django_filters.CharFilter(field_name='car', method='filter_car')

    end_date = django_filters.DateFromToRangeFilter(widget=DateRangeWidget(
        attrs={'type': 'date'}))

    def filter_customer(self, queryset, name, value):
        return queryset.filter(customer__name__icontains=value)

    def filter_car(self, queryset, name, value):
        return queryset.filter(car__registration_number__icontains=value)

    class Meta:
        model = InsurancePolicy
        fields = [
            'number', 'registration_date', 'begin_date', 'end_date',
            'customer', 'car', 'insurance_code'
        ]

        filter_overrides = {
            models.DateField: {
                'filter_class': django_filters.DateFilter,
                'extra': lambda f: {
                    'widget': DatePickerInput(format=DATE_INPUT_FORMAT),
                },
            },
        }
Example #6
0
class UsersFilter(filters.FilterSet):
    date_joined = DateFromToRangeFilter(widget=DateRangeWidget(
        attrs={
            'placeholder': 'MM/DD/YYYY',
            'type': 'date'
        }))
    is_active = ChoiceFilter(choices=USER_STATUS)

    # date_joined__lt = DateFilter(field_name='date_joined', lookup_expr='lt', label='Joined Before')
    class Meta:
        model = User
        attrs = {"class": "table table-hover table-bordered table-sm"}
        fields = ("phone", "email", "is_active")
Example #7
0
class MessageSmsInsurancePolicyExpiresFilter(django_filters.FilterSet):
    insurance_policy = django_filters.CharFilter(
        field_name='insurance_policy', method='filter_insurance_policy')

    created = django_filters.DateFromToRangeFilter(widget=DateRangeWidget(
        attrs={'type': 'date'}))

    class Meta:
        model = MessageSmsInsurancePolicyExpires
        fields = ['created', 'sid', 'body', 'insurance_policy']

    def filter_insurance_policy(self, queryset, name, value):
        return queryset.filter(insurance_policy__number__icontains=value)
Example #8
0
class BuildFlowFilterSet(django_filters.rest_framework.FilterSet):
    """A "conditional" filterset for generating the BuildFlow sub-select.

    The tricky bit is that this filter serves three different jobs.

    1. It validates incoming parameters (which is not strictly necessary
        but probably difficult to turn off and perhaps sometimes useful).

    2. It populates the django-filter form.

    3. It drives actual filtering of the build-list sub-query.
    """

    repo_choices = (Repository.objects.values_list(
        "name", "name").order_by("name").distinct())

    repo = django_filters.rest_framework.ChoiceFilter(
        field_name="rel_repo__name", label="Repo Name", choices=repo_choices)

    branch = django_filters.rest_framework.CharFilter(
        field_name="rel_branch__name", label="Branch Name")

    plan_choices = Plan.objects.values_list(
        "name", "name").order_by("name").distinct()
    plan = django_filters.rest_framework.ChoiceFilter(
        field_name="rel_plan__name", label="Plan Name", choices=plan_choices)

    # Django-filter's DateRangeFilter is kind of ... special
    # disable until we can fix it.
    #
    # recentdate = DateRangeFilter(label="Recent Date", field_name="time_end")
    # disable_by_default.append("recentdate")

    daterange = django_filters.rest_framework.DateFromToRangeFilter(
        field_name="week_start",
        label="Date range",
        widget=DateRangeWidget(attrs={"type": "date"}),
    )

    def dummy_filter(self, queryset, name, value):
        return queryset

    fields_and_stuff = locals()

    build_fields = {}

    for name in ("repo", "plan", "branch"):
        # make a list of db field_names for use in grouping
        build_fields[name] = fields_and_stuff[name].field_name
Example #9
0
class CurrencyHistoryFilterSet(rest_framework.FilterSet):
    """Currency History FilterSet"""

    hour: rest_framework.NumberFilter = rest_framework.NumberFilter(
        method="filter_hour",
        label="Hour",
    )

    date_range: DateTimeFromToRangeFilter = DateTimeFromToRangeFilter(
        method="filter_date_range",
        label="Date range",
        widget=DateRangeWidget(attrs={"type": "date"}),
    )

    date_: rest_framework.DateFilter = rest_framework.DateFilter(
        method="filter_date",
        label="Date",
    )

    class Meta:
        """Meta."""

        model: Type[CurrencyHistory] = CurrencyHistory
        fields: Dict[str, List[str]] = {
            "currency": ["exact"],
        }

    def filter_hour(self, queryset: QuerySet, name: str,
                    value: int) -> QuerySet:
        """Filter history by hour"""
        return queryset.filter(creation_date__hour=value)

    def filter_date_range(self, queryset: QuerySet, name: str,
                          value: slice) -> QuerySet:
        """Filter history between to dates"""
        return queryset.filter(creation_date__gte=value.start,
                               creation_date__lte=value.stop)

    def filter_date(self, queryset: QuerySet, name: str,
                    value: date) -> QuerySet:
        """Filter history by date"""
        return queryset.filter(
            creation_date__day=value.day,
            creation_date__year=value.year,
            creation_date__month=value.month,
        )
Example #10
0
class UserFilterAPI(FilterSet):
    date_joined = DateTimeFromToRangeFilter(
        label='Date joined range',
        field_name='created',
        lookup_expr='date',
        widget=DateRangeWidget(attrs={
            'class': 'form-control datetimepicker-input',
            'type': 'date',
        }))
    is_active = BooleanFilter()
    user_id = BaseInFilter(field_name='id', label='User id')

    class Meta:
        model = User
        fields = {
            'email': ['icontains'],
            'first_name': ['icontains'],
            'last_name': ['icontains'],
        }
Example #11
0
class RateFilter(django_filters.FilterSet):
    ordering = django_filters.OrderingFilter(
        label='Ordering',
        choices=(
            ('created', 'Date (ascending)'),
            ('-created', 'Date (descending)'),
            ('sale', 'Currency sale (ascending)'),
            ('-sale', 'Currency sale (descending)'),
            ('buy', 'Currency buy (ascending)'),
            ('-buy', 'Currency buy (descending)'),
        ),
    )
    created = django_filters.DateTimeFromToRangeFilter(
        label='Date range',
        field_name='created',
        lookup_expr='date',
        widget=DateRangeWidget(attrs={
            'class': 'form-control datetimepicker-input',
            'type': 'date',
        }))

    class Meta:
        model = Rate
        fields = '__all__'
Example #12
0
class BuildFlowFilterSet(TurnFilterSetOffByDefaultBase):
    """A "conditional" filterset for generating the BuildFlow sub-select.

    The tricky bit is that this filter serves three different jobs.

    1. It validates incoming parameters (which is not strictly necessary
        but probably difficult to turn off and perhaps sometimes useful).

    2. It populates the django-filter form.

    3. It drives actual filtering of the build-list sub-query.

    Feature 3 is turned on and off by the really_filter parameter.
    We do NOT want django-filter to try to automatically filter the
    output queryset based on these filters because it is actually the
    sub-query that we need to filter.

    Accordingly, its fields are turned off by default (see disable_by_default) 
    and turned on explicitly ("really_filter") when it is created by get_queryset
    """

    disable_by_default = []

    repo_choices = (
        Repository.objects.values_list("name", "name").order_by("name").distinct()
    )
    repo = django_filters.rest_framework.ChoiceFilter(
        field_name="build__repo__name", label="Repo Name", choices=repo_choices
    )
    disable_by_default.append("repo")

    branch_choices = (
        Branch.objects.values_list("name", "name").order_by("name").distinct()
    )
    branch = django_filters.rest_framework.ChoiceFilter(
        field_name="build__branch__name", label="Branch Name", choices=branch_choices
    )
    disable_by_default.append("branch")

    plan_choices = Plan.objects.values_list("name", "name").order_by("name").distinct()
    plan = django_filters.rest_framework.ChoiceFilter(
        field_name="build__plan__name", label="Plan Name", choices=plan_choices
    )
    disable_by_default.append("plan")

    flow_choices = (
        BuildFlow.objects.values_list("flow", "flow").order_by("flow").distinct()
    )
    flow = django_filters.rest_framework.ChoiceFilter(
        field_name="flow", label="Flow Name", choices=flow_choices
    )
    disable_by_default.append("flow")

    recentdate = DateRangeFilter(label="Recent Date", field_name="time_end")
    disable_by_default.append("recentdate")

    daterange = django_filters.rest_framework.DateFromToRangeFilter(
        field_name="time_end",
        label="Date range",
        widget=DateRangeWidget(attrs={"type": "date"}),
    )
    disable_by_default.append("daterange")

    # This is not really a filter. It's actually just a query input but putting it
    # here lets me get it in the form.
    build_flows_limit = django_filters.rest_framework.NumberFilter(
        method="dummy_filter", label="Build Flows Limit (default: 100)"
    )

    fields_and_stuff = locals()

    build_fields = {}

    for name in ("repo", "plan", "branch", "flow"):
        # make a list of db field_names for use in grouping
        build_fields[name] = fields_and_stuff[name].field_name