Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Django admin portal changes for plans and tiers #1097

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
107 changes: 106 additions & 1 deletion codecov_auth/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.contrib.admin.models import LogEntry
from django.db.models import OuterRef, Subquery
from django.db.models.fields import BLANK_CHOICE_DASH
from django.forms import CheckboxInput, Select
from django.forms import CheckboxInput, Select, Textarea
from django.http import HttpRequest
from django.shortcuts import redirect, render
from django.utils import timezone
Expand All @@ -17,7 +17,9 @@
Account,
AccountsUsers,
InvoiceBilling,
Plan,
StripeBilling,
Tier,
)
from shared.plan.constants import USER_PLAN_REPRESENTATIONS
from shared.plan.service import PlanService
Expand Down Expand Up @@ -708,3 +710,106 @@ class AccountsUsersAdmin(AdminMixin, admin.ModelAdmin):
]

fields = readonly_fields + ["account", "user"]


class PlansInline(admin.TabularInline):
model = Plan
extra = 1
verbose_name_plural = "Plans (click save to commit changes)"
verbose_name = "Plan"
fields = [
"name",
"marketing_name",
"base_unit_price",
"billing_rate",
"max_seats",
"monthly_uploads_limit",
"paid_plan",
"is_active",
]
formfield_overrides = {
Plan._meta.get_field("benefits"): {"widget": Textarea(attrs={"rows": 3})},
}


@admin.register(Tier)
class TierAdmin(admin.ModelAdmin):
list_display = (
"tier_name",
"bundle_analysis",
"test_analytics",
"flaky_test_detection",
"project_coverage",
"private_repo_support",
)
list_editable = (
"bundle_analysis",
"test_analytics",
"flaky_test_detection",
"project_coverage",
"private_repo_support",
)
search_fields = ("tier_name__iregex",)
inlines = [PlansInline]
fields = [
"tier_name",
"bundle_analysis",
"test_analytics",
"flaky_test_detection",
"project_coverage",
"private_repo_support",
]


class PlanAdminForm(forms.ModelForm):
class Meta:
model = Plan
fields = "__all__"

def clean_base_unit_price(self):
RulaKhaled marked this conversation as resolved.
Show resolved Hide resolved
base_unit_price = self.cleaned_data.get("base_unit_price")
if base_unit_price is not None and base_unit_price < 0:
raise forms.ValidationError("Base unit price cannot be negative.")
return base_unit_price

def clean_max_seats(self):
max_seats = self.cleaned_data.get("max_seats")
if max_seats is not None and max_seats < 0:
raise forms.ValidationError("Max seats cannot be negative.")
return max_seats

def clean_monthly_uploads_limit(self):
monthly_uploads_limit = self.cleaned_data.get("monthly_uploads_limit")
if monthly_uploads_limit is not None and monthly_uploads_limit < 0:
raise forms.ValidationError("Monthly uploads limit cannot be negative.")
return monthly_uploads_limit


@admin.register(Plan)
class PlanAdmin(admin.ModelAdmin):
form = PlanAdminForm
list_display = (
"name",
"marketing_name",
"base_unit_price",
"is_active",
"paid_plan",
RulaKhaled marked this conversation as resolved.
Show resolved Hide resolved
)
list_filter = ("is_active", "paid_plan", "billing_rate")
RulaKhaled marked this conversation as resolved.
Show resolved Hide resolved
search_fields = ("name__iregex", "marketing_name__iregex")
fields = [
"tier",
"name",
"marketing_name",
"base_unit_price",
"benefits",
"billing_rate",
"is_active",
"max_seats",
"monthly_uploads_limit",
"paid_plan",
]
formfield_overrides = {
Plan._meta.get_field("benefits"): {"widget": Textarea(attrs={"rows": 3})},
}
autocomplete_fields = ["tier"] # a dropdown for selecting related Tiers
165 changes: 164 additions & 1 deletion codecov_auth/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
InvoiceBillingFactory,
OrganizationLevelTokenFactory,
OwnerFactory,
PlanFactory,
SentryUserFactory,
SessionFactory,
StripeBillingFactory,
TierFactory,
UserFactory,
)
from shared.django_apps.core.tests.factories import PullFactory, RepositoryFactory
Expand All @@ -39,7 +41,14 @@
UserAdmin,
find_and_remove_stale_users,
)
from codecov_auth.models import OrganizationLevelToken, Owner, SentryUser, User
from codecov_auth.models import (
OrganizationLevelToken,
Owner,
Plan,
SentryUser,
Tier,
User,
)
from core.models import Pull


Expand Down Expand Up @@ -881,3 +890,157 @@ def test_account_widget(self):
self.assertFalse(form.base_fields["account"].widget.can_add_related)
self.assertFalse(form.base_fields["account"].widget.can_change_related)
self.assertFalse(form.base_fields["account"].widget.can_delete_related)


class PlanAdminTest(TestCase):
def setUp(self):
self.staff_user = UserFactory(is_staff=True)
self.client.force_login(user=self.staff_user)
admin_site = AdminSite()
admin_site.register(Plan)

