admin.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from django.conf import settings
  2. from django.contrib import admin
  3. from django.utils.html import format_html
  4. from organizations.base_admin import (
  5. BaseOrganizationAdmin,
  6. BaseOrganizationUserAdmin,
  7. BaseOwnerInline,
  8. )
  9. from .models import Organization, OrganizationOwner, OrganizationUser
  10. ORGANIZATION_LIST_FILTER = (
  11. "is_active",
  12. "is_accepting_events",
  13. )
  14. if settings.BILLING_ENABLED:
  15. ORGANIZATION_LIST_FILTER += ("djstripe_customers__subscriptions__plan__product",)
  16. class OwnerInline(BaseOwnerInline):
  17. model = OrganizationOwner
  18. class OrganizationUserInline(admin.StackedInline):
  19. raw_id_fields = ("user",)
  20. model = OrganizationUser
  21. extra = 0
  22. class OrganizationAdmin(BaseOrganizationAdmin):
  23. list_per_page = 50
  24. list_display = [
  25. "name",
  26. "is_active",
  27. "is_accepting_events",
  28. "issue_events",
  29. "transaction_events",
  30. "uptime_check_events",
  31. "file_size",
  32. "total_events",
  33. ]
  34. readonly_fields = ("customers",)
  35. list_filter = ORGANIZATION_LIST_FILTER
  36. inlines = [OrganizationUserInline, OwnerInline]
  37. show_full_result_count = False
  38. def issue_events(self, obj):
  39. return obj.issue_event_count
  40. def customers(self, obj):
  41. return format_html(
  42. " ".join(
  43. [
  44. f'<a href="{customer.get_stripe_dashboard_url()}" target="_blank">{customer.id}</a>'
  45. for customer in obj.djstripe_customers.all()
  46. ]
  47. )
  48. )
  49. def transaction_events(self, obj):
  50. return obj.transaction_count
  51. def uptime_check_events(self, obj):
  52. return obj.uptime_check_event_count
  53. def file_size(self, obj):
  54. return obj.file_size
  55. def total_events(self, obj):
  56. return obj.total_event_count
  57. def get_queryset(self, request):
  58. qs = self.model.objects.with_event_counts()
  59. # From super
  60. ordering = self.ordering or ()
  61. if ordering:
  62. qs = qs.order_by(*ordering)
  63. return qs
  64. class OrganizationUserAdmin(BaseOrganizationUserAdmin):
  65. list_display = ["user", "organization", "role"]
  66. admin.site.register(Organization, OrganizationAdmin)
  67. admin.site.register(OrganizationUser, OrganizationUserAdmin)