- Новый фильтр has_contact_channel показывает клиентов с записями в ContactChannel - Проверяет наличие альтернативных контактов (Instagram, Telegram и т.д.) - Добавлен чекбокс в форму фильтров с flex-wrap для переноса - Обновлено условие показа кнопки Очистить - Фильтр автоматически сохраняется в пагинации через url_replace
78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
# -*- 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'})
|
||
)
|
||
|
||
# Фильтр: есть канал связи
|
||
has_contact_channel = django_filters.BooleanFilter(
|
||
method='filter_has_contact_channel',
|
||
label='Есть канал связи',
|
||
widget=forms.CheckboxInput(attrs={'class': 'form-check-input'})
|
||
)
|
||
|
||
class Meta:
|
||
model = Customer
|
||
fields = ['has_notes', 'no_phone', 'no_email', 'has_contact_channel']
|
||
|
||
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
|
||
|
||
def filter_has_contact_channel(self, queryset, name, value):
|
||
"""Фильтр клиентов с каналами связи (Instagram, Telegram и т.д.)"""
|
||
if value:
|
||
from .models import ContactChannel
|
||
# Возвращаем только клиентов у которых есть хотя бы один канал связи
|
||
customer_ids = ContactChannel.objects.values_list('customer_id', flat=True).distinct()
|
||
return queryset.filter(id__in=customer_ids)
|
||
return queryset
|