Add some feedback about the number of private repositories used.
This commit is contained in:
parent
c7e83c4d41
commit
c044893ff9
4 changed files with 52 additions and 20 deletions
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.';
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
<div class="container">
|
||||
<div class="row" ng-hide="loading">
|
||||
<div class="row" ng-show="errorMessage">
|
||||
<div class="col-md-12">
|
||||
<div class="alert alert-danger">{{ errorMessage }}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row" ng-hide="planLoading">
|
||||
<div class="col-md-4" ng-repeat='plan in plans'>
|
||||
<div class="panel" ng-class="{'panel-success': subscription.plan == plan.stripeId, 'panel-default': subscription.plan != plan.stripeId}">
|
||||
<div class="panel-heading">
|
||||
|
@ -18,7 +23,23 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="loading" ng-show="loading">
|
||||
<div class="row" ng-hide="planLoading">
|
||||
<div class="col-md-6">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
Plan Usage
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<b>{{ subscription.usedPrivateRepos }}</b> of {{ subscribedPlan.privateRepos }} private repositories used
|
||||
<div class="progress">
|
||||
<div class="progress-bar" role="progressbar" aria-valuenow="{{ subscription.usedPrivateRepos }}" aria-valuemin="0" aria-valuemax="{{ subscribedPlan.privateRepos }}" style="width: {{ planUsagePercent }}%;">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="loading" ng-show="planLoading">
|
||||
<div class="spin"></div>
|
||||
</div>
|
||||
</div>
|
Reference in a new issue