diff --git a/endpoints/api/billing.py b/endpoints/api/billing.py index aae577908..b6596054b 100644 --- a/endpoints/api/billing.py +++ b/endpoints/api/billing.py @@ -18,15 +18,14 @@ import features import uuid import json -def lookup_allowed_private_repos(namespace): - """ Returns false if the given namespace has used its allotment of private repositories. """ - # Lookup the namespace and verify it has a subscription. +def get_namespace_plan(namespace): + """ Returns the plan of the given namespace. """ namespace_user = model.user.get_namespace_user(namespace) if namespace_user is None: - return False + return None if not namespace_user.stripe_id: - return False + return None # Ask Stripe for the subscribed plan. # TODO: Can we cache this or make it faster somehow? @@ -36,14 +35,20 @@ def lookup_allowed_private_repos(namespace): abort(503, message='Cannot contact Stripe') if not cus.subscription: + return None + + return get_plan(cus.subscription.plan.id) + + +def lookup_allowed_private_repos(namespace): + """ Returns false if the given namespace has used its allotment of private repositories. """ + current_plan = get_namespace_plan(namespace) + if current_plan is None: return False # Find the number of private repositories used by the namespace and compare it to the # plan subscribed. private_repos = model.user.get_private_repo_count(namespace) - current_plan = get_plan(cus.subscription.plan.id) - if current_plan is None: - return False return private_repos < current_plan['privateRepos'] diff --git a/endpoints/api/repository.py b/endpoints/api/repository.py index b9664864e..851bd644d 100644 --- a/endpoints/api/repository.py +++ b/endpoints/api/repository.py @@ -17,7 +17,8 @@ from endpoints.api import (truthy_bool, format_date, nickname, log_action, valid RepositoryParamResource, resource, query_param, parse_args, ApiResource, request_error, require_scope, Unauthorized, NotFound, InvalidRequest, path_param, ExceedsLicenseException) -from endpoints.api.billing import lookup_allowed_private_repos +from endpoints.api.billing import lookup_allowed_private_repos, get_namespace_plan +from endpoints.common import check_repository_usage from auth.permissions import (ModifyRepositoryPermission, AdministerRepositoryPermission, CreateRepositoryPermission) @@ -329,6 +330,9 @@ class Repository(RepositoryParamResource): def delete(self, namespace, repository): """ Delete a repository. """ model.repository.purge_repository(namespace, repository) + user = model.user.get_namespace_user(namespace) + plan = get_namespace_plan(namespace) + check_repository_usage(user, plan) log_action('delete_repo', namespace, {'repo': repository, 'namespace': namespace}) return 'Deleted', 204 diff --git a/endpoints/common.py b/endpoints/common.py index 7469c58be..6e3abb4ac 100644 --- a/endpoints/common.py +++ b/endpoints/common.py @@ -200,10 +200,13 @@ def render_page_template(name, **kwargs): def check_repository_usage(user_or_org, plan_found): private_repos = model.user.get_private_repo_count(user_or_org.username) - repos_allowed = plan_found['privateRepos'] + if plan_found is None: + repos_allowed = 0 + else: + repos_allowed = plan_found['privateRepos'] if private_repos > repos_allowed: - model.notification.create_notification('over_private_usage', user_or_org, + model.notification.create_unique_notification('over_private_usage', user_or_org, {'namespace': user_or_org.username}) else: model.notification.delete_notifications_by_kind(user_or_org, 'over_private_usage')