<s> import calendar <EOL> from datetime import datetime <EOL> from dateutil . relativedelta import relativedelta <EOL> from dateutil . rrule import * <EOL> from django . conf import settings <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . utils . translation import ugettext_noop <EOL> from corehq . apps . locations . models import SQLLocation , LocationType <EOL> from corehq . apps . locations . util import location_hierarchy_config , load_locs_json <EOL> from corehq . apps . products . models import SQLProduct <EOL> from corehq . apps . programs . models import Program <EOL> from corehq . apps . reports . filters . base import BaseSingleOptionFilter , BaseMultipleOptionFilter , BaseReportFilter , CheckboxFilter <EOL> from corehq . apps . reports . filters . fixtures import AsyncLocationFilter <EOL> from corehq . util import reverse <EOL> from custom . common import ALL_OPTION <EOL> from corehq . apps . domain . models import Domain <EOL> from custom . ewsghana . utils import ews_date_format , calculate_last_period <EOL> class ProductByProgramFilter ( BaseSingleOptionFilter ) : <EOL> slug = "<STR_LIT>" <EOL> label = ugettext_noop ( "<STR_LIT>" ) <EOL> default_text = '<STR_LIT>' <EOL> @ property <EOL> def options ( self ) : <EOL> options = [ ( ALL_OPTION , "<STR_LIT>" ) ] <EOL> for program in Program . by_domain ( self . domain ) : <EOL> options . append ( ( program . get_id , program . name ) ) <EOL> return options <EOL> class ViewReportFilter ( BaseSingleOptionFilter ) : <EOL> default_text = ugettext_noop ( "<STR_LIT>" ) <EOL> slug = '<STR_LIT>' <EOL> label = '<STR_LIT>' <EOL> @ property <EOL> def options ( self ) : <EOL> return [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> class ProductFilter ( BaseSingleOptionFilter ) : <EOL> slug = '<STR_LIT>' <EOL> label = '<STR_LIT>' <EOL> default_text = '<STR_LIT>' <EOL> @ property <EOL> def options ( self ) : <EOL> return SQLProduct . objects . filter ( domain = self . domain , is_archived = False ) . values_list ( '<STR_LIT>' , '<STR_LIT:name>' ) . order_by ( '<STR_LIT:name>' ) <EOL> class MultiProductFilter ( BaseMultipleOptionFilter ) : <EOL> slug = '<STR_LIT>' <EOL> label = '<STR_LIT>' <EOL> default_text = '<STR_LIT>' <EOL> @ property <EOL> def options ( self ) : <EOL> return SQLProduct . objects . filter ( domain = self . domain , is_archived = False ) . values_list ( '<STR_LIT>' , '<STR_LIT:name>' ) . order_by ( '<STR_LIT:name>' ) <EOL> class EWSRestrictionLocationFilter ( AsyncLocationFilter ) : <EOL> template = "<STR_LIT>" <EOL> only_administrative = False <EOL> @ property <EOL> def filter_context ( self ) : <EOL> api_root = reverse ( '<STR_LIT>' , <EOL> params = { '<STR_LIT>' : True } , <EOL> kwargs = { '<STR_LIT>' : self . domain , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> user = self . request . couch_user <EOL> loc_id = self . request . GET . get ( '<STR_LIT>' ) <EOL> if not loc_id : <EOL> domain_membership = user . get_domain_membership ( self . domain ) <EOL> if domain_membership : <EOL> loc_id = domain_membership . location_id <EOL> return { <EOL> '<STR_LIT>' : api_root , <EOL> '<STR_LIT>' : self . label , <EOL> '<STR_LIT>' : self . slug , <EOL> '<STR_LIT>' : loc_id , <EOL> '<STR_LIT>' : load_locs_json ( self . domain , loc_id , only_administrative = self . only_administrative ) , <EOL> '<STR_LIT>' : location_hierarchy_config ( self . domain ) <EOL> } <EOL> class EWSLocationFilter ( EWSRestrictionLocationFilter ) : <EOL> only_administrative = True <EOL> def reporting_types ( self ) : <EOL> return [ <EOL> location_type . name <EOL> for location_type in Domain . get_by_name ( self . domain ) . location_types <EOL> if not location_type . administrative <EOL> ] <EOL> @ property <EOL> def filter_context ( self ) : <EOL> from custom . ewsghana import ROOT_SITE_CODE <EOL> api_root = reverse ( '<STR_LIT>' , <EOL> params = { '<STR_LIT>' : False } , <EOL> kwargs = { '<STR_LIT>' : self . domain , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> user = self . request . couch_user <EOL> loc_id = self . request . GET . get ( '<STR_LIT>' ) <EOL> if not loc_id : <EOL> domain_membership = user . get_domain_membership ( self . domain ) <EOL> if not domain_membership or not domain_membership . location_id : <EOL> loc_id = SQLLocation . objects . get ( <EOL> domain = self . domain , <EOL> site_code = ROOT_SITE_CODE <EOL> ) . location_id <EOL> else : <EOL> loc_id = domain_membership . location_id <EOL> location = get_object_or_404 ( SQLLocation , location_id = loc_id ) <EOL> if not location . location_type . administrative : <EOL> loc_id = location . parent . location_id <EOL> hier = location_hierarchy_config ( self . domain ) <EOL> hierarchy = [ ] <EOL> for h in hier : <EOL> if h [ <NUM_LIT:0> ] not in self . reporting_types ( ) : <EOL> hierarchy . append ( h ) <EOL> return { <EOL> '<STR_LIT>' : api_root , <EOL> '<STR_LIT>' : self . label , <EOL> '<STR_LIT>' : self . slug , <EOL> '<STR_LIT>' : loc_id , <EOL> '<STR_LIT>' : load_locs_json ( self . domain , loc_id , only_administrative = self . only_administrative ) , <EOL> '<STR_LIT>' : hierarchy <EOL> } <EOL> class EWSDateFilter ( BaseReportFilter ) : <EOL> template = "<STR_LIT>" <EOL> slug = "<STR_LIT>" <EOL> label = "<STR_LIT>" <EOL> def selected ( self , type ) : <EOL> slug = '<STR_LIT>' . format ( self . slug , type ) <EOL> return self . request . GET . get ( slug ) <EOL> @ property <EOL> def select_options ( self ) : <EOL> start_year = getattr ( settings , '<STR_LIT>' , <NUM_LIT> ) <EOL> years = [ dict ( val = unicode ( y ) , text = y ) for y in range ( start_year , datetime . utcnow ( ) . year + <NUM_LIT:1> ) ] <EOL> years . reverse ( ) <EOL> months = [ dict ( val = "<STR_LIT>" % m , text = calendar . month_name [ m ] ) for m in range ( <NUM_LIT:1> , <NUM_LIT> ) ] <EOL> now = datetime . now ( ) <EOL> three_month_earlier = now - relativedelta ( months = <NUM_LIT:3> ) <EOL> first_friday = rrule ( DAILY , <EOL> dtstart = datetime ( three_month_earlier . year , three_month_earlier . month , <NUM_LIT:1> ) , <EOL> until = now , <EOL> byweekday = FR ) [ <NUM_LIT:0> ] <EOL> if first_friday . day != <NUM_LIT:1> : <EOL> first_friday = first_friday - relativedelta ( days = <NUM_LIT:7> ) <EOL> fridays = rrule ( WEEKLY , dtstart = first_friday , until = now , byweekday = FR ) <EOL> weeks = [ ] <EOL> value = None <EOL> text = None <EOL> for idx , val in enumerate ( fridays ) : <EOL> try : <EOL> value = '<STR_LIT>' . format ( val . strftime ( "<STR_LIT>" ) , fridays [ idx + <NUM_LIT:1> ] . strftime ( "<STR_LIT>" ) ) <EOL> text = '<STR_LIT>' . format ( <EOL> ews_date_format ( val ) , <EOL> ews_date_format ( fridays [ idx + <NUM_LIT:1> ] - relativedelta ( days = <NUM_LIT:1> ) ) <EOL> ) <EOL> except IndexError : <EOL> next_thursday = val + relativedelta ( days = <NUM_LIT:6> ) <EOL> value = '<STR_LIT>' . format ( val . strftime ( "<STR_LIT>" ) , next_thursday . strftime ( "<STR_LIT>" ) ) <EOL> text = '<STR_LIT>' . format ( ews_date_format ( val ) , ews_date_format ( next_thursday ) ) <EOL> finally : <EOL> weeks . append ( dict ( val = value , text = text ) ) <EOL> return [ <EOL> { <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : weeks , <EOL> '<STR_LIT>' : [ ] <EOL> } , <EOL> { <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : months , <EOL> '<STR_LIT>' : years <EOL> } <EOL> ] <EOL> @ staticmethod <EOL> def last_reporting_period ( ) : <EOL> return calculate_last_period ( ) <EOL> @ property <EOL> def default_week ( self ) : <EOL> start_date , end_date = self . last_reporting_period ( ) <EOL> return '<STR_LIT>' . format ( start_date . strftime ( "<STR_LIT>" ) , end_date . strftime ( "<STR_LIT>" ) ) <EOL> @ property <EOL> def filter_context ( self ) : <EOL> return dict ( <EOL> select_options = self . select_options , <EOL> selected_type = self . selected ( '<STR_LIT:type>' ) if self . selected ( '<STR_LIT:type>' ) else <NUM_LIT:2> , <EOL> selected_first = self . selected ( '<STR_LIT>' ) if self . selected ( '<STR_LIT>' ) else self . default_week , <EOL> selected_second = self . selected ( '<STR_LIT>' ) if self . selected ( '<STR_LIT>' ) else '<STR_LIT>' <EOL> ) <EOL> class LocationTypeFilter ( BaseMultipleOptionFilter ) : <EOL> slug = '<STR_LIT>' <EOL> label = "<STR_LIT>" <EOL> placeholder = '<STR_LIT>' <EOL> @ property <EOL> def options ( self ) : <EOL> return [ <EOL> ( unicode ( loc_type . pk ) , loc_type . name ) for loc_type in <EOL> LocationType . objects . filter ( <EOL> domain = self . domain , administrative = False <EOL> ) <EOL> ] <EOL> class TransactionCheckboxFilter ( CheckboxFilter ) : <EOL> label = '<STR_LIT>' <EOL> slug = '<STR_LIT>' </s>
