This commit is contained in:
root 2013-11-12 00:30:53 +00:00
commit 8c92230b71
3 changed files with 63 additions and 20 deletions

View file

@ -40,6 +40,10 @@ def api_login_required(f):
def decorated_view(*args, **kwargs):
if not current_user.is_authenticated():
abort(401)
if current_user and current_user.db_user() and current_user.db_user().organization:
abort(401)
return f(*args, **kwargs)
return decorated_view
@ -82,6 +86,9 @@ def get_logged_in_user():
return jsonify({'anonymous': True})
user = current_user.db_user()
if not user or user.organization:
return jsonify({'anonymous': True})
organizations = model.get_user_organizations(user.username)
return jsonify({
@ -223,9 +230,7 @@ def conduct_signin(username, password):
@api_login_required
def logout():
logout_user()
identity_changed.send(app, identity=AnonymousIdentity())
return make_response('Success', 200)
@ -1190,7 +1195,8 @@ def subscribe_api():
token = request_data['token'] if 'token' in request_data else None
user = current_user.db_user()
return subscribe(user, plan, token, USER_PLANS)
def subscribe(user, plan, token, accepted_plans):
plan_found = None
for plan_obj in accepted_plans:
@ -1202,16 +1208,25 @@ def subscribe(user, plan, token, accepted_plans):
private_repos = model.get_private_repo_count(user.username)
if not user.stripe_id:
# Create the customer and plan simultaneously
card = token
cus = stripe.Customer.create(email=user.email, plan=plan, card=card)
user.stripe_id = cus.id
user.save()
# This is the default response
response_json = {
'plan': plan,
'usedPrivateRepos': private_repos,
}
status_code = 200
resp = jsonify(subscription_view(cus.subscription, private_repos))
resp.status_code = 201
return resp
if not user.stripe_id:
# Check if a non-paying user is trying to subscribe to a free plan
if not plan_found['price'] == 0:
# They want a real paying plan, create the customerand plan
# simultaneously
card = token
cus = stripe.Customer.create(email=user.email, plan=plan, card=card)
user.stripe_id = cus.id
user.save()
response_json = subscription_view(cus.subscription, private_repos)
status_code = 201
else:
# Change the plan
@ -1223,11 +1238,6 @@ def subscribe(user, plan, token, accepted_plans):
cus.cancel_subscription()
cus.save()
response_json = {
'plan': plan,
'usedPrivateRepos': private_repos,
}
else:
cus.plan = plan
# User may have been a previous customer who is resubscribing
@ -1237,7 +1247,9 @@ def subscribe(user, plan, token, accepted_plans):
cus.save()
response_json = subscription_view(cus.subscription, private_repos)
return jsonify(response_json)
resp = jsonify(response_json)
resp.status_code = status_code
return resp
@app.route('/api/organization/<orgname>/plan', methods=['PUT'])

View file

@ -60,7 +60,7 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics',
var userService = {}
userService.load = function() {
userService.load = function(opt_callback) {
var userFetch = Restangular.one('user/');
userFetch.get().then(function(loadedUser) {
userResponse = loadedUser;
@ -76,6 +76,10 @@ quayApp = angular.module('quay', ['restangular', 'angularMoment', 'angulartics',
'$created': new Date()
})
}
if (opt_callback) {
opt_callback();
}
});
};
@ -867,7 +871,16 @@ quayApp.directive('ngBlur', function() {
};
});
quayApp.run(['$location', '$rootScope', function($location, $rootScope) {
quayApp.run(['$location', '$rootScope', 'Restangular', 'UserService', function($location, $rootScope, Restangular, UserService) {
Restangular.setErrorInterceptor(function(response) {
if (response.status == 401) {
$('#sessionexpiredModal').modal({});
return false;
}
return true;
});
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
if (current.$$route.title) {
$rootScope.title = current.$$route.title;

View file

@ -30,4 +30,22 @@
{% block body_content %}
<div ng-view></div>
<!-- Modal message dialog -->
<div class="modal fade" id="sessionexpiredModal" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Session Expired</h4>
</div>
<div class="modal-body">
Your user session has expired. Please reload to continue.
</div>
<div class="modal-footer">
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
{% endblock %}