<s> from __future__ import division , absolute_import , unicode_literals <EOL> import collections <EOL> from itertools import chain <EOL> from django import forms <EOL> from django . forms . util import flatatt <EOL> from django . utils . html import format_html <EOL> from django . utils . encoding import force_text , force_bytes <EOL> from django . utils . safestring import mark_safe <EOL> from django . forms import widgets as django_widgets <EOL> from django . utils import six <EOL> from django . utils . translation import ugettext_lazy <EOL> import django_filters <EOL> from . utils import type_str <EOL> LINK_TEMPLATE = '<STR_LIT>' <EOL> class NumericDateFilter ( django_filters . DateFilter ) : <EOL> field_class = forms . IntegerField <EOL> class ChoicesAsLinksWidget ( django_widgets . Select ) : <EOL> """<STR_LIT>""" <EOL> def render ( self , name , value , attrs = None , choices = ( ) ) : <EOL> links = [ ] <EOL> for choice_value , choice_label in chain ( self . choices , choices ) : <EOL> links . append ( format_html ( <EOL> LINK_TEMPLATE , <EOL> name , choice_value , flatatt ( attrs ) , force_text ( choice_label ) , <EOL> ) ) <EOL> return mark_safe ( u"<STR_LIT>" . join ( links ) ) <EOL> class NullBooleanLinksWidget ( <EOL> ChoicesAsLinksWidget , <EOL> django_widgets . NullBooleanSelect <EOL> ) : <EOL> def __init__ ( self , attrs = None , choices = ( ) ) : <EOL> super ( ChoicesAsLinksWidget , self ) . __init__ ( attrs ) <EOL> self . choices = [ <EOL> ( '<STR_LIT:1>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:2>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:3>' , ugettext_lazy ( '<STR_LIT>' ) ) , <EOL> ] <EOL> FILTER_TYPE_TO_WIDGET = { <EOL> django_filters . BooleanFilter : NullBooleanLinksWidget , <EOL> django_filters . ChoiceFilter : ChoicesAsLinksWidget , <EOL> django_filters . ModelChoiceFilter : ChoicesAsLinksWidget , <EOL> } <EOL> def build_list_filter ( request , model_admin , queryset ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( model_admin . list_filter , collections . Iterable ) : <EOL> return model_admin . list_filter ( <EOL> request . GET , <EOL> queryset = queryset , <EOL> ) <EOL> filters = [ ] <EOL> for field_filter in model_admin . list_filter : <EOL> if isinstance ( field_filter , six . string_types ) : <EOL> filters . append ( get_filter_for_field_name ( <EOL> queryset . model , <EOL> field_filter , <EOL> ) ) <EOL> else : <EOL> filters . append ( field_filter ) <EOL> filterset_dict = { } <EOL> for field_filter in filters : <EOL> filterset_dict [ field_filter . name ] = field_filter <EOL> fields = list ( filterset_dict . keys ( ) ) <EOL> filterset_dict [ '<STR_LIT:Meta>' ] = type ( <EOL> type_str ( '<STR_LIT:Meta>' ) , <EOL> ( ) , <EOL> { <EOL> '<STR_LIT>' : queryset . model , <EOL> '<STR_LIT>' : fields , <EOL> } , <EOL> ) <EOL> return type ( type_str ( '<STR_LIT>' % queryset . model . __name__ ) , ( django_filters . FilterSet , ) , filterset_dict , ) ( request . GET , queryset = queryset ) <EOL> def build_date_filter ( request , model_admin , queryset ) : <EOL> filterset_dict = { <EOL> "<STR_LIT>" : NumericDateFilter ( <EOL> name = "<STR_LIT>" , <EOL> lookup_type = "<STR_LIT>" , <EOL> ) , <EOL> "<STR_LIT>" : NumericDateFilter ( <EOL> name = "<STR_LIT>" , <EOL> lookup_type = "<STR_LIT>" , <EOL> ) , <EOL> "<STR_LIT>" : NumericDateFilter ( <EOL> name = "<STR_LIT>" , <EOL> lookup_type = "<STR_LIT>" , <EOL> ) <EOL> } <EOL> return type ( <EOL> type_str ( '<STR_LIT>' % queryset . model . __name__ ) , <EOL> ( django_filters . FilterSet , ) , <EOL> filterset_dict , <EOL> ) ( request . GET , queryset = queryset ) <EOL> def get_filter_for_field_name ( model , field_name ) : <EOL> """<STR_LIT>""" <EOL> filter_ = django_filters . FilterSet . filter_for_field ( <EOL> django_filters . filterset . get_model_field ( model , field_name , ) , <EOL> field_name , <EOL> ) <EOL> filter_ . widget = FILTER_TYPE_TO_WIDGET . get ( <EOL> filter_ . __class__ , <EOL> filter_ . widget , <EOL> ) <EOL> return filter_ </s>
