Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 93 additions & 26 deletions codecov_auth/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,13 @@ class OwnerAdmin(AdminMixin, admin.ModelAdmin):
"username",
"service",
"email",
"business_email",
"name",
"service_id",
"createstamp",
"parent_service_id",
"root_parent_service_id",
"private_access",
"cache",
"free",
"invoice_details",
"yaml",
"updatestamp",
Expand All @@ -586,28 +584,87 @@ class OwnerAdmin(AdminMixin, admin.ModelAdmin):
"student_updated_at",
"user",
"trial_fired_by",
"sentry_user_id",
)

fields = readonly_fields + (
"admins",
"plan_auto_activate",
"onboarding_completed",
"staff",
"plan",
"plan_provider",
"plan_user_count",
"plan_activated_users",
"uses_invoice",
"delinquent",
"integration_id",
"bot",
"stripe_customer_id",
"stripe_subscription_id",
"organizations",
"max_upload_limit",
"account",
"upload_token_required_for_public_repos",
)
fieldsets = [
(
None,
{
"fields": [
"ownerid",
"username",
"service",
"name",
"service_id",
"student",
"user",
"sentry_user_id",
]
},
),
(
"Trial fields",
{
"fields": [
"trial_status",
"trial_fired_by",
"trial_start_date",
"trial_end_date",
]
},
),
(
"Plan fields",
{
"fields": [
"plan_auto_activate",
"plan",
"plan_user_count",
"free",
"plan_activated_users",
]
},
),
(
"Billing fields",
{
"fields": [
"uses_invoice",
"delinquent",
"stripe_customer_id",
"stripe_subscription_id",
]
},
),
(
"Reference fields",
{
"fields": [
"admins",
"staff",
"upload_token_required_for_public_repos",
"email",
"parent_service_id",
"root_parent_service_id",
"private_access",
"cache",
"yaml",
"bot",
"max_upload_limit",
"organizations",
"account",
"permission",
"student_created_at",
"student_updated_at",
"onboarding_completed",
"did_trial",
"createstamp",
"updatestamp",
]
},
),
]

def get_form(self, request, obj=None, change=False, **kwargs):
form = super().get_form(request, obj, change, **kwargs)
Expand All @@ -629,6 +686,16 @@ def get_form(self, request, obj=None, change=False, **kwargs):
field.widget.can_change_related = False
field.widget.can_delete_related = False

# workaround for when a model field has null=True without blank=True
for field_name in [
"trial_start_date",
"trial_end_date",
"trial_status",
"free",
]:
if form.base_fields.get(field_name):
form.base_fields[field_name].required = False

return form

def has_add_permission(self, _, obj=None):
Expand All @@ -649,11 +716,11 @@ def get_deleted_objects(self, objs, request):

def save_related(self, request: HttpRequest, form, formsets, change: bool) -> None:
if formsets:
token_formset = formsets[0]
token_id = token_formset.data.get("organization_tokens-0-id")
token_refresh = token_formset.data.get("organization_tokens-0-REFRESH")
formset = formsets[0]
token_id = formset.data.get("organization_tokens-0-id")
token_refresh = formset.data.get("organization_tokens-0-REFRESH")
# token_id only exists if the token already exists (edit operation)
if token_formset.is_valid() and token_id and token_refresh:
if formset.is_valid() and token_id and token_refresh:
OrgLevelTokenService.refresh_token(token_id)
return super().save_related(request, form, formsets, change)

Expand Down
10 changes: 8 additions & 2 deletions codecov_auth/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ def test_org_token_refresh_request_calls_service_to_refresh_token(
"organization_tokens-0-REFRESH": "on",
"_continue": ["Save and continue editing"],
}
self.client.post(request_url, data=fake_data)
res = self.client.post(request_url, data=fake_data)

# redirected to list page if form change is successful
self.assertEqual(res.status_code, 302)
mock_refresh.assert_called_with(str(org_token.id))

@patch(
Expand Down Expand Up @@ -278,7 +281,10 @@ def test_org_token_request_doesnt_call_service_to_refresh_token(self, mock_refre
"organization_tokens-0-token_type": ["upload"],
"_continue": ["Save and continue editing"],
}
self.client.post(request_url, data=fake_data)
res = self.client.post(request_url, data=fake_data)

# redirected to list page if form change is successful
self.assertEqual(res.status_code, 302)
mock_refresh.assert_not_called()

def test_start_trial_ui_display(self):
Expand Down
Loading