<s> import pytz <EOL> from django . template . loader import render_to_string <EOL> from dimagi . utils . decorators . memoized import memoized <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . translation import ugettext_noop <EOL> from corehq . apps . reports . util import ( <EOL> DEFAULT_CSS_FIELD_CLASS_REPORT_FILTER , <EOL> DEFAULT_CSS_LABEL_CLASS_REPORT_FILTER , <EOL> ) <EOL> class BaseReportFilter ( object ) : <EOL> """<STR_LIT>""" <EOL> slug = None <EOL> template = None <EOL> label = None <EOL> css_class = "<STR_LIT>" <EOL> help_text = None <EOL> is_cacheable = False <EOL> help_style_bubble = False <EOL> def __init__ ( self , request , domain = None , timezone = pytz . utc , parent_report = None , <EOL> is_bootstrap3 = False , css_label = None , css_field = None ) : <EOL> if self . slug is None : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if self . template is None : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if self . label is None : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> self . request = request <EOL> self . domain = domain <EOL> self . timezone = timezone <EOL> self . parent_report = parent_report <EOL> self . is_bootstrap3 = is_bootstrap3 <EOL> self . css_label = css_label or DEFAULT_CSS_LABEL_CLASS_REPORT_FILTER <EOL> self . css_field = css_field or DEFAULT_CSS_FIELD_CLASS_REPORT_FILTER <EOL> self . context = { } <EOL> @ property <EOL> def is_disabled ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> @ property <EOL> def filter_context ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def render ( self ) : <EOL> if self . is_disabled : <EOL> return "<STR_LIT>" <EOL> self . context . update ( { <EOL> '<STR_LIT>' : self . slug , <EOL> '<STR_LIT:label>' : self . label , <EOL> '<STR_LIT>' : '<STR_LIT>' % self . slug , <EOL> '<STR_LIT>' : self . css_class , <EOL> '<STR_LIT>' : self . css_label , <EOL> '<STR_LIT>' : self . css_field , <EOL> '<STR_LIT>' : self . help_text , <EOL> '<STR_LIT>' : self . help_style_bubble , <EOL> } ) <EOL> filter_context = self . filter_context <EOL> if not ( filter_context , dict ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . context . update ( filter_context ) <EOL> return render_to_string ( self . get_bootstrap_template ( ) , self . context ) <EOL> @ classmethod <EOL> def get_value ( cls , request , domain ) : <EOL> return request . GET . get ( cls . slug ) <EOL> def get_bootstrap_template ( self ) : <EOL> if self . is_bootstrap3 : <EOL> return self . template . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return self . template <EOL> class CheckboxFilter ( BaseReportFilter ) : <EOL> slug = "<STR_LIT>" <EOL> label = "<STR_LIT:hello>" <EOL> template = "<STR_LIT>" <EOL> @ property <EOL> def filter_context ( self ) : <EOL> return { '<STR_LIT>' : self . request . GET . get ( self . slug , False ) } <EOL> @ classmethod <EOL> def get_value ( cls , request , domain ) : <EOL> val = request . GET . get ( cls . slug , False ) <EOL> if not val : <EOL> return False <EOL> else : <EOL> return val is True or val == '<STR_LIT:True>' or val == '<STR_LIT:true>' <EOL> class BaseSingleOptionFilter ( BaseReportFilter ) : <EOL> """<STR_LIT>""" <EOL> template = "<STR_LIT>" <EOL> default_text = ugettext_noop ( "<STR_LIT>" ) <EOL> placeholder = '<STR_LIT>' <EOL> is_paginated = False <EOL> pagination_source = None <EOL> async_handler = None <EOL> async_action = None <EOL> @ property <EOL> def options ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ property <EOL> @ memoized <EOL> def selected ( self ) : <EOL> return self . get_value ( self . request , self . domain ) or "<STR_LIT>" <EOL> @ property <EOL> def filter_context ( self ) : <EOL> options = [ ] <EOL> if not self . is_paginated : <EOL> options = self . options <EOL> if not isinstance ( options , list ) and not isinstance ( options [ <NUM_LIT:0> ] , tuple ) and not len ( options [ <NUM_LIT:0> ] ) == <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> options = [ dict ( val = o [ <NUM_LIT:0> ] , text = o [ <NUM_LIT:1> ] ) for o in self . options ] <EOL> return { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : options , <EOL> '<STR_LIT>' : self . default_text , <EOL> '<STR_LIT>' : self . selected , <EOL> '<STR_LIT>' : self . placeholder , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . is_paginated , <EOL> '<STR_LIT:url>' : self . pagination_source , <EOL> '<STR_LIT>' : self . async_handler . slug if self . async_handler else '<STR_LIT>' , <EOL> '<STR_LIT:action>' : self . async_action , <EOL> } , <EOL> } <EOL> @ classmethod <EOL> def get_value ( cls , request , domain ) : <EOL> value = super ( BaseSingleOptionFilter , cls ) . get_value ( request , domain ) <EOL> if cls . is_paginated : <EOL> return value if value else None <EOL> if isinstance ( cls , cls ) : <EOL> instance = cls <EOL> else : <EOL> instance = cls ( request , domain ) <EOL> valid_options = [ op [ <NUM_LIT:0> ] for op in instance . options ] <EOL> if value in valid_options : <EOL> return value <EOL> return None <EOL> class BaseMultipleOptionFilter ( BaseSingleOptionFilter ) : <EOL> """<STR_LIT>""" <EOL> template = "<STR_LIT>" <EOL> default_options = [ ] <EOL> @ classmethod <EOL> def get_value ( cls , request , domain ) : <EOL> return request . GET . getlist ( cls . slug ) <EOL> @ property <EOL> @ memoized <EOL> def selected ( self ) : <EOL> return self . get_value ( self . request , self . domain ) or self . default_options <EOL> class BaseDrilldownOptionFilter ( BaseReportFilter ) : <EOL> """<STR_LIT>""" <EOL> template = "<STR_LIT>" <EOL> use_only_last = False <EOL> drilldown_empty_text = ugettext_noop ( "<STR_LIT>" ) <EOL> is_cacheable = True <EOL> @ property <EOL> def drilldown_map ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def get_labels ( cls ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ property <EOL> def selected ( self ) : <EOL> selected = [ ] <EOL> for label in self . rendered_labels : <EOL> value = self . _get_label_value ( self . request , label ) <EOL> if not value [ '<STR_LIT:value>' ] : <EOL> break <EOL> selected . append ( value [ '<STR_LIT:value>' ] ) <EOL> return selected <EOL> @ property <EOL> def rendered_labels ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_labels ( ) <EOL> @ property <EOL> def filter_context ( self ) : <EOL> controls = [ ] <EOL> for level , label in enumerate ( self . rendered_labels ) : <EOL> controls . append ( { <EOL> '<STR_LIT:label>' : label [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : label [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : label [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : level , <EOL> } ) <EOL> drilldown_map = list ( self . drilldown_map ) <EOL> return { <EOL> '<STR_LIT>' : drilldown_map , <EOL> '<STR_LIT>' : controls , <EOL> '<STR_LIT>' : self . selected , <EOL> '<STR_LIT>' : self . use_only_last , <EOL> '<STR_LIT>' : self . final_notifications , <EOL> '<STR_LIT>' : self . drilldown_empty_text , <EOL> '<STR_LIT>' : not drilldown_map , <EOL> } <EOL> @ property <EOL> def final_notifications ( self ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> @ property <EOL> @ memoized <EOL> def GET_values ( self ) : <EOL> values = [ ] <EOL> for label in self . rendered_labels : <EOL> value = self . _get_label_value ( self . request , label ) <EOL> if not value [ '<STR_LIT:value>' ] : <EOL> break <EOL> values . append ( value ) <EOL> return values <EOL> def _map_structure ( self , val , text , next = None ) : <EOL> if next is None : <EOL> next = [ ] <EOL> return { <EOL> '<STR_LIT>' : val , <EOL> '<STR_LIT:text>' : text , <EOL> '<STR_LIT>' : next , <EOL> } <EOL> @ property <EOL> def shared_pagination_GET_params ( self ) : <EOL> return [ dict ( name = '<STR_LIT>' % ( self . slug , val [ '<STR_LIT>' ] ) , value = val [ '<STR_LIT:value>' ] ) for val in self . GET_values ] <EOL> @ classmethod <EOL> def _get_label_value ( cls , request , label ) : <EOL> slug = str ( label [ <NUM_LIT:2> ] ) <EOL> val = request . GET . get ( '<STR_LIT>' % ( cls . slug , slug ) ) <EOL> return { <EOL> '<STR_LIT>' : slug , <EOL> '<STR_LIT:value>' : val , <EOL> } <EOL> @ classmethod <EOL> def get_value ( cls , request , domain ) : <EOL> instance = cls ( request , domain ) <EOL> return instance . GET_values , instance <EOL> class BaseTagsFilter ( BaseReportFilter ) : <EOL> template = "<STR_LIT>" <EOL> tags = [ ] <EOL> @ property <EOL> def selected ( self ) : <EOL> return self . get_value ( self . request , self . domain ) or '<STR_LIT>' <EOL> @ property <EOL> def filter_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . tags , <EOL> '<STR_LIT>' : self . selected , <EOL> '<STR_LIT>' : self . placeholder , <EOL> } </s>
