# coding=utf-8

import django_filters

from .models import SysLog

from utils.format import clean_datetime_range

class SysLogFilter(django_filters.FilterSet):
    type = django_filters.ChoiceFilter(choices=SysLog.TYPE_CHOICES, field_name='type')
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')
    name = django_filters.CharFilter(field_name="user__name", lookup_expr="icontains")
    description = django_filters.CharFilter(field_name="description", lookup_expr="icontains")

    class Meta:
        model = SysLog
        fields = ('create_time', 'type', 'name', 'description', )

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