<s> import json <EOL> from django . db . models import Q <EOL> from corehq . apps . accounting . models import Feature , SoftwareProduct , BillingAccount , SoftwarePlanVersion , Subscription , Subscriber , BillingContactInfo , SoftwarePlan <EOL> from corehq . apps . accounting . utils import fmt_feature_rate_dict , fmt_product_rate_dict <EOL> from corehq . apps . domain . models import Domain <EOL> from corehq . apps . hqwebapp . async_handler import BaseAsyncHandler , AsyncHandlerError <EOL> from corehq . apps . hqwebapp . encoders import LazyEncoder <EOL> from corehq . apps . users . models import WebUser <EOL> class BaseRateAsyncHandler ( BaseAsyncHandler ) : <EOL> """<STR_LIT>""" <EOL> allowed_actions = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> @ property <EOL> def name ( self ) : <EOL> return self . data . get ( '<STR_LIT:name>' ) <EOL> @ property <EOL> def rate_type ( self ) : <EOL> return self . data . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def rate_id ( self ) : <EOL> return self . data . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def create_response ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ property <EOL> def apply_response ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> class FeatureRateAsyncHandler ( BaseRateAsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> @ property <EOL> def create_response ( self ) : <EOL> if Feature . objects . filter ( name = self . name ) . count ( ) > <NUM_LIT:0> : <EOL> raise AsyncHandlerError ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . name ) <EOL> new_feature , _ = Feature . objects . get_or_create ( <EOL> name = self . name , <EOL> feature_type = self . rate_type , <EOL> ) <EOL> return fmt_feature_rate_dict ( new_feature ) <EOL> @ property <EOL> def apply_response ( self ) : <EOL> try : <EOL> feature = Feature . objects . get ( id = self . rate_id ) <EOL> return fmt_feature_rate_dict ( feature ) <EOL> except Feature . DoesNotExist : <EOL> raise AsyncHandlerError ( "<STR_LIT>" ) <EOL> class SoftwareProductRateAsyncHandler ( BaseRateAsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> @ property <EOL> def create_response ( self ) : <EOL> if SoftwareProduct . objects . filter ( name = self . name ) . count ( ) > <NUM_LIT:0> : <EOL> raise AsyncHandlerError ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . name ) <EOL> new_product , _ = SoftwareProduct . objects . get_or_create ( <EOL> name = self . name , <EOL> product_type = self . rate_type <EOL> ) <EOL> return fmt_product_rate_dict ( new_product ) <EOL> @ property <EOL> def apply_response ( self ) : <EOL> try : <EOL> product = SoftwareProduct . objects . get ( id = self . rate_id ) <EOL> return fmt_product_rate_dict ( product ) <EOL> except SoftwareProduct . DoesNotExist : <EOL> raise AsyncHandlerError ( "<STR_LIT>" ) <EOL> class BaseSelect2AsyncHandler ( BaseAsyncHandler ) : <EOL> @ property <EOL> def search_string ( self ) : <EOL> return self . data . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def existing ( self ) : <EOL> return self . data . getlist ( '<STR_LIT>' ) <EOL> def _fmt_success ( self , response ) : <EOL> success = json . dumps ( { <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:id>' : r [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:text>' : r [ <NUM_LIT:1> ] , <EOL> } for r in response ] <EOL> } , cls = LazyEncoder ) <EOL> return success <EOL> class Select2RateAsyncHandler ( BaseSelect2AsyncHandler ) : <EOL> """<STR_LIT>""" <EOL> slug = '<STR_LIT>' <EOL> allowed_actions = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> @ property <EOL> def feature_id_response ( self ) : <EOL> features = Feature . objects <EOL> if self . existing : <EOL> features = features . exclude ( name__in = self . existing ) <EOL> if self . search_string : <EOL> features = features . filter ( name__startswith = self . search_string ) <EOL> return [ ( f . id , f . name , f . feature_type ) for f in features . all ( ) ] <EOL> @ property <EOL> def product_id_response ( self ) : <EOL> products = SoftwareProduct . objects <EOL> if self . existing : <EOL> products = products . exclude ( name__in = self . existing ) <EOL> if self . search_string : <EOL> products = products . filter ( name__startswith = self . search_string ) <EOL> return [ ( p . id , p . name , p . product_type ) for p in products . all ( ) ] <EOL> def _fmt_success ( self , response ) : <EOL> return json . dumps ( { <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT:id>' : r [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:name>' : r [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : r [ <NUM_LIT:2> ] , <EOL> '<STR_LIT:text>' : '<STR_LIT>' % ( r [ <NUM_LIT:1> ] , r [ <NUM_LIT:2> ] ) , <EOL> '<STR_LIT>' : True , <EOL> } for r in response ] <EOL> } ) <EOL> class Select2BillingInfoHandler ( BaseSelect2AsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> allowed_actions = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> @ property <EOL> def country_response ( self ) : <EOL> from django_countries . data import COUNTRIES <EOL> countries = sorted ( COUNTRIES . items ( ) , key = lambda x : x [ <NUM_LIT:1> ] . encode ( '<STR_LIT:utf-8>' ) ) <EOL> if self . search_string : <EOL> return filter ( lambda x : x [ <NUM_LIT:1> ] . lower ( ) . startswith ( self . search_string . lower ( ) ) , countries ) <EOL> return countries <EOL> @ property <EOL> def active_accounts_response ( self ) : <EOL> accounts = BillingAccount . objects . filter ( is_active = True ) <EOL> if self . search_string : <EOL> accounts = accounts . filter ( name__contains = self . search_string ) <EOL> return [ ( a . id , a . name ) for a in accounts ] <EOL> @ property <EOL> def domain_response ( self ) : <EOL> domain_names = [ domain [ '<STR_LIT:key>' ] for domain in Domain . get_all ( include_docs = False ) ] <EOL> if self . search_string : <EOL> domain_names = filter ( lambda x : x . lower ( ) . startswith ( self . search_string . lower ( ) ) , domain_names ) <EOL> return [ ( name , name ) for name in domain_names ] <EOL> @ property <EOL> def account_response ( self ) : <EOL> accounts = BillingAccount . objects <EOL> if self . search_string : <EOL> accounts = accounts . filter ( name__contains = self . search_string ) <EOL> return [ ( a . id , a . name ) for a in accounts . order_by ( '<STR_LIT:name>' ) ] <EOL> @ property <EOL> def plan_version_response ( self ) : <EOL> edition = self . data . get ( '<STR_LIT>' ) <EOL> product = self . data . get ( '<STR_LIT>' ) <EOL> plan_versions = SoftwarePlanVersion . objects . filter ( <EOL> plan__edition = edition <EOL> ) . filter ( product_rate__product__product_type = product ) <EOL> if self . search_string : <EOL> plan_versions = plan_versions . filter ( <EOL> plan__name__contains = self . search_string ) <EOL> return [ ( p . id , p . __str__ ( ) ) for p in plan_versions . order_by ( '<STR_LIT>' ) ] <EOL> @ property <EOL> def new_plan_version_response ( self ) : <EOL> current_version = int ( self . data . get ( '<STR_LIT>' ) ) <EOL> plan_versions = filter ( lambda x : x [ <NUM_LIT:0> ] != current_version , <EOL> self . plan_version_response ) <EOL> return plan_versions <EOL> class Select2InvoiceTriggerHandler ( BaseSelect2AsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> allowed_actions = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> @ property <EOL> def domain_response ( self ) : <EOL> domain_names = [ domain [ '<STR_LIT:key>' ] for domain in Domain . get_all ( include_docs = False ) ] <EOL> if self . search_string : <EOL> domain_names = filter ( lambda x : x . lower ( ) . startswith ( self . search_string . lower ( ) ) , domain_names ) <EOL> return [ ( d , d ) for d in domain_names ] <EOL> class BaseSingleOptionFilterAsyncHandler ( BaseAsyncHandler ) : <EOL> @ property <EOL> def query ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ property <EOL> def search_string ( self ) : <EOL> return self . data . get ( '<STR_LIT:q>' , None ) <EOL> @ property <EOL> def page ( self ) : <EOL> return int ( self . data . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> @ property <EOL> def paginated_data ( self ) : <EOL> start = ( self . page - <NUM_LIT:1> ) * self . limit <EOL> end = self . page * self . limit <EOL> return self . query . all ( ) [ start : end ] <EOL> @ property <EOL> def limit ( self ) : <EOL> return self . data . get ( '<STR_LIT>' , <NUM_LIT:10> ) <EOL> @ property <EOL> def total ( self ) : <EOL> return self . query . count ( ) <EOL> @ staticmethod <EOL> def _fmt_select2_data ( data_id , data_text ) : <EOL> return { <EOL> '<STR_LIT:id>' : data_id , <EOL> '<STR_LIT:text>' : data_text , <EOL> } <EOL> def _fmt_success ( self , data ) : <EOL> return json . dumps ( { <EOL> '<STR_LIT:success>' : True , <EOL> '<STR_LIT>' : self . limit , <EOL> '<STR_LIT>' : self . page , <EOL> '<STR_LIT>' : self . total , <EOL> '<STR_LIT>' : data , <EOL> } ) <EOL> class SubscriberFilterAsyncHandler ( BaseSingleOptionFilterAsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> allowed_actions = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> @ property <EOL> def query ( self ) : <EOL> query = Subscriber . objects . exclude ( domain = None ) . order_by ( '<STR_LIT>' ) <EOL> if self . search_string : <EOL> query = query . filter ( domain__istartswith = self . search_string ) <EOL> return query <EOL> @ property <EOL> def subscriber_response ( self ) : <EOL> return [ self . _fmt_select2_data ( s . domain , s . domain ) <EOL> for s in self . paginated_data ] <EOL> class SubscriptionFilterAsyncHandler ( BaseSingleOptionFilterAsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> allowed_actions = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> @ property <EOL> def query ( self ) : <EOL> query = Subscription . objects <EOL> if self . action == '<STR_LIT>' : <EOL> query = query . exclude ( <EOL> salesforce_contract_id = None <EOL> ) . exclude ( salesforce_contract_id = '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) <EOL> if self . search_string : <EOL> query = query . filter ( <EOL> salesforce_contract_id__istartswith = self . search_string <EOL> ) <EOL> return query <EOL> @ property <EOL> def contract_id_response ( self ) : <EOL> return [ self . _fmt_select2_data ( <EOL> s . salesforce_contract_id , s . salesforce_contract_id ) <EOL> for s in self . paginated_data ] <EOL> class AccountFilterAsyncHandler ( BaseSingleOptionFilterAsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> allowed_actions = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> @ property <EOL> def query ( self ) : <EOL> query = BillingAccount . objects . order_by ( '<STR_LIT:name>' ) <EOL> if self . action == '<STR_LIT>' and self . search_string : <EOL> query = query . filter ( name__icontains = self . search_string ) <EOL> if self . action == '<STR_LIT>' : <EOL> query = query . exclude ( <EOL> salesforce_account_id = None <EOL> ) . exclude ( <EOL> salesforce_account_id = '<STR_LIT>' <EOL> ) . order_by ( '<STR_LIT>' ) <EOL> if self . search_string : <EOL> query = query . filter ( <EOL> salesforce_account_id__istartswith = self . search_string ) <EOL> if self . action == '<STR_LIT>' : <EOL> query = query . exclude ( <EOL> dimagi_contact = None <EOL> ) . exclude ( <EOL> dimagi_contact = '<STR_LIT>' <EOL> ) . order_by ( '<STR_LIT>' ) <EOL> if self . search_string : <EOL> query = query . filter ( <EOL> dimagi_contact__icontains = self . search_string ) <EOL> return query <EOL> @ property <EOL> def account_name_response ( self ) : <EOL> return [ self . _fmt_select2_data ( a . name , a . name ) <EOL> for a in self . paginated_data ] <EOL> @ property <EOL> def account_id_response ( self ) : <EOL> return [ self . _fmt_select2_data ( a . salesforce_account_id , <EOL> a . salesforce_account_id ) <EOL> for a in self . paginated_data ] <EOL> @ property <EOL> def dimagi_contact_response ( self ) : <EOL> return [ self . _fmt_select2_data ( a . dimagi_contact , a . dimagi_contact ) <EOL> for a in self . paginated_data ] <EOL> class BillingContactInfoAsyncHandler ( BaseSingleOptionFilterAsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> allowed_actions = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> @ property <EOL> def query ( self ) : <EOL> query = BillingContactInfo . objects . exclude ( <EOL> first_name = '<STR_LIT>' , last_name = '<STR_LIT>' ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . search_string : <EOL> query = query . filter ( <EOL> Q ( first_name__istartswith = self . search_string ) | <EOL> Q ( last_name__istartswith = self . search_string ) <EOL> ) <EOL> return query <EOL> @ property <EOL> def contact_name_response ( self ) : <EOL> return [ self . _fmt_select2_data ( c . full_name , c . full_name ) <EOL> for c in self . paginated_data ] <EOL> class SoftwarePlanAsyncHandler ( BaseSingleOptionFilterAsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> allowed_actions = [ <EOL> '<STR_LIT:name>' , <EOL> ] <EOL> @ property <EOL> def query ( self ) : <EOL> query = SoftwarePlan . objects . order_by ( '<STR_LIT:name>' ) <EOL> if self . search_string : <EOL> query = query . filter ( name__icontains = self . search_string ) <EOL> return query <EOL> @ property <EOL> def name_response ( self ) : <EOL> return [ self . _fmt_select2_data ( p . name , p . name ) <EOL> for p in self . paginated_data ] <EOL> class DomainFilterAsyncHandler ( BaseSingleOptionFilterAsyncHandler ) : <EOL> slug = '<STR_LIT>' <EOL> allowed_actions = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> @ property <EOL> def query ( self ) : <EOL> db = Domain . get_db ( ) <EOL> startkey = self . search_string <EOL> endkey = "<STR_LIT>" . format ( self . search_string ) if startkey else '<STR_LIT>' <EOL> query = db . view ( <EOL> '<STR_LIT>' , <EOL> reduce = False , <EOL> startkey = startkey , <EOL> endkey = endkey , <EOL> limit = <NUM_LIT:20> , <EOL> ) <EOL> return query <EOL> @ property <EOL> def domain_name_response ( self ) : <EOL> return [ self . _fmt_select2_data ( p [ '<STR_LIT:key>' ] , p [ '<STR_LIT:key>' ] ) for p in self . paginated_data ] </s>
