Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 3a5b3af

Browse files
committed
add missing imports + refactor to call 1 query
1 parent 16f7fba commit 3a5b3af

File tree

7 files changed

+29
-10
lines changed

7 files changed

+29
-10
lines changed

api/internal/owner/serializers.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
)
1313
from shared.plan.service import PlanService
1414

15-
from codecov_auth.models import Owner
15+
from codecov_auth.models import Owner, Plan
1616
from services.billing import BillingService
1717
from services.sentry import send_user_webhook as send_sentry_webhook
1818

@@ -147,10 +147,18 @@ def validate(self, plan: Dict[str, Any]) -> Dict[str, Any]:
147147
detail="You cannot update your plan manually, for help or changes to plan, connect with [email protected]"
148148
)
149149

150+
active_plans = list(
151+
Plan.objects.filter(paid_plan=True, is_active=True).values_list(
152+
"name", "tier"
153+
)
154+
)
155+
active_plan_names = {name for name, _ in active_plans}
156+
team_tier_plans = {
157+
name for name, tier in active_plans if tier == TierName.TEAM.value
158+
}
159+
150160
# Validate quantity here because we need access to whole plan object
151-
if plan["value"] in Plan.objects.filter(
152-
paid_plan=True, is_active=True
153-
).values_list("name", flat=True):
161+
if plan["value"] in active_plan_names:
154162
if "quantity" not in plan:
155163
raise serializers.ValidationError(
156164
"Field 'quantity' required for updating to paid plans"
@@ -179,8 +187,7 @@ def validate(self, plan: Dict[str, Any]) -> Dict[str, Any]:
179187
"Quantity or plan for paid plan must be different from the existing one"
180188
)
181189
if (
182-
plan["value"]
183-
in Plan.objects.filter(tier=TierName.TEAM.value, is_active=True)
190+
plan["value"] in team_tier_plans
184191
and plan["quantity"] > TEAM_PLAN_MAX_USERS
185192
):
186193
raise serializers.ValidationError(

api/internal/tests/views/test_account_viewset.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
AccountFactory,
1313
InvoiceBillingFactory,
1414
OwnerFactory,
15+
PlanFactory,
16+
TierFactory,
1517
UserFactory,
1618
)
17-
from shared.plan.constants import PlanName, TrialStatus
19+
from shared.plan.constants import PlanName, TierName, TrialStatus
1820
from stripe import StripeError
1921

2022
from api.internal.tests.test_utils import GetAdminProviderAdapter
@@ -187,6 +189,13 @@ def test_retrieve_account_gets_account_fields(self):
187189
def test_retrieve_account_gets_account_fields_when_there_are_scheduled_details(
188190
self, mock_retrieve_subscription, mock_retrieve_schedule
189191
):
192+
tier = TierFactory(tier_name=TierName.BASIC.value)
193+
194+
PlanFactory(
195+
name="users-basic",
196+
tier=tier,
197+
is_active=True,
198+
)
190199
owner = OwnerFactory(
191200
admins=[self.current_owner.ownerid], stripe_subscription_id="sub_123"
192201
)

billing/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.db.models import QuerySet
33
from shared.plan.constants import TierName
44

5-
from codecov_auth.models import Owner
5+
from codecov_auth.models import Owner, Plan
66

77

88
def on_enterprise_plan(owner: Owner) -> bool:

codecov_auth/services/org_level_token_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.dispatch import receiver
66
from django.forms import ValidationError
77

8-
from codecov_auth.models import OrganizationLevelToken, Owner
8+
from codecov_auth.models import OrganizationLevelToken, Owner, Plan
99

1010
log = logging.getLogger(__name__)
1111

graphql_api/types/owner/owner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Account,
2323
GithubAppInstallation,
2424
Owner,
25+
Plan,
2526
)
2627
from codecov_auth.views.okta_cloud import OKTA_SIGNED_IN_ACCOUNTS_SESSION_KEY
2728
from core.models import Repository

services/billing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
from django.conf import settings
99
from shared.plan.constants import (
1010
PlanBillingRate,
11+
TierName,
1112
)
1213
from shared.plan.service import PlanService
1314

1415
from billing.constants import REMOVED_INVOICE_STATUSES
15-
from codecov_auth.models import Owner
16+
from codecov_auth.models import Owner, Plan
1617

1718
log = logging.getLogger(__name__)
1819

upload/helpers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
SERVICE_GITHUB_ENTERPRISE,
2929
GithubAppInstallation,
3030
Owner,
31+
Plan,
3132
)
3233
from core.models import Commit, Repository
3334
from reports.models import CommitReport, ReportSession

0 commit comments

Comments
 (0)