def post_auto_renewal_settings(**kwargs): api_url = kwargs.get("api_url") token = kwargs.get("token") should_auto_renew = kwargs.get("should_auto_renew", False) advantage = UAContractsAPI(session, token, api_url=api_url) accounts = advantage.get_accounts() for account in accounts: monthly_subscriptions = advantage.get_account_subscriptions( account_id=account["id"], marketplace="canonical-ua", filters={"status": "active", "period": "monthly"}, ) for subscription in monthly_subscriptions: advantage.post_subscription_auto_renewal( subscription_id=subscription["subscription"]["id"], should_auto_renew=should_auto_renew, ) return ( flask.jsonify({"message": "subscription renewal status was changed"}), 200, )
def advantage_view(**kwargs): is_test_backend = kwargs.get("test_backend") api_url = kwargs.get("api_url") stripe_publishable_key = kwargs["stripe_publishable_key"] token = kwargs.get("token") open_subscription = kwargs.get("subscription", None) personal_account = None new_subscription_id = None new_subscription_start_date = None pending_purchase_id = None enterprise_contracts = {} previous_purchase_ids = {"monthly": "", "yearly": ""} total_enterprise_contracts = 0 monthly_info = { "total_subscriptions": 0, "has_monthly": False, "next_payment": {}, } advantage = UAContractsAPI( session, token, api_url=api_url, is_for_view=True ) # Support admin "view as" functionality. email = kwargs.get("email", "").strip() accounts = advantage.get_accounts(email=email) for account in accounts: monthly_purchased_products = {} yearly_purchased_products = {} account["contracts"] = advantage.get_account_contracts(account["id"]) all_subscriptions = advantage.get_account_subscriptions( account_id=account["id"], marketplace="canonical-ua", ) monthly_subscriptions = [] yearly_subscriptions = [] for subscription in all_subscriptions: period = subscription["subscription"]["period"] status = subscription["subscription"]["status"] if status not in ["active", "locked"]: continue if subscription.get("pendingPurchases"): pending_purchase_id = subscription.get("pendingPurchases")[0] previous_purchase_ids[period] = subscription.get("lastPurchaseID") if subscription["subscription"]["period"] == "yearly": yearly_subscriptions.append(subscription) continue monthly_subscriptions.append(subscription) for subscription in monthly_subscriptions: purchased_products = subscription.get("purchasedProductListings") if purchased_products is None: continue for purchased_product_listing in purchased_products: product_listing = purchased_product_listing["productListing"] product_id = product_listing["productID"] quantity = purchased_product_listing["value"] monthly_purchased_products[product_id] = { "quantity": quantity, "price": product_listing["price"], "product_listing_id": product_listing["id"], } _prepare_monthly_info(monthly_info, subscription, advantage) for subscription in yearly_subscriptions: purchased_products = subscription.get("purchasedProductListings") if purchased_products is None: continue for purchased_product_listing in purchased_products: product_listing = purchased_product_listing["productListing"] product_id = product_listing["productID"] quantity = purchased_product_listing["value"] yearly_purchased_products[product_id] = { "quantity": quantity, "price": product_listing["price"], "product_listing_id": product_listing["id"], } for contract in account["contracts"]: contract_info = contract["contractInfo"] if not contract_info.get("items"): # TODO(frankban): clean up existing contracts with no items in # production. continue contract_id = contract_info["id"] contract["token"] = advantage.get_contract_token(contract_id) if contract_info.get("origin", "") == "free": personal_account = account personal_account["free_token"] = contract["token"] continue entitlements = {} for entitlement in contract_info["resourceEntitlements"]: contract["supportLevel"] = "-" if entitlement["type"] == "support": affordance = entitlement["affordances"] contract["supportLevel"] = affordance["supportLevel"] continue entitlement_type = entitlement["type"] entitlements[entitlement_type] = True contract["entitlements"] = entitlements created_at = parse(contract_info["createdAt"]) format_create = created_at.strftime("%d %B %Y") contract["contractInfo"]["createdAtFormatted"] = format_create contract["contractInfo"]["status"] = "active" time_now = datetime.utcnow().replace(tzinfo=pytz.utc) if ( not new_subscription_start_date or created_at > new_subscription_start_date ): new_subscription_start_date = created_at new_subscription_id = contract["contractInfo"]["id"] effective_to = parse(contract_info["effectiveTo"]) format_effective = effective_to.strftime("%d %B %Y") contract["contractInfo"]["effectiveToFormatted"] = format_effective if effective_to < time_now: contract["contractInfo"]["status"] = "expired" restart_date = time_now - timedelta(days=1) contract["contractInfo"]["expired_restart_date"] = restart_date date_difference = effective_to - time_now contract["expiring"] = date_difference.days <= 30 contract["contractInfo"]["daysTillExpiry"] = date_difference.days try: contract["renewal"] = _make_renewal( advantage, contract["contractInfo"] ) except KeyError: flask.current_app.extensions["sentry"].captureException() contract["renewal"] = None enterprise_contract = enterprise_contracts.setdefault( contract["accountInfo"]["name"], [] ) product_name = contract["contractInfo"]["products"][0] contract["productID"] = product_name contract["is_detached"] = False contract["machineCount"] = 0 contract["rowMachineCount"] = 0 allowances = contract_info.get("allowances") if ( allowances and len(allowances) > 0 and allowances[0]["metric"] == "units" ): contract["rowMachineCount"] = allowances[0]["value"] if product_name in yearly_purchased_products: purchased_product = yearly_purchased_products[product_name] contract["price_per_unit"] = purchased_product["price"] contract["machineCount"] = purchased_product["quantity"] contract["product_listing_id"] = purchased_product[ "product_listing_id" ] contract["period"] = "yearly" if contract["contractInfo"]["id"] == open_subscription: enterprise_contract.insert(0, contract) elif contract["contractInfo"]["id"] == new_subscription_id: enterprise_contract.insert(0, contract) else: enterprise_contract.append(contract) if contract["contractInfo"]["status"] != "expired": total_enterprise_contracts += 1 if product_name in monthly_purchased_products: contract = contract.copy() purchased_product = monthly_purchased_products[product_name] contract["price_per_unit"] = purchased_product["price"] contract["machineCount"] = purchased_product["quantity"] contract["is_cancelable"] = True contract["product_listing_id"] = purchased_product[ "product_listing_id" ] contract["period"] = "monthly" if contract["contractInfo"]["id"] == open_subscription: enterprise_contract.insert(0, contract) elif contract["contractInfo"]["id"] == new_subscription_id: enterprise_contract.insert(0, contract) else: enterprise_contract.append(contract) if contract["contractInfo"]["status"] != "expired": total_enterprise_contracts += 1 if ( product_name not in yearly_purchased_products and product_name not in monthly_purchased_products ): contract["is_detached"] = True if contract["contractInfo"]["id"] == open_subscription: enterprise_contract.insert(0, contract) elif contract["contractInfo"]["id"] == new_subscription_id: enterprise_contract.insert(0, contract) else: enterprise_contract.append(contract) if contract["contractInfo"]["status"] != "expired": total_enterprise_contracts += 1 return flask.render_template( "advantage/index.html", accounts=accounts, monthly_information=monthly_info, total_enterprise_contracts=total_enterprise_contracts, pending_purchase_id=pending_purchase_id, enterprise_contracts=enterprise_contracts, previous_purchase_ids=previous_purchase_ids, personal_account=personal_account, open_subscription=open_subscription, new_subscription_id=new_subscription_id, stripe_publishable_key=stripe_publishable_key, is_test_backend=is_test_backend, )