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

Commit 18eb985

Browse files
authored
reorg admin (#1294)
1 parent d3b6462 commit 18eb985

File tree

2 files changed

+101
-28
lines changed

2 files changed

+101
-28
lines changed

codecov_auth/admin.py

Lines changed: 93 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -568,15 +568,13 @@ class OwnerAdmin(AdminMixin, admin.ModelAdmin):
568568
"username",
569569
"service",
570570
"email",
571-
"business_email",
572571
"name",
573572
"service_id",
574573
"createstamp",
575574
"parent_service_id",
576575
"root_parent_service_id",
577576
"private_access",
578577
"cache",
579-
"free",
580578
"invoice_details",
581579
"yaml",
582580
"updatestamp",
@@ -586,28 +584,87 @@ class OwnerAdmin(AdminMixin, admin.ModelAdmin):
586584
"student_updated_at",
587585
"user",
588586
"trial_fired_by",
587+
"sentry_user_id",
589588
)
590589

591-
fields = readonly_fields + (
592-
"admins",
593-
"plan_auto_activate",
594-
"onboarding_completed",
595-
"staff",
596-
"plan",
597-
"plan_provider",
598-
"plan_user_count",
599-
"plan_activated_users",
600-
"uses_invoice",
601-
"delinquent",
602-
"integration_id",
603-
"bot",
604-
"stripe_customer_id",
605-
"stripe_subscription_id",
606-
"organizations",
607-
"max_upload_limit",
608-
"account",
609-
"upload_token_required_for_public_repos",
610-
)
590+
fieldsets = [
591+
(
592+
None,
593+
{
594+
"fields": [
595+
"ownerid",
596+
"username",
597+
"service",
598+
"name",
599+
"service_id",
600+
"student",
601+
"user",
602+
"sentry_user_id",
603+
]
604+
},
605+
),
606+
(
607+
"Trial fields",
608+
{
609+
"fields": [
610+
"trial_status",
611+
"trial_fired_by",
612+
"trial_start_date",
613+
"trial_end_date",
614+
]
615+
},
616+
),
617+
(
618+
"Plan fields",
619+
{
620+
"fields": [
621+
"plan_auto_activate",
622+
"plan",
623+
"plan_user_count",
624+
"free",
625+
"plan_activated_users",
626+
]
627+
},
628+
),
629+
(
630+
"Billing fields",
631+
{
632+
"fields": [
633+
"uses_invoice",
634+
"delinquent",
635+
"stripe_customer_id",
636+
"stripe_subscription_id",
637+
]
638+
},
639+
),
640+
(
641+
"Reference fields",
642+
{
643+
"fields": [
644+
"admins",
645+
"staff",
646+
"upload_token_required_for_public_repos",
647+
"email",
648+
"parent_service_id",
649+
"root_parent_service_id",
650+
"private_access",
651+
"cache",
652+
"yaml",
653+
"bot",
654+
"max_upload_limit",
655+
"organizations",
656+
"account",
657+
"permission",
658+
"student_created_at",
659+
"student_updated_at",
660+
"onboarding_completed",
661+
"did_trial",
662+
"createstamp",
663+
"updatestamp",
664+
]
665+
},
666+
),
667+
]
611668

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

689+
# workaround for when a model field has null=True without blank=True
690+
for field_name in [
691+
"trial_start_date",
692+
"trial_end_date",
693+
"trial_status",
694+
"free",
695+
]:
696+
if form.base_fields.get(field_name):
697+
form.base_fields[field_name].required = False
698+
632699
return form
633700

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

650717
def save_related(self, request: HttpRequest, form, formsets, change: bool) -> None:
651718
if formsets:
652-
token_formset = formsets[0]
653-
token_id = token_formset.data.get("organization_tokens-0-id")
654-
token_refresh = token_formset.data.get("organization_tokens-0-REFRESH")
719+
formset = formsets[0]
720+
token_id = formset.data.get("organization_tokens-0-id")
721+
token_refresh = formset.data.get("organization_tokens-0-REFRESH")
655722
# token_id only exists if the token already exists (edit operation)
656-
if token_formset.is_valid() and token_id and token_refresh:
723+
if formset.is_valid() and token_id and token_refresh:
657724
OrgLevelTokenService.refresh_token(token_id)
658725
return super().save_related(request, form, formsets, change)
659726

codecov_auth/tests/test_admin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ def test_org_token_refresh_request_calls_service_to_refresh_token(
242242
"organization_tokens-0-REFRESH": "on",
243243
"_continue": ["Save and continue editing"],
244244
}
245-
self.client.post(request_url, data=fake_data)
245+
res = self.client.post(request_url, data=fake_data)
246+
247+
# redirected to list page if form change is successful
248+
self.assertEqual(res.status_code, 302)
246249
mock_refresh.assert_called_with(str(org_token.id))
247250

248251
@patch(
@@ -278,7 +281,10 @@ def test_org_token_request_doesnt_call_service_to_refresh_token(self, mock_refre
278281
"organization_tokens-0-token_type": ["upload"],
279282
"_continue": ["Save and continue editing"],
280283
}
281-
self.client.post(request_url, data=fake_data)
284+
res = self.client.post(request_url, data=fake_data)
285+
286+
# redirected to list page if form change is successful
287+
self.assertEqual(res.status_code, 302)
282288
mock_refresh.assert_not_called()
283289

284290
def test_start_trial_ui_display(self):

0 commit comments

Comments
 (0)