Add a check_repository_usage method which adds (or removes) a notification on the user/org when they go over their plan usage
This commit is contained in:
parent
525ef8d14f
commit
e5a461989f
6 changed files with 39 additions and 28 deletions
|
@ -28,7 +28,7 @@ from auth.permissions import (ReadRepositoryPermission,
|
|||
ViewTeamPermission,
|
||||
UserPermission)
|
||||
from endpoints.common import (common_login, get_route_data, truthy_param,
|
||||
start_build, add_notification)
|
||||
start_build, check_repository_usage)
|
||||
from endpoints.trigger import (BuildTrigger, TriggerActivationException,
|
||||
TriggerDeactivationException,
|
||||
EmptyRepositoryException)
|
||||
|
@ -2197,6 +2197,7 @@ def subscribe(user, plan, token, require_business_plan):
|
|||
cus = stripe.Customer.create(email=user.email, plan=plan, card=card)
|
||||
user.stripe_id = cus.id
|
||||
user.save()
|
||||
check_repository_usage(user, plan_found)
|
||||
log_action('account_change_plan', user.username, {'plan': plan})
|
||||
except stripe.CardError as e:
|
||||
return carderror_response(e)
|
||||
|
@ -2213,6 +2214,7 @@ def subscribe(user, plan, token, require_business_plan):
|
|||
# We only have to cancel the subscription if they actually have one
|
||||
cus.cancel_subscription()
|
||||
cus.save()
|
||||
check_repository_usage(user, plan_found)
|
||||
log_action('account_change_plan', user.username, {'plan': plan})
|
||||
|
||||
else:
|
||||
|
@ -2228,6 +2230,7 @@ def subscribe(user, plan, token, require_business_plan):
|
|||
return carderror_response(e)
|
||||
|
||||
response_json = subscription_view(cus.subscription, private_repos)
|
||||
check_repository_usage(user, plan_found)
|
||||
log_action('account_change_plan', user.username, {'plan': plan})
|
||||
|
||||
resp = jsonify(response_json)
|
||||
|
|
|
@ -126,11 +126,14 @@ def render_page_template(name, **kwargs):
|
|||
return resp
|
||||
|
||||
|
||||
def add_notification(kind, metadata=None, user=None):
|
||||
if not user and current_user:
|
||||
user = current_user.db_user()
|
||||
def check_repository_usage(user_or_org, plan_found):
|
||||
private_repos = model.get_private_repo_count(user_or_org.username)
|
||||
repos_allowed = plan_found['privateRepos']
|
||||
|
||||
return model.create_notification(kind, user, metadata or {})
|
||||
if private_repos > repos_allowed:
|
||||
model.create_notification('over_private_usage', user_or_org, {'namespace': user_or_org.username})
|
||||
else:
|
||||
model.delete_notifications_by_kind(user_or_org, 'over_private_usage')
|
||||
|
||||
|
||||
def start_build(repository, dockerfile_id, tags, build_name, subdir, manual,
|
||||
|
|
Reference in a new issue