Merge pull request #791 from mjibson/clear-repo-notifications
Remove error notification when user deletes repos
This commit is contained in:
commit
5d9999d1f7
3 changed files with 23 additions and 11 deletions
|
@ -18,15 +18,14 @@ import features
|
||||||
import uuid
|
import uuid
|
||||||
import json
|
import json
|
||||||
|
|
||||||
def lookup_allowed_private_repos(namespace):
|
def get_namespace_plan(namespace):
|
||||||
""" Returns false if the given namespace has used its allotment of private repositories. """
|
""" Returns the plan of the given namespace. """
|
||||||
# Lookup the namespace and verify it has a subscription.
|
|
||||||
namespace_user = model.user.get_namespace_user(namespace)
|
namespace_user = model.user.get_namespace_user(namespace)
|
||||||
if namespace_user is None:
|
if namespace_user is None:
|
||||||
return False
|
return None
|
||||||
|
|
||||||
if not namespace_user.stripe_id:
|
if not namespace_user.stripe_id:
|
||||||
return False
|
return None
|
||||||
|
|
||||||
# Ask Stripe for the subscribed plan.
|
# Ask Stripe for the subscribed plan.
|
||||||
# TODO: Can we cache this or make it faster somehow?
|
# 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')
|
abort(503, message='Cannot contact Stripe')
|
||||||
|
|
||||||
if not cus.subscription:
|
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
|
return False
|
||||||
|
|
||||||
# Find the number of private repositories used by the namespace and compare it to the
|
# Find the number of private repositories used by the namespace and compare it to the
|
||||||
# plan subscribed.
|
# plan subscribed.
|
||||||
private_repos = model.user.get_private_repo_count(namespace)
|
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']
|
return private_repos < current_plan['privateRepos']
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,8 @@ from endpoints.api import (truthy_bool, format_date, nickname, log_action, valid
|
||||||
RepositoryParamResource, resource, query_param, parse_args, ApiResource,
|
RepositoryParamResource, resource, query_param, parse_args, ApiResource,
|
||||||
request_error, require_scope, Unauthorized, NotFound, InvalidRequest,
|
request_error, require_scope, Unauthorized, NotFound, InvalidRequest,
|
||||||
path_param, ExceedsLicenseException)
|
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,
|
from auth.permissions import (ModifyRepositoryPermission, AdministerRepositoryPermission,
|
||||||
CreateRepositoryPermission)
|
CreateRepositoryPermission)
|
||||||
|
@ -329,6 +330,9 @@ class Repository(RepositoryParamResource):
|
||||||
def delete(self, namespace, repository):
|
def delete(self, namespace, repository):
|
||||||
""" Delete a repository. """
|
""" Delete a repository. """
|
||||||
model.repository.purge_repository(namespace, 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,
|
log_action('delete_repo', namespace,
|
||||||
{'repo': repository, 'namespace': namespace})
|
{'repo': repository, 'namespace': namespace})
|
||||||
return 'Deleted', 204
|
return 'Deleted', 204
|
||||||
|
|
|
@ -200,10 +200,13 @@ def render_page_template(name, **kwargs):
|
||||||
|
|
||||||
def check_repository_usage(user_or_org, plan_found):
|
def check_repository_usage(user_or_org, plan_found):
|
||||||
private_repos = model.user.get_private_repo_count(user_or_org.username)
|
private_repos = model.user.get_private_repo_count(user_or_org.username)
|
||||||
|
if plan_found is None:
|
||||||
|
repos_allowed = 0
|
||||||
|
else:
|
||||||
repos_allowed = plan_found['privateRepos']
|
repos_allowed = plan_found['privateRepos']
|
||||||
|
|
||||||
if private_repos > repos_allowed:
|
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})
|
{'namespace': user_or_org.username})
|
||||||
else:
|
else:
|
||||||
model.notification.delete_notifications_by_kind(user_or_org, 'over_private_usage')
|
model.notification.delete_notifications_by_kind(user_or_org, 'over_private_usage')
|
||||||
|
|
Reference in a new issue