<s> import six <EOL> from django . conf import settings <EOL> from django . db import models <EOL> from django . utils . text import capfirst <EOL> from django_filters import Filter , MultipleChoiceFilter <EOL> from django_filters . filterset import FilterSet , FilterSetMetaclass <EOL> from graphql_relay . node . node import from_global_id <EOL> from graphene . contrib . django . forms import ( GlobalIDFormField , <EOL> GlobalIDMultipleChoiceField ) <EOL> class GlobalIDFilter ( Filter ) : <EOL> field_class = GlobalIDFormField <EOL> def filter ( self , qs , value ) : <EOL> gid = from_global_id ( value ) <EOL> return super ( GlobalIDFilter , self ) . filter ( qs , gid . id ) <EOL> class GlobalIDMultipleChoiceFilter ( MultipleChoiceFilter ) : <EOL> field_class = GlobalIDMultipleChoiceField <EOL> def filter ( self , qs , value ) : <EOL> gids = [ from_global_id ( v ) . id for v in value ] <EOL> return super ( GlobalIDMultipleChoiceFilter , self ) . filter ( qs , gids ) <EOL> ORDER_BY_FIELD = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> GRAPHENE_FILTER_SET_OVERRIDES = { <EOL> models . AutoField : { <EOL> '<STR_LIT>' : GlobalIDFilter , <EOL> } , <EOL> models . OneToOneField : { <EOL> '<STR_LIT>' : GlobalIDFilter , <EOL> } , <EOL> models . ForeignKey : { <EOL> '<STR_LIT>' : GlobalIDFilter , <EOL> } , <EOL> models . ManyToManyField : { <EOL> '<STR_LIT>' : GlobalIDMultipleChoiceFilter , <EOL> } <EOL> } <EOL> class GrapheneFilterSetMetaclass ( FilterSetMetaclass ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> new_class = super ( GrapheneFilterSetMetaclass , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> for k , v in GRAPHENE_FILTER_SET_OVERRIDES . items ( ) : <EOL> new_class . filter_overrides . setdefault ( k , v ) <EOL> return new_class <EOL> class GrapheneFilterSetMixin ( object ) : <EOL> order_by_field = ORDER_BY_FIELD <EOL> @ classmethod <EOL> def filter_for_reverse_field ( cls , f , name ) : <EOL> """<STR_LIT>""" <EOL> rel = f . field . rel <EOL> default = { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT:label>' : capfirst ( rel . related_name ) <EOL> } <EOL> if rel . multiple : <EOL> return GlobalIDMultipleChoiceFilter ( ** default ) <EOL> else : <EOL> return GlobalIDFilter ( ** default ) <EOL> class GrapheneFilterSet ( six . with_metaclass ( GrapheneFilterSetMetaclass , GrapheneFilterSetMixin , FilterSet ) ) : <EOL> """<STR_LIT>""" <EOL> def setup_filterset ( filterset_class ) : <EOL> """<STR_LIT>""" <EOL> return type ( <EOL> '<STR_LIT>' . format ( filterset_class . __name__ ) , <EOL> ( six . with_metaclass ( GrapheneFilterSetMetaclass , GrapheneFilterSetMixin , filterset_class ) , ) , <EOL> { } , <EOL> ) <EOL> def custom_filterset_factory ( model , filterset_base_class = GrapheneFilterSet , <EOL> ** meta ) : <EOL> """<STR_LIT>""" <EOL> meta . update ( { <EOL> '<STR_LIT>' : model , <EOL> } ) <EOL> meta_class = type ( str ( '<STR_LIT:Meta>' ) , ( object , ) , meta ) <EOL> filterset = type ( <EOL> str ( '<STR_LIT>' % model . _meta . object_name ) , <EOL> ( filterset_base_class , ) , <EOL> { <EOL> '<STR_LIT:Meta>' : meta_class <EOL> } <EOL> ) <EOL> return filterset </s>
