# coding=utf-8

import django_filters

from .models import Option, Config, BizLog


class OptionFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')
    type = django_filters.CharFilter(field_name='type')

    class Meta:
        model = Option
        fields = '__all__'

class ConfigFilter(django_filters.FilterSet):

    class Meta:
        model = Config
        fields = '__all__'


class BizLogFilter(django_filters.FilterSet):
    type = django_filters.CharFilter(field_name='type')
    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(create_time__gte=range_time[0], create_time__lte=end_time)
        return queryset

    class Meta:
        model = BizLog
        fields = '__all__'
