class UserFilter(filters.FilterSet): date_from = filters.DateFilter(name='date_joined', lookup_type='gte') date_to = filters.DateFilter(name='date_joined', lookup_type='lte') class Meta: model = get_user_model() fields = ('date_from', 'date_to')
class UserCarsFilter(filters.FilterSet): created_from = filters.DateFilter(name='created', lookup_type='gte') created_to = filters.DateFilter(name='created', lookup_type='lte') class Meta: model = UserCars fields = ('created_from', 'created_to')
class TimingSessionFilter(filters.FilterSet): start_date = filters.DateFilter(name='start_time', lookup_type='gte') stop_date = filters.DateFilter(name='start_time', lookup_type='lte') athlete = filters.NumberFilter(name='splits__athlete', distinct=True) class Meta: model = TimingSession fields = ('start_date', 'stop_date', 'private', 'coach', 'athlete')
class UpfileFilter(filters.FilterSet): status = filters.RelatedFilter(StatusFilter, name='status') start_date = filters.DateFilter(name="created_date", lookup_type='gte') end_date = filters.DateFilter(name="created_date", lookup_type='lte') name = filters.ModelMultipleChoiceFilter # created_date__gte=filters.DateFilter(name='created_date__gte') class Meta: model = Up_file fields = [ 'blob_name', 'department', 'status', 'created_date', 'start_date', 'start_date' ]
class AbsenceCategoryFilter(filters.FilterSet): name = filters.AllLookupsFilter() created_at = filters.DateFilter() created__gte = filters.DateFilter(name='created_at', lookup_expr='gte') created__lte = filters.DateFilter(name='created_at', lookup_expr='lte') created__gt = filters.DateFilter(name='created_at', lookup_expr='gt') created__lt = filters.DateFilter(name='created_at', lookup_expr='lt') billable = filters.BooleanFilter() active = filters.BooleanFilter() class Meta: model = AbsenceCategory
class ProgressFilter(filters.FilterSet): done_at = filters.DateFilter() done_at__gte = filters.DateFilter(name='done_at', lookup_expr='gte') done_at__lte = filters.DateFilter(name='done_at', lookup_expr='lte') done_at__gt = filters.DateFilter(name='done_at', lookup_expr='gt') done_at__lt = filters.DateFilter(name='done_at', lookup_expr='lt') created_at = filters.DateFilter() created__gte = filters.DateFilter(name='created_at', lookup_expr='gte') created__lte = filters.DateFilter(name='created_at', lookup_expr='lte') created__gt = filters.DateFilter(name='created_at', lookup_expr='gt') created__lt = filters.DateFilter(name='created_at', lookup_expr='lt') note = filters.AllLookupsFilter() project = filters.RelatedFilter(ProjectFilter, name='project') user = filters.RelatedFilter(UserFilter, name='user') class Meta: model = Progress
class ServiceCategoryFilter(filters.FilterSet): category_name = filters.AllLookupsFilter(name="category_name") crt_time_after = filters.DateFilter(name="create_time", lookup_type='gte') class Meta: model = Service_category fields = [ 'id', 'category_name', 'cate_abbreviation', 'crt_time_after', ]
class PhotoFilter(drf_filters.FilterSet): date_taken_from = drf_filters.DateFilter(name='date_taken', lookup_expr='gte') date_taken_to = drf_filters.DateFilter(name='date_taken', lookup_expr='lte') class Meta: model = models.Photo fields = [ 'date_taken_from', 'date_taken_to', 'motive', 'security_level', 'category', 'media', 'album', 'place', 'tags', 'scanned', 'on_home_page', 'splash', 'lapel', ]
def add_date_filtering(cls): """ Adds date-filtering to filtering-class 'cls'. Fields provided to cls.Meta.date_fields are given date-filtering for =, __lte, __gte Fields provided to cls.Meta.date_range_fields are given DTR date-range-filtering for options like: Today, Last 7 Days :param cls: Filtering class """ if hasattr(cls.Meta, 'date_fields'): for field in cls.Meta.date_fields: filter_update = { field: filters.DateFilter(name=field, lookup_type='contains'), field + '__lte': filters.DateTimeFilter(name=field, lookup_type='lte'), field + '__gte': filters.DateTimeFilter(name=field, lookup_type='gte') } cls.declared_filters.update(filter_update) cls.base_filters.update(filter_update) # class DateTimeFilter(object): # """ # Represents DateTime field as Date and applies filtering as Date field. # Use this class if the field type is DateTime but filtering needs to be applied as Date field. # """ # # @staticmethod # def date__gte(queryset, value, field): # date_time = dateutils.get_date_time(value) # if '__gte' not in field: # field += '__gte' # # kwargs = {field: date_time} # return queryset.filter(**kwargs) # # @staticmethod # def date__lt(queryset, value, field): # date_time = dateutils.get_date_time(value) + datetime.timedelta(days=1) # if '__lt' not in field: # field += '__lt' # # kwargs = {field: date_time} # return queryset.filter(**kwargs) # # @staticmethod # def date(queryset, value, field): # queryset = DateTimeFilter.date__gte(queryset, value, field) # return DateTimeFilter.date__lt(queryset, value, field)
class BountiesFilter(filters.FilterSet): categories = filters.RelatedFilter(CategoriesFilter, name='categories', queryset=Category.objects.all()) fulfillments = filters.RelatedFilter(FulfillmentsFilter, name='fulfillments', queryset=Fulfillment.objects.all()) bounty_created = filters.DateFilter(name='bounty_created') class Meta: model = Bounty fields = { 'issuer': ['exact'], 'fulfillmentAmount': ['exact', 'lt', 'gt', 'lte'], 'bountyStage': ['exact'], 'bounty_created': ['lt', 'gt', 'exact'], 'deadline': ['lt', 'gt', 'exact'], 'bounty_id': ['exact'], }
class BountiesFilter(filters.FilterSet): categories = filters.RelatedFilter(CategoriesFilter, name='categories', queryset=Category.objects.all()) fulfillments = filters.RelatedFilter(FulfillmentsFilter, name='fulfillments', queryset=Fulfillment.objects.all()) bounty_created = filters.DateFilter(name='bounty_created') class Meta: model = Bounty fields = { 'platform': ['in', 'exact'], 'issuer': ['exact'], 'experience_level': ['exact', 'in'], 'fulfillment_amount': ['exact', 'lt', 'gt', 'lte'], 'bounty_stage': ['exact', 'in'], 'bounty_created': ['lt', 'gt', 'exact'], 'deadline': ['lt', 'gt', 'exact'], 'bounty_id': ['exact'], 'contract_version': ['exact'] }
class ДокументFilter(filters.FilterSet): disqualification = filters.BooleanFilter(name='СвЮЛ__СведДолжнФЛ__СвДискв', lookup_expr='isnull') codeEduMethod = filters.CharFilter( name='СвЮЛ__СвОбрЮЛ__СпОбрЮЛ__КодСпОбрЮЛ') region = filters.CharFilter(name='СвЮЛ__СвАдресЮЛ__АдресРФ__КодРегион') search = filters.CharFilter(method='complex_search_filter') fio = filters.CharFilter(method='complex_fio_filter') reg_start_date = filters.DateFilter(name='СвЮЛ__ДатаОГРН', lookup_expr='gte') reg_end_date = filters.DateFilter(name='СвЮЛ__ДатаОГРН', lookup_expr='lte') state = filters.CharFilter(name='СвЮЛ__СвСтатус__Св_Статус__КодСтатусЮЛ') isactive = filters.BooleanFilter(method='is_active_filter') isAddrFalsity = filters.BooleanFilter(method='check_addr_falsity') isAddrChange = filters.BooleanFilter(method='check_addr_сhange') isemail = filters.BooleanFilter(method='check_email_filter') email = filters.CharFilter(name='СвЮЛ__СвАдрЭлПочты__E_mail', lookup_expr='icontains') index = filters.CharFilter(name='СвЮЛ__СвАдресЮЛ__АдресРФ__Индекс', lookup_expr='icontains') codeKLADR = filters.CharFilter( name='СвЮЛ__СвАдресЮЛ__АдресРФ__КодАдрКладр', lookup_expr='icontains') area = filters.CharFilter( name='СвЮЛ__СвАдресЮЛ__АдресРФ__Район__НаимРайон', lookup_expr='icontains') city = filters.CharFilter( name='СвЮЛ__СвАдресЮЛ__АдресРФ__Город__НаимГород', lookup_expr='icontains') locality = filters.CharFilter( name='СвЮЛ__СвАдресЮЛ__АдресРФ__НаселПункт__НаимНаселПункт', lookup_expr='icontains') street = filters.CharFilter( name='СвЮЛ__СвАдресЮЛ__АдресРФ__Улица__НаимУлица', lookup_expr='icontains') regNum = filters.CharFilter(name='СвЮЛ__СвОбрЮЛ__РегНом', lookup_expr='icontains') startregDate = filters.DateFilter(name='СвЮЛ__СвОбрЮЛ__ДатаРег', lookup_expr='gte') endregDate = filters.DateFilter(name='СвЮЛ__СвОбрЮЛ__ДатаРег', lookup_expr='lte') isChartCapital = filters.BooleanFilter(method='check_isChartCapital') nameCapital = filters.CharFilter(name='СвЮЛ__СвУстКап__НаимВидКап', lookup_expr='icontains') summCap = filters.CharFilter(name='СвЮЛ__СвУстКап__СумКап', lookup_expr='lte') isSvUprOrg = filters.BooleanFilter(method='check_isSvUprOrg') nameUprOrg = filters.CharFilter(method='complex_UprOrg_filter') isSvWithoutAtt = filters.BooleanFilter(method='check_isSvWithoutAtt') fioWA = filters.CharFilter(method='complex_fioWA_filter') isFounder = filters.BooleanFilter(method='check_isFounder') isFRus = filters.BooleanFilter(method='check_isFRus') isFFl = filters.BooleanFilter(method='check_isFFl') isFGOS = filters.BooleanFilter(method='check_isFGOS') isFIn = filters.BooleanFilter(method='check_isFIn') okved = filters.CharFilter(method='okved_filter') okvedtxt = filters.CharFilter(method='okved_filter') numberL = filters.CharFilter(name='СвЮЛ__СвЛицензия__НомЛиц', lookup_expr='icontains', distinct=True) startLicense = filters.DateFilter(name='СвЮЛ__СвЛицензия__ДатаНачЛиц', lookup_expr='gte', distinct=True) endLicense = filters.DateFilter(name='СвЮЛ__СвЛицензия__ДатаОкончЛиц', lookup_expr='lte', distinct=True) nameL = filters.CharFilter(method='license_filter') grn = filters.CharFilter(name='СвЮЛ__СвЗапЕГРЮЛ__ГРН', lookup_expr='icontains', distinct=True) startRecord = filters.DateFilter(name='СвЮЛ__СвЗапЕГРЮЛ__ДатаЗап', lookup_expr='gte', distinct=True) endRecord = filters.DateFilter(name='СвЮЛ__СвЗапЕГРЮЛ__ДатаЗап', lookup_expr='lte', distinct=True) declarer = filters.CharFilter(method='complex_declarer_filter') documents = filters.CharFilter( name='СвЮЛ__СвЗапЕГРЮЛ__СведПредДок__НаимДок', lookup_expr='icontains', distinct=True) class Meta: model = Документ fields = { 'id', 'ИдДок', 'СвЮЛ', } def complex_search_filter(self, qs, name, value): #print(qs.count()) if value.find(' ') == -1: qs_ = qs.filter( Q(СвЮЛ__ОГРН__icontains=value) | Q(СвЮЛ__ИНН__icontains=value) | Q(СвЮЛ__СвНаимЮЛ__НаимЮЛСокр__icontains=value) | Q(СвЮЛ__СвНаимЮЛ__НаимЮЛПолн__icontains=value)).distinct() else: qs_ = qs.filter(СвЮЛ__СвНаимЮЛ__search_vector=SearchQuery( value, config='russian')) # qs2 = qs.filter(СвЮЛ__ИНН__icontains=value) #print(qs_.count()) return qs_ def complex_UprOrg_filter(self, qs, name, value): # print(qs.count()) if value.find(' ') == -1: qs_ = qs.filter( Q(СвЮЛ__СвУпрОрг__НаимИННЮЛ__ОГРН__icontains=value) | Q(СвЮЛ__СвУпрОрг__НаимИННЮЛ__ИНН__icontains=value) | Q(СвЮЛ__СвУпрОрг__НаимИННЮЛ__НаимЮЛПолн__icontains=value) ).distinct() else: qs_ = qs.filter(СвЮЛ__СвУпрОрг__search_vector=SearchQuery( value, config='russian')) # qs2 = qs.filter(СвЮЛ__ИНН__icontains=value) # print(qs_.count()) return qs_ def complex_fio_filter(self, qs, name, value): #print(qs.count(), name, value) qs1 = qs.filter( Q(СвЮЛ__СвУчредит__УчрФЛ__СвФЛ__search_vector=SearchQuery( value, config='russian')) | Q(СвЮЛ__СвУчредит__УчрФЛ__СвФЛ__ИННФЛ__icontains=value) | Q(СвЮЛ__СвУчредит__УчрФЛ__СвДовУпрФЛ__СвФЛ__search_vector= SearchQuery(value, config='russian')) | Q(СвЮЛ__СвУчредит__УчрФЛ__СвДовУпрФЛ__СвФЛ__ИННФЛ__icontains=value) ).distinct() #qs2 = qs.filter( #qs_ = qs1.union(qs2).all() #print(qs1.count()) #print(qs2.count()) #print(qs_.count()) #Q(СвЮЛ__СвУчредит__УчрФЛ__СвДовУпрФЛ__СвФЛ__search_vector=SearchQuery(value, config='russian')) | #Q(СвЮЛ__СвУчредит__УчрФЛ__ЛицоУпрНасл__СвФЛ__search_vector=SearchQuery(value, config='russian')) | #Q(СвЮЛ__СвУчредит__УчрРФСМО__СвФЛОсущПр__СвФЛ__search_vector=SearchQuery(value, config='russian')) | #Q(СвЮЛ__СвКФХПредш__СвФЛ__search_vector=SearchQuery(value, config='russian')) | #Q(СвЮЛ__СвКФХПреем__СвФЛ__search_vector=SearchQuery(value, config='russian'))) #print(len(qs_)) return qs1 def complex_fioWA_filter(self, qs, name, value): # print(qs.count(), name, value) #qs1 = qs.filter(СвЮЛ__СведДолжнФЛ__СвФЛ__ИННФЛ__icontains=value) qs1 = qs.filter( Q(СвЮЛ__СведДолжнФЛ__СвФЛ__search_vector=SearchQuery( value, config='russian')) | Q(СвЮЛ__СведДолжнФЛ__СвФЛ__ИННФЛ__icontains=value)).distinct() return qs1 def complex_declarer_filter(self, qs, name, value): # print(qs.count(), name, value) # qs1 = qs.filter(СвЮЛ__СведДолжнФЛ__СвФЛ__ИННФЛ__icontains=value) qs1 = qs.filter( Q(СвЮЛ__СвЗапЕГРЮЛ__СвЗФЛ__СвФЛ__СвФИОИНН__search_vector= SearchQuery(value, config='russian')) | Q(СвЮЛ__СвЗапЕГРЮЛ__СвЗФЛ__СвФЛ__СвФИОИНН__ИННФЛ__icontains=value )).distinct() return qs1 def okved_filter(self, qs, name, value): # qs1 = qs.filter(СвЮЛ__СведДолжнФЛ__СвФЛ__ИННФЛ__icontains=value) qs1 = qs.filter( Q(СвЮЛ__СвОКВЭД__СвОКВЭДОсн__КодОКВЭД__exact=value) | Q(СвЮЛ__СвОКВЭД__СвОКВЭДДоп__КодОКВЭД__exact=value) | Q(СвЮЛ__СвОКВЭД__СвОКВЭДОсн__search_vector=SearchQuery( value, config='russian')) | Q(СвЮЛ__СвОКВЭД__СвОКВЭДДоп__search_vector=SearchQuery( value, config='russian'))).distinct() #Q(СвЮЛ__СвОКВЭД__СвОКВЭДОсн__КодОКВЭД__search_vector=SearchQuery(value, config='russian')) | return qs1 def license_filter(self, qs, name, value): qs1 = qs.filter(СвЮЛ__СвЛицензия__search_vector=SearchQuery( value, config='russian')).distinct() return qs1 def check_addr_сhange(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter( СвЮЛ__СвАдресЮЛ__СвРешИзмМН__isnull=False).distinct() return qs_ def check_addr_falsity(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter( СвЮЛ__СвАдресЮЛ__СвНедАдресЮЛ__isnull=False).distinct() return qs_ def check_email_filter(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter(СвЮЛ__СвАдрЭлПочты__isnull=False).distinct() return qs_ def check_isChartCapital(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter(СвЮЛ__СвУстКап__isnull=False).distinct() return qs_ def check_isSvUprOrg(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter(СвЮЛ__СвУпрОрг__isnull=False).distinct() return qs_ def check_isSvWithoutAtt(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter(СвЮЛ__СведДолжнФЛ__isnull=False).distinct() return qs_ def check_isFounder(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter(СвЮЛ__СвУчредит__isnull=False).distinct() return qs_ def check_isFRus(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter(СвЮЛ__СвУчредит__УчрЮЛРос__isnull=False).distinct() return qs_ def check_isFFl(self, qs, name, value): # print(name, value) if value is False: qs_ = qs else: qs_ = qs.filter(СвЮЛ__СвУчредит__УчрФЛ__isnull=False).distinct() return qs_ def check_isFGOS(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter(СвЮЛ__СвУчредит__УчрРФСМО__isnull=False).distinct() return qs_ def check_isFIn(self, qs, name, value): if value is False: qs_ = qs else: qs_ = qs.filter(СвЮЛ__СвУчредит__УчрЮЛИн__isnull=False).distinct() return qs_ def is_active_filter(self, qs, name, value): if value is False: qs_ = qs.filter(СвЮЛ__СвПрекрЮЛ__isnull=False).distinct() else: qs_ = qs.filter(СвЮЛ__СвПрекрЮЛ__isnull=True).distinct() return qs_
class PaymentFilter(filters.FilterSet): """Filter payments on payment plan, activity, case, dates, etc.""" payment_schedule = filters.RelatedFilter( PaymentScheduleFilter, field_name="payment_schedule", queryset=PaymentSchedule.objects.all(), ) case = filters.RelatedFilter( CaseForPaymentFilter, field_name="payment_schedule__activity__appropriation__case", label=Case._meta.verbose_name.title(), queryset=Case.objects.all(), ) activity = filters.RelatedFilter( ActivityFilter, field_name="payment_schedule__activity", label=Activity._meta.verbose_name.title(), queryset=Activity.objects.all(), ) paid_date__gte = filters.DateFilter( field_name="paid_date", lookup_expr="gte", label=gettext("Betalingsdato større eller lig med"), ) paid_date__lte = filters.DateFilter( field_name="paid_date", lookup_expr="lte", label=gettext("Betalingsdato mindre eller lig med"), ) date__gte = filters.DateFilter( field_name="date", lookup_expr="gte", label=gettext("Dato større eller lig med"), ) date__lte = filters.DateFilter( field_name="date", lookup_expr="lte", label=gettext("Dato mindre eller lig med"), ) paid_date_or_date__gte = filters.DateFilter( method="filter_paid_date_or_date_gte", label=gettext("Betalingsdato eller Dato større eller lig med"), ) paid_date_or_date__lte = filters.DateFilter( method="filter_paid_date_or_date_lte", label=gettext("Betalingsdato eller Dato mindre eller lig med"), ) generic_time_choices = ( ("previous", "Forrige"), ("current", "Nuværende"), ("next", "Næste"), ) paid_date_or_date_week = filters.ChoiceFilter( method="filter_paid_date_or_date_week", label=gettext("Betalingsdato eller Dato for uge"), choices=generic_time_choices, ) paid_date_or_date_month = filters.ChoiceFilter( method="filter_paid_date_or_date_month", label=gettext("Betalingsdato eller Dato for måned"), choices=generic_time_choices, ) paid_date_or_date_year = filters.ChoiceFilter( method="filter_paid_date_or_date_year", label=gettext("Betalingsdato eller Dato for år"), choices=generic_time_choices, ) date_week = filters.ChoiceFilter( method="filter_date_week", label=gettext("Betalingsdato eller Dato for uge"), choices=generic_time_choices, ) date_month = filters.ChoiceFilter( method="filter_date_month", label=gettext("Betalingsdato eller Dato for måned"), choices=generic_time_choices, ) date_year = filters.ChoiceFilter( method="filter_date_year", label=gettext("Betalingsdato eller Dato for år"), choices=generic_time_choices, ) def filter_paid_date_or_date_gte(self, queryset, name, value): """Filter on value <= "best known payment date".""" return queryset.paid_date_or_date_gte(value) def filter_paid_date_or_date_lte(self, queryset, name, value): """Filter on value >= "best known payment date".""" return queryset.paid_date_or_date_lte(value) def filter_paid_date_or_date_week(self, queryset, name, value): """Filter best known payment date on previous, current, next week.""" now = timezone.now().date() if value == "previous": reference_date = now - relativedelta(weeks=1) elif value == "current": reference_date = now elif value == "next": # pragma: no branch reference_date = now + relativedelta(weeks=1) min_date = reference_date - relativedelta(weekday=MO(-1)) max_date = reference_date + relativedelta(weekday=SU(1)) return queryset.paid_date_or_date_lte(max_date).paid_date_or_date_gte( min_date) def filter_paid_date_or_date_month(self, queryset, name, value): """Filter best known payment date on previous, current, next month.""" now = timezone.now().date() if value == "previous": reference_date = now - relativedelta(months=1) elif value == "current": reference_date = now elif value == "next": # pragma: no branch reference_date = now + relativedelta(months=1) min_date = reference_date + relativedelta(day=1) # day=31 will always return the last day of the month. max_date = reference_date + relativedelta(day=31) return queryset.paid_date_or_date_lte(max_date).paid_date_or_date_gte( min_date) def filter_paid_date_or_date_year(self, queryset, name, value): """Filter best known payment date on previous, current, next year.""" now = timezone.now().date() if value == "previous": year = (now - relativedelta(years=1)).year elif value == "current": year = now.year elif value == "next": # pragma: no branch year = (now + relativedelta(years=1)).year min_date = date(day=1, month=1, year=year) max_date = date(day=31, month=12, year=year) return queryset.paid_date_or_date_lte(max_date).paid_date_or_date_gte( min_date) def filter_date_week(self, queryset, name, value): """Filter date on previous, current, next week.""" now = timezone.now().date() if value == "previous": reference_date = now - relativedelta(weeks=1) elif value == "current": reference_date = now elif value == "next": # pragma: no branch reference_date = now + relativedelta(weeks=1) min_date = reference_date - relativedelta(weekday=MO(-1)) max_date = reference_date + relativedelta(weekday=SU(1)) return queryset.filter(date__lte=max_date, date__gte=min_date) def filter_date_month(self, queryset, name, value): """Filter date on previous, current, next month.""" now = timezone.now().date() if value == "previous": reference_date = now - relativedelta(months=1) elif value == "current": reference_date = now elif value == "next": # pragma: no branch reference_date = now + relativedelta(months=1) min_date = reference_date + relativedelta(day=1) # day=31 will always return the last day of the month. max_date = reference_date + relativedelta(day=31) return queryset.filter(date__lte=max_date, date__gte=min_date) def filter_date_year(self, queryset, name, value): """Filter date on previous, current, next year.""" now = timezone.now().date() if value == "previous": year = (now - relativedelta(years=1)).year elif value == "current": year = now.year elif value == "next": # pragma: no branch year = (now + relativedelta(years=1)).year min_date = date(day=1, month=1, year=year) max_date = date(day=31, month=12, year=year) return queryset.filter(date__lte=max_date, date__gte=min_date) class Meta: model = Payment fields = "__all__"