diff --git a/codecov_auth/admin.py b/codecov_auth/admin.py index 9e1a738838..935f2fbd7a 100644 --- a/codecov_auth/admin.py +++ b/codecov_auth/admin.py @@ -568,7 +568,6 @@ class OwnerAdmin(AdminMixin, admin.ModelAdmin): "username", "service", "email", - "business_email", "name", "service_id", "createstamp", @@ -576,7 +575,6 @@ class OwnerAdmin(AdminMixin, admin.ModelAdmin): "root_parent_service_id", "private_access", "cache", - "free", "invoice_details", "yaml", "updatestamp", @@ -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) @@ -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): @@ -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) diff --git a/codecov_auth/tests/test_admin.py b/codecov_auth/tests/test_admin.py index fa89693bdf..824b7929d4 100644 --- a/codecov_auth/tests/test_admin.py +++ b/codecov_auth/tests/test_admin.py @@ -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( @@ -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):