def test_plan_admin_modal_display(self):
plan = PlanFactory()
response = self.client.get(
reverse("admin:codecov_auth_plan_change", args=[plan.pk])
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, plan.name)

def test_plan_modal_tiers_display(self):
tier = TierFactory()
plan = PlanFactory(tier=tier)
response = self.client.get(
reverse("admin:codecov_auth_plan_change", args=[plan.pk])
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, tier.tier_name)

def test_add_plans_modal_action(self):
plan = PlanFactory(base_unit_price=10, max_seats=5)
tier = TierFactory()
data = {
"action": "add_plans",
ACTION_CHECKBOX_NAME: [plan.pk],
"tier_id": tier.pk,
}
response = self.client.post(
reverse("admin:codecov_auth_plan_changelist"), data=data
)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/admin/codecov_auth/plan/")

def test_plan_change_form(self):
plan = PlanFactory()
response = self.client.get(
reverse("admin:codecov_auth_plan_change", args=[plan.pk])
)
self.assertEqual(response.status_code, 200)
for field in [
"tier",
"name",
"marketing_name",
"base_unit_price",
"benefits",
"billing_rate",
"is_active",
"max_seats",
"monthly_uploads_limit",
"paid_plan",
]:
self.assertContains(response, f"id_{field}")

def test_plan_change_form_validation(self):
plan = PlanFactory(base_unit_price=-10)

response = self.client.post(
reverse("admin:codecov_auth_plan_change", args=[plan.pk]),
{
"tier": plan.tier_id,
"name": plan.name,
"marketing_name": plan.marketing_name,
"base_unit_price": -10,
"benefits": plan.benefits,
"is_active": plan.is_active,
"paid_plan": plan.paid_plan,
},
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Base unit price cannot be negative.")

response = self.client.post(
reverse("admin:codecov_auth_plan_change", args=[plan.pk]),
{
"tier": plan.tier_id,
"name": plan.name,
"marketing_name": plan.marketing_name,
"base_unit_price": plan.base_unit_price,
"benefits": plan.benefits,
"is_active": plan.is_active,
"max_seats": -5,
"paid_plan": plan.paid_plan,
},
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Max seats cannot be negative.")

response = self.client.post(
reverse("admin:codecov_auth_plan_change", args=[plan.pk]),
{
"tier": plan.tier_id,
"name": plan.name,
"marketing_name": plan.marketing_name,
"benefits": plan.benefits,
"is_active": plan.is_active,
"monthly_uploads_limit": -5,
"paid_plan": plan.paid_plan,
},
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Monthly uploads limit cannot be negative.")


class TierAdminTest(TestCase):
def setUp(self):
self.staff_user = UserFactory(is_staff=True)
self.client.force_login(user=self.staff_user)
admin_site = AdminSite()
admin_site.register(Tier)

def test_tier_modal_plans_display(self):
tier = TierFactory()
response = self.client.get(
reverse("admin:codecov_auth_tier_change", args=[tier.pk])
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, tier.tier_name)

def test_add_plans_modal_action(self):
tier = TierFactory()
plan = PlanFactory()
data = {
"action": "add_plans",
ACTION_CHECKBOX_NAME: [plan.pk],
"tier_id": tier.pk,
}
response = self.client.post(
reverse("admin:codecov_auth_tier_changelist"), data=data
)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/admin/codecov_auth/tier/")

def test_tier_change_form(self):
tier = TierFactory()
response = self.client.get(
reverse("admin:codecov_auth_tier_change", args=[tier.pk])
)
self.assertEqual(response.status_code, 200)
for field in [
"tier_name",
"bundle_analysis",
"test_analytics",
"flaky_test_detection",
"project_coverage",
"private_repo_support",
]:
self.assertContains(response, f"id_{field}")
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ freezegun
google-cloud-pubsub
gunicorn>=22.0.0
https://github.com/codecov/opentelem-python/archive/refs/tags/v0.0.4a1.tar.gz#egg=codecovopentelem
https://github.com/codecov/shared/archive/07b39b24cc32b384df311262e4253f0c891562db.tar.gz#egg=shared
https://github.com/codecov/shared/archive/d5d7c208f9716ac0f48543f84b635aa28d15f0f2.tar.gz#egg=shared
https://github.com/photocrowd/django-cursor-pagination/archive/f560902696b0c8509e4d95c10ba0d62700181d84.tar.gz
idna>=3.7
minio
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ sentry-sdk[celery]==2.13.0
# shared
setproctitle==1.1.10
# via -r requirements.in
shared @ https://github.com/codecov/shared/archive/07b39b24cc32b384df311262e4253f0c891562db.tar.gz
shared @ https://github.com/codecov/shared/archive/d5d7c208f9716ac0f48543f84b635aa28d15f0f2.tar.gz
# via -r requirements.in
simplejson==3.17.2
# via -r requirements.in
Expand Down
Loading