admin.py 3.0 KB

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