"""Contains custom filter sets to filter model data based on various criteria."""fromdatetimeimporttimedeltafromtypingimportClassVarimportdjango_filters# type: ignore[import-untyped]fromdjango.db.modelsimportQuerySetfromdjango.utilsimporttimezonefromnotifications.modelsimportNotificationModel
[docs]classNotificationFilter(django_filters.FilterSet):# type: ignore[misc]"""Filters notifications based on various criteria such as date range and status."""
[docs]deffilter_by_multiple_types(queryset:QuerySet[NotificationModel],_:str,value:str)->QuerySet[NotificationModel]:"""Split the comma-separated values into a list for types. Args: queryset: The queryset of `NotificationModel`. _: A placeholder parameter for type. value: The value to filter the queryset by. Returns: The filtered queryset based on the provided value. """ifvalue:types=value.split(',')returnqueryset.filter(notification_type__in=types)returnqueryset
@staticmethod
[docs]deffilter_by_multiple_sources(queryset:QuerySet[NotificationModel],_:str,value:str)->QuerySet[NotificationModel]:"""Split the comma-separated values into a list for sources. Args: queryset: The queryset of `NotificationModel`. _: A placeholder parameter for type. value: The value to filter the queryset by. Returns: The filtered queryset based on the provided value. """ifvalue:sources=value.split(',')returnqueryset.filter(notification_source__in=sources)returnqueryset
@staticmethod
[docs]deffilter_by_date_range(queryset:QuerySet[NotificationModel],_:str,value:str)->QuerySet[NotificationModel]:"""Filter the given QuerySet by date range. Args: queryset: The queryset of `NotificationModel`. _: A placeholder parameter for type. value: The value to filter the queryset by. Returns: The filtered queryset based on the provided value. """now=timezone.now()ifvalue=='today':returnqueryset.filter(created_at__date=now.date())ifvalue=='last7days':returnqueryset.filter(created_at__gte=now-timedelta(days=7))ifvalue=='last30days':returnqueryset.filter(created_at__gte=now-timedelta(days=30))ifvalue=='all':returnqueryset# No filtering, return allreturnqueryset