<s> from __future__ import absolute_import , print_function , unicode_literals <EOL> from collections import defaultdict <EOL> from decimal import Decimal <EOL> import logging <EOL> from optparse import make_option <EOL> from django . apps import apps as default_apps <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . management . base import BaseCommand <EOL> from corehq . apps . accounting . exceptions import AccountingError <EOL> from corehq . apps . accounting . models import ( <EOL> SoftwareProductType , SoftwarePlanEdition , SoftwarePlanVisibility , FeatureType , <EOL> ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> BOOTSTRAP_EDITION_TO_ROLE = { <EOL> SoftwarePlanEdition . MANAGED_HOSTING : '<STR_LIT>' , <EOL> SoftwarePlanEdition . RESELLER : '<STR_LIT>' , <EOL> } <EOL> EDITIONS = [ <EOL> SoftwarePlanEdition . MANAGED_HOSTING , <EOL> SoftwarePlanEdition . RESELLER , <EOL> ] <EOL> FEATURE_TYPES = [ f [ <NUM_LIT:0> ] for f in FeatureType . CHOICES ] <EOL> PRODUCT_TYPES = [ p [ <NUM_LIT:0> ] for p in SoftwareProductType . CHOICES ] <EOL> class Command ( BaseCommand ) : <EOL> help = '<STR_LIT>' '<STR_LIT>' <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> def handle ( self , dry_run = False , verbose = False , <EOL> flush = False , testing = False , * args , ** options ) : <EOL> logger . info ( '<STR_LIT>' ) <EOL> for_tests = testing <EOL> if for_tests : <EOL> logger . info ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not flush : <EOL> ensure_plans ( dry_run = dry_run , verbose = verbose , for_tests = for_tests , apps = default_apps ) <EOL> def ensure_plans ( dry_run , verbose , for_tests , apps ) : <EOL> SoftwarePlan = apps . get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> SoftwarePlanVersion = apps . get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Role = apps . get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> edition_to_features = _ensure_features ( dry_run = dry_run , verbose = verbose , apps = apps ) <EOL> advanced_role = Role . objects . get ( slug = '<STR_LIT>' ) <EOL> for product_type in PRODUCT_TYPES : <EOL> for edition in EDITIONS : <EOL> software_plan_version = SoftwarePlanVersion ( role = advanced_role ) <EOL> product , product_rates = _ensure_product_and_rate ( <EOL> product_type , edition , dry_run = dry_run , verbose = verbose , apps = apps <EOL> ) <EOL> feature_rates = _ensure_feature_rates ( <EOL> edition_to_features [ edition ] , edition , dry_run = dry_run , <EOL> verbose = verbose , for_tests = for_tests , apps = apps <EOL> ) <EOL> software_plan = SoftwarePlan ( <EOL> name = '<STR_LIT>' % product . name , edition = edition , <EOL> visibility = SoftwarePlanVisibility . INTERNAL <EOL> ) <EOL> if dry_run : <EOL> logger . info ( "<STR_LIT>" % software_plan . name ) <EOL> else : <EOL> try : <EOL> software_plan = SoftwarePlan . objects . get ( name = software_plan . name ) <EOL> if verbose : <EOL> logger . info ( "<STR_LIT>" <EOL> % software_plan . name ) <EOL> except SoftwarePlan . DoesNotExist : <EOL> software_plan . save ( ) <EOL> if verbose : <EOL> logger . info ( "<STR_LIT>" % software_plan . name ) <EOL> software_plan_version . plan = software_plan <EOL> if hasattr ( SoftwarePlanVersion , '<STR_LIT>' ) : <EOL> software_plan_version . save ( ) <EOL> for product_rate in product_rates : <EOL> product_rate . save ( ) <EOL> if hasattr ( SoftwarePlanVersion , '<STR_LIT>' ) : <EOL> software_plan_version . product_rates . add ( product_rate ) <EOL> elif hasattr ( SoftwarePlanVersion , '<STR_LIT>' ) : <EOL> assert len ( product_rates ) == <NUM_LIT:1> <EOL> software_plan_version . product_rate = product_rate <EOL> else : <EOL> raise AccountingError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if hasattr ( SoftwarePlanVersion , '<STR_LIT>' ) : <EOL> software_plan_version . save ( ) <EOL> for feature_rate in feature_rates : <EOL> feature_rate . save ( ) <EOL> software_plan_version . feature_rates . add ( feature_rate ) <EOL> software_plan_version . save ( ) <EOL> def _ensure_product_and_rate ( product_type , edition , dry_run , verbose , apps ) : <EOL> """<STR_LIT>""" <EOL> SoftwareProduct = apps . get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> SoftwareProductRate = apps . get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if verbose : <EOL> logger . info ( '<STR_LIT>' ) <EOL> product = SoftwareProduct ( name = '<STR_LIT>' % ( product_type , edition ) , product_type = product_type ) <EOL> product_rates = [ ] <EOL> BOOTSTRAP_PRODUCT_RATES = { <EOL> SoftwarePlanEdition . RESELLER : [ <EOL> SoftwareProductRate ( monthly_fee = Decimal ( '<STR_LIT>' ) ) , <EOL> ] , <EOL> SoftwarePlanEdition . MANAGED_HOSTING : [ <EOL> SoftwareProductRate ( monthly_fee = Decimal ( '<STR_LIT>' ) ) , <EOL> ] , <EOL> } <EOL> for product_rate in BOOTSTRAP_PRODUCT_RATES [ edition ] : <EOL> if dry_run : <EOL> logger . info ( "<STR_LIT>" % product ) <EOL> logger . info ( "<STR_LIT>" % product_rate . monthly_fee ) <EOL> else : <EOL> try : <EOL> product = SoftwareProduct . objects . get ( name = product . name ) <EOL> if verbose : <EOL> logger . info ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % product . name ) <EOL> except SoftwareProduct . DoesNotExist : <EOL> product . save ( ) <EOL> if verbose : <EOL> logger . info ( "<STR_LIT>" % product ) <EOL> if verbose : <EOL> logger . info ( "<STR_LIT>" <EOL> % product_rate . monthly_fee ) <EOL> product_rate . product = product <EOL> product_rates . append ( product_rate ) <EOL> return product , product_rates <EOL> def _ensure_features ( dry_run , verbose , apps ) : <EOL> """<STR_LIT>""" <EOL> Feature = apps . get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if verbose : <EOL> logger . info ( '<STR_LIT>' ) <EOL> edition_to_features = defaultdict ( list ) <EOL> for edition in EDITIONS : <EOL> for feature_type in FEATURE_TYPES : <EOL> feature = Feature ( name = '<STR_LIT>' % ( feature_type , edition ) , feature_type = feature_type ) <EOL> if edition == SoftwarePlanEdition . ENTERPRISE : <EOL> feature . name = "<STR_LIT>" % feature . name <EOL> if dry_run : <EOL> logger . info ( "<STR_LIT>" % feature ) <EOL> else : <EOL> try : <EOL> feature = Feature . objects . get ( name = feature . name ) <EOL> if verbose : <EOL> logger . info ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % feature . name ) <EOL> except ObjectDoesNotExist : <EOL> feature . save ( ) <EOL> if verbose : <EOL> logger . info ( "<STR_LIT>" % feature ) <EOL> edition_to_features [ edition ] . append ( feature ) <EOL> return edition_to_features <EOL> def _ensure_feature_rates ( features , edition , dry_run , verbose , for_tests , apps ) : <EOL> """<STR_LIT>""" <EOL> FeatureRate = apps . get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if verbose : <EOL> logger . info ( '<STR_LIT>' ) <EOL> feature_rates = [ ] <EOL> BOOTSTRAP_FEATURE_RATES = { <EOL> SoftwarePlanEdition . RESELLER : { <EOL> FeatureType . USER : FeatureRate ( monthly_limit = <NUM_LIT:2> if for_tests else <NUM_LIT:10> , <EOL> per_excess_fee = Decimal ( '<STR_LIT>' ) ) , <EOL> FeatureType . SMS : FeatureRate ( monthly_limit = <NUM_LIT:0> ) , <EOL> } , <EOL> SoftwarePlanEdition . MANAGED_HOSTING : { <EOL> FeatureType . USER : FeatureRate ( monthly_limit = <NUM_LIT:0> , <EOL> per_excess_fee = Decimal ( '<STR_LIT>' ) ) , <EOL> FeatureType . SMS : FeatureRate ( monthly_limit = <NUM_LIT:0> ) , <EOL> } , <EOL> } <EOL> for feature in features : <EOL> feature_rate = BOOTSTRAP_FEATURE_RATES [ edition ] [ feature . feature_type ] <EOL> feature_rate . feature = feature <EOL> if dry_run : <EOL> logger . info ( "<STR_LIT>" % ( feature . name , feature_rate ) ) <EOL> elif verbose : <EOL> logger . info ( "<STR_LIT>" % ( feature . name , feature_rate ) ) <EOL> feature_rates . append ( feature_rate ) <EOL> return feature_rates </s>
