def ensure_plans(config, verbose, apps): DefaultProductPlan = apps.get_model('accounting', 'DefaultProductPlan') SoftwarePlan = apps.get_model('accounting', 'SoftwarePlan') SoftwarePlanVersion = apps.get_model('accounting', 'SoftwarePlanVersion') Role = apps.get_model('django_prbac', 'Role') for plan_key, plan_deets in config.items(): edition, is_trial, is_report_builder_enabled = plan_key features = _ensure_features(edition, verbose, apps) try: role = _ensure_role(plan_deets['role'], apps) except Role.DoesNotExist: return product, product_rate = _ensure_product_rate( plan_deets['product_rate_monthly_fee'], edition, verbose=verbose, apps=apps, ) feature_rates = _ensure_feature_rates( plan_deets['feature_rates'], features, edition, verbose=verbose, apps=apps, ) software_plan = SoftwarePlan( name=((('%s Trial' % product_rate.name) if is_trial else ('%s Edition' % product_rate.name)) if product is None else product.name # TODO - remove after squashing migrations ), edition=edition, visibility=SoftwarePlanVisibility.PUBLIC) if is_report_builder_enabled: software_plan.name = '%s - Report Builder (5 Reports)' % software_plan.name try: software_plan = SoftwarePlan.objects.get(name=software_plan.name) if verbose: log_accounting_info( "Plan '%s' already exists. Using existing plan to add version." % software_plan.name) except SoftwarePlan.DoesNotExist: software_plan.save() if verbose: log_accounting_info("Creating Software Plan: %s" % software_plan.name) product_rate.save() software_plan_version = SoftwarePlanVersion(role=role, plan=software_plan, product_rate=product_rate) software_plan_version.save() for feature_rate in feature_rates: feature_rate.save() software_plan_version.feature_rates.add(feature_rate) software_plan_version.save() try: default_product_plan = DefaultProductPlan.objects.get( edition=edition, is_trial=is_trial, is_report_builder_enabled=is_report_builder_enabled, ) if verbose: log_accounting_info( "Default for edition '%s' with is_trial='%s' already exists." % (default_product_plan.edition, is_trial)) except DefaultProductPlan.DoesNotExist: default_product_plan = DefaultProductPlan( edition=edition, is_trial=is_trial, is_report_builder_enabled=is_report_builder_enabled, ) finally: default_product_plan.plan = software_plan default_product_plan.save() if verbose: log_accounting_info( "Setting plan as default for edition '%s' with is_trial='%s'." % (default_product_plan.edition, is_trial)) _clear_cache(SoftwarePlan.objects.all())
def ensure_plans(config, verbose, apps): DefaultProductPlan = apps.get_model('accounting', 'DefaultProductPlan') SoftwarePlan = apps.get_model('accounting', 'SoftwarePlan') SoftwarePlanVersion = apps.get_model('accounting', 'SoftwarePlanVersion') Role = apps.get_model('django_prbac', 'Role') for plan_key, plan_deets in six.iteritems(config): edition, is_trial, is_report_builder_enabled = plan_key features = _ensure_features(edition, verbose, apps) try: role = _ensure_role(plan_deets['role'], apps) except Role.DoesNotExist: return product, product_rate = _ensure_product_rate( plan_deets['product_rate_monthly_fee'], edition, verbose=verbose, apps=apps, ) feature_rates = _ensure_feature_rates( plan_deets['feature_rates'], features, edition, verbose=verbose, apps=apps, ) software_plan = SoftwarePlan( name=( (('%s Trial' % product_rate.name) if is_trial else ('%s Edition' % product_rate.name)) if product is None else product.name # TODO - remove after squashing migrations ), edition=edition, visibility=SoftwarePlanVisibility.PUBLIC ) if is_report_builder_enabled: software_plan.name = '%s - Report Builder (5 Reports)' % software_plan.name try: software_plan = SoftwarePlan.objects.get(name=software_plan.name) if verbose: log_accounting_info( "Plan '%s' already exists. Using existing plan to add version." % software_plan.name ) except SoftwarePlan.DoesNotExist: software_plan.save() if verbose: log_accounting_info("Creating Software Plan: %s" % software_plan.name) product_rate.save() software_plan_version = SoftwarePlanVersion(role=role, plan=software_plan, product_rate=product_rate) software_plan_version.save() for feature_rate in feature_rates: feature_rate.save() software_plan_version.feature_rates.add(feature_rate) software_plan_version.save() try: default_product_plan = DefaultProductPlan.objects.get( edition=edition, is_trial=is_trial, is_report_builder_enabled=is_report_builder_enabled, ) if verbose: log_accounting_info( "Default for edition '%s' with is_trial='%s' already exists." % (default_product_plan.edition, is_trial) ) except DefaultProductPlan.DoesNotExist: default_product_plan = DefaultProductPlan( edition=edition, is_trial=is_trial, is_report_builder_enabled=is_report_builder_enabled, ) finally: default_product_plan.plan = software_plan default_product_plan.save() if verbose: log_accounting_info( "Setting plan as default for edition '%s' with is_trial='%s'." % (default_product_plan.edition, is_trial) ) _clear_cache(SoftwarePlan.objects.all())