def cancel_advantage_subscriptions(**kwargs): api_url = kwargs.get("api_url") token = kwargs.get("token") account_id = kwargs.get("account_id") previous_purchase_id = kwargs.get("previous_purchase_id") product_listing_id = kwargs.get("product_listing_id") advantage = UAContractsAPI(session, token, api_url=api_url) monthly_subscriptions = advantage.get_account_subscriptions( account_id=account_id, marketplace="canonical-ua", filters={ "status": "active", "period": "monthly" }, ) if not monthly_subscriptions: return flask.jsonify({"error": "no monthly subscriptions found"}), 400 monthly_subscription = monthly_subscriptions[0] purchase_request = { "accountID": account_id, "purchaseItems": [{ "productListingID": product_listing_id, "metric": "active-machines", "value": 0, "delete": True, }], "previousPurchaseID": previous_purchase_id, } try: purchase = advantage.purchase_from_marketplace( marketplace="canonical-ua", purchase_request=purchase_request) except CannotCancelLastContractError: advantage.cancel_subscription( subscription_id=monthly_subscription["subscription"]["id"]) return ( flask.jsonify({"message": "Subscription Cancelled"}), 200, ) return flask.jsonify(purchase), 200
def post_advantage_subscriptions(preview, **kwargs): api_url = kwargs.get("api_url") token = kwargs.get("token") token_type = kwargs.get("token_type") account_id = kwargs.get("account_id") previous_purchase_id = kwargs.get("previous_purchase_id") period = kwargs.get("period") products = kwargs.get("products") resizing = kwargs.get("resizing", False) advantage = UAContractsAPI( session, token, token_type=token_type, api_url=api_url ) current_subscription = {} if user_info(flask.session): subscriptions = advantage.get_account_subscriptions( account_id=account_id, marketplace="canonical-ua", filters={"status": "active"}, ) for subscription in subscriptions: if subscription["subscription"]["period"] == period: current_subscription = subscription # If there is a subscription we get the current metric # value for each product listing so we can generate a # purchase request with updated quantities later. # If we resize we want to override the existing value subscribed_quantities = {} if not resizing and current_subscription: for item in current_subscription["purchasedProductListings"]: product_listing_id = item["productListing"]["id"] subscribed_quantities[product_listing_id] = item["value"] purchase_items = [] for product in products: product_listing_id = product["product_listing_id"] metric_value = product["quantity"] + subscribed_quantities.get( product_listing_id, 0 ) purchase_items.append( { "productListingID": product_listing_id, "metric": "active-machines", "value": metric_value, } ) purchase_request = { "accountID": account_id, "purchaseItems": purchase_items, "previousPurchaseID": previous_purchase_id, } try: if not preview: purchase = advantage.purchase_from_marketplace( marketplace="canonical-ua", purchase_request=purchase_request ) else: purchase = advantage.preview_purchase_from_marketplace( marketplace="canonical-ua", purchase_request=purchase_request ) except CannotCancelLastContractError as error: raise UAContractsAPIError(error) return flask.jsonify(purchase), 200