# coding=utf-8

import django_filters

from apps.customer.models import Customer, CustomerAddress


class CustomerFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')
    tel = django_filters.CharFilter(field_name='tel', lookup_expr='icontains')
    range_time = django_filters.CharFilter(method='find_base_range_time')

    def find_base_range_time(self, queryset, *args):
        if args[1]:
            range_time = args[1].split(' - ')
            end_time = range_time[1] + ' 23:59:59'
            queryset = queryset.filter(user__date_joined__gte=range_time[0], user__date_joined__lte=end_time)
        return queryset


    class Meta:
        model = Customer
        fields = '__all__'


class CustomerAddressFilter(django_filters.FilterSet):

    class Meta:
        model = CustomerAddress
        fields = '__all__'
