From c044893ff96df801e298e8b5a8b9153d02ef89f3 Mon Sep 17 00:00:00 2001 From: yackob03 Date: Wed, 2 Oct 2013 01:40:11 -0400 Subject: [PATCH] Add some feedback about the number of private repositories used. --- data/model.py | 7 +++++++ endpoints/api.py | 15 +++++++++------ static/js/controllers.js | 25 +++++++++++++------------ static/partials/user-admin.html | 25 +++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/data/model.py b/data/model.py index 931809895..0ad1ddc5b 100644 --- a/data/model.py +++ b/data/model.py @@ -380,7 +380,14 @@ def delete_user_permission(username, namespace_name, repository_name): fetched[0].delete_instance() + def purge_repository(namespace_name, repository_name): fetched = Repository.get(Repository.name == repository_name, Repository.namespace == namespace_name) fetched.delete_instance(recursive=True, delete_nullable=True) + + +def get_private_repo_count(username): + joined = Repository.select().join(Visibility) + return joined.where(Repository.namespace == username, + Visibility.name == 'private').count() diff --git a/endpoints/api.py b/endpoints/api.py index 6f920807a..96e0ca01d 100644 --- a/endpoints/api.py +++ b/endpoints/api.py @@ -348,11 +348,12 @@ def delete_permissions(namespace, repository, username): abort(403) # Permission denied -def subscription_view(stripe_subscription): +def subscription_view(stripe_subscription, used_repos): return { - 'current_period_start': stripe_subscription.current_period_start, - 'current_period_end': stripe_subscription.current_period_end, + 'currentPeriodStart': stripe_subscription.current_period_start, + 'currentPeriodEnd': stripe_subscription.current_period_end, 'plan': stripe_subscription.plan.id, + 'usedPrivateRepos': used_repos, } @@ -367,6 +368,7 @@ def subscribe(): card = request_data['token'] user = current_user.db_user + private_repos = model.get_private_repo_count(user.username) if not user.stripe_id: # Create the customer and plan simultaneously @@ -374,7 +376,7 @@ def subscribe(): user.stripe_id = cus.id user.save() - resp = jsonify(subscription_view(cus.subscription)) + resp = jsonify(subscription_view(cus.subscription, private_repos)) resp.status_code = 201 return resp @@ -383,7 +385,7 @@ def subscribe(): cus = stripe.Customer.retrieve(user.stripe_id) cus.plan = plan cus.save() - return jsonify(subscription_view(cus.subscription)) + return jsonify(subscription_view(cus.subscription, private_repos)) @app.route('/api/user/plan', methods=['GET']) @@ -392,7 +394,8 @@ def get_subscription(): user = current_user.db_user if user.stripe_id: + private_repos = model.get_private_repo_count(user.username) cus = stripe.Customer.retrieve(user.stripe_id) - return jsonify(subscription_view(cus.subscription)) + return jsonify(subscription_view(cus.subscription, private_repos)) abort(404) diff --git a/static/js/controllers.js b/static/js/controllers.js index 9930dad0d..d2c6dcd73 100644 --- a/static/js/controllers.js +++ b/static/js/controllers.js @@ -447,14 +447,18 @@ function UserAdminCtrl($scope, Restangular) { $('.spin').spin(); - $scope.loading = true; - var getSubscription = Restangular.one('user/plan'); - getSubscription.get().then(function(sub) { - // User has a subscription + var subscribedToPlan = function(sub) { $scope.subscription = sub; - $scope.loading = false; - }, function() { - $scope.loading = false; + $scope.subscribedPlan = planDict[sub.plan]; + $scope.planUsagePercent = sub.usedPrivateRepos * 100 / $scope.subscribedPlan.privateRepos; + $scope.planLoading = false; + } + + $scope.planLoading = true; + var getSubscription = Restangular.one('user/plan'); + getSubscription.get().then(subscribedToPlan, function() { + // User has no subscription + $scope.planLoading = false; }); $scope.subscribe = function(planId) { @@ -468,12 +472,9 @@ function UserAdminCtrl($scope, Restangular) { console.log(subscriptionDetails); var createSubscriptionRequest = Restangular.one('user/plan'); - createSubscriptionRequest.customPUT(subscriptionDetails).then(function() { - // Success - console.log('successfully created subscription'); - }, function() { + createSubscriptionRequest.customPUT(subscriptionDetails).then(subscribedToPlan, function() { // Failure - console.log('failed to created subscription'); + $scope.errorMessage = 'Unable to process subscription change.'; }); }); }; diff --git a/static/partials/user-admin.html b/static/partials/user-admin.html index d93540136..fb70a249e 100644 --- a/static/partials/user-admin.html +++ b/static/partials/user-admin.html @@ -1,5 +1,10 @@
-
+
+
+
{{ errorMessage }}
+
+
+
@@ -18,7 +23,23 @@
-
+
+
+
+
+ Plan Usage +
+
+ {{ subscription.usedPrivateRepos }} of {{ subscribedPlan.privateRepos }} private repositories used +
+
+
+
+
+
+
+
+
\ No newline at end of file