admin.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from django.contrib import admin
  2. from django.conf import settings
  3. from django.db.models import Count, OuterRef, Subquery, Sum
  4. from django.utils.html import format_html
  5. from organizations.base_admin import (
  6. BaseOrganizationAdmin,
  7. BaseOrganizationUserAdmin,
  8. BaseOwnerInline,
  9. )
  10. from performance.models import TransactionEvent
  11. from projects.models import Project
  12. from .models import Organization, OrganizationOwner, OrganizationUser
  13. ORGANIZATION_LIST_FILTER = (
  14. "is_active",
  15. "is_accepting_events",
  16. )
  17. if settings.BILLING_ENABLED:
  18. ORGANIZATION_LIST_FILTER += ("djstripe_customers__subscriptions__plan__product",)
  19. class OwnerInline(BaseOwnerInline):
  20. model = OrganizationOwner
  21. class OrganizationUserInline(admin.StackedInline):
  22. raw_id_fields = ("user",)
  23. model = OrganizationUser
  24. extra = 0
  25. class OrganizationAdmin(BaseOrganizationAdmin):
  26. list_per_page = 50
  27. list_display = [
  28. "name",
  29. "is_active",
  30. "is_accepting_events",
  31. "issue_events",
  32. "transaction_events",
  33. "uptime_check_events",
  34. "total_events",
  35. ]
  36. readonly_fields = ("customers",)
  37. list_filter = ORGANIZATION_LIST_FILTER
  38. inlines = [OrganizationUserInline, OwnerInline]
  39. show_full_result_count = False
  40. def issue_events(self, obj):
  41. return obj.issue_events
  42. def customers(self, obj):
  43. return format_html(
  44. " ".join(
  45. [
  46. f'<a href="{customer.get_stripe_dashboard_url()}" target="_blank">{customer.id}</a>'
  47. for customer in obj.djstripe_customers.all()
  48. ]
  49. )
  50. )
  51. def transaction_events(self, obj):
  52. return obj.transaction_events
  53. def uptime_check_events(self, obj):
  54. return obj.uptime_check_events
  55. def total_events(self, obj):
  56. total = 0
  57. if obj.issue_events:
  58. total += obj.issue_events
  59. if obj.transaction_events:
  60. total += obj.transaction_events
  61. if obj.uptime_check_events:
  62. total += obj.uptime_check_events
  63. return total
  64. def get_queryset(self, request):
  65. queryset = super().get_queryset(request)
  66. projects = Project.objects.filter(organization=OuterRef("pk")).values(
  67. "organization"
  68. )
  69. total_issue_events = projects.annotate(total=Sum("issue__count")).values(
  70. "total"
  71. )
  72. total_transaction_events = projects.annotate(
  73. total=Count("transactionevent")
  74. ).values("total")
  75. queryset = queryset.annotate(
  76. issue_events=Subquery(total_issue_events),
  77. transaction_events=Subquery(total_transaction_events),
  78. uptime_check_events=Count("monitor__checks"),
  79. )
  80. return queryset
  81. class OrganizationUserAdmin(BaseOrganizationUserAdmin):
  82. list_display = ["user", "organization", "role"]
  83. admin.site.register(Organization, OrganizationAdmin)
  84. admin.site.register(OrganizationUser, OrganizationUserAdmin)