# coding=utf-8
import django_filters

from .models import *
from apps.base import clean_datetime_range

class OrderFilter(django_filters.FilterSet):
    status = django_filters.CharFilter(field_name='status')
    no = django_filters.CharFilter(field_name='no', lookup_expr='icontains')
    school_name = django_filters.CharFilter(field_name='student__school__name', lookup_expr='icontains')
    grade_name = django_filters.CharFilter(field_name='student__classes__grade__name', lookup_expr='icontains')
    classes_name = django_filters.CharFilter(field_name='student__classes__name', lookup_expr='icontains')
    create_time_0 = django_filters.DateTimeFilter(field_name='create_time', lookup_expr='gte')
    create_time_1 = django_filters.DateTimeFilter(field_name='create_time', lookup_expr='lte')
    student_name = django_filters.CharFilter(field_name='student__name', lookup_expr='icontains')

    class Meta:
        model = Order
        fields = '__all__'

    def __init__(self, data=None, *args, **kwargs):
        data = clean_datetime_range(data, 'create_time')
        super(OrderFilter, self).__init__(data, *args, **kwargs)
