diff --git a/myproject/customers/filters.py b/myproject/customers/filters.py new file mode 100644 index 0000000..c7becba --- /dev/null +++ b/myproject/customers/filters.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +""" +Фильтры для клиентов с использованием django-filter +""" + +import django_filters +from django import forms +from .models import Customer + + +class CustomerFilter(django_filters.FilterSet): + """ + Фильтр для списка клиентов + Поддерживает фильтрацию по: + - Наличию заметок + - Отсутствию телефона + - Отсутствию email + """ + + # Фильтр: есть заметки + has_notes = django_filters.BooleanFilter( + method='filter_has_notes', + label='Есть заметки', + widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}) + ) + + # Фильтр: нет телефона + no_phone = django_filters.BooleanFilter( + method='filter_no_phone', + label='Нет телефона', + widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}) + ) + + # Фильтр: нет email + no_email = django_filters.BooleanFilter( + method='filter_no_email', + label='Нет email', + widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}) + ) + + class Meta: + model = Customer + fields = ['has_notes', 'no_phone', 'no_email'] + + def filter_has_notes(self, queryset, name, value): + """Фильтр клиентов с заметками""" + if value: + return queryset.filter(notes__isnull=False).exclude(notes='') + return queryset + + def filter_no_phone(self, queryset, name, value): + """Фильтр клиентов без телефона""" + if value: + return queryset.filter(phone__isnull=True) | queryset.filter(phone='') + return queryset + + def filter_no_email(self, queryset, name, value): + """Фильтр клиентов без email""" + if value: + return queryset.filter(email__isnull=True) | queryset.filter(email='') + return queryset diff --git a/myproject/customers/templates/customers/customer_list.html b/myproject/customers/templates/customers/customer_list.html index 2ea2508..8fdfbb6 100644 --- a/myproject/customers/templates/customers/customer_list.html +++ b/myproject/customers/templates/customers/customer_list.html @@ -27,21 +27,48 @@ - +