admin.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from django.contrib import admin
  2. from django.db.models import Sum, Count
  3. from organizations.base_admin import (
  4. BaseOrganizationAdmin,
  5. BaseOrganizationUserAdmin,
  6. BaseOwnerInline,
  7. )
  8. from .models import Organization, OrganizationUser, OrganizationOwner
  9. class OwnerInline(BaseOwnerInline):
  10. model = OrganizationOwner
  11. class OrganizationUserInline(admin.StackedInline):
  12. raw_id_fields = ("user",)
  13. model = OrganizationUser
  14. extra = 0
  15. class OrganizationAdmin(BaseOrganizationAdmin):
  16. list_per_page = 50
  17. list_display = [
  18. "name",
  19. "is_active",
  20. "is_accepting_events",
  21. "issue_events",
  22. "transaction_events",
  23. "total_events",
  24. ]
  25. list_filter = ("is_active", "is_accepting_events")
  26. inlines = [OrganizationUserInline, OwnerInline]
  27. show_full_result_count = False
  28. def issue_events(self, obj):
  29. return obj.issue_events
  30. def transaction_events(self, obj):
  31. return obj.transaction_events
  32. def total_events(self, obj):
  33. total = 0
  34. issue = self.issue_events(obj)
  35. if issue:
  36. total += issue
  37. total += self.transaction_events(obj)
  38. return total
  39. def get_queryset(self, request):
  40. queryset = super().get_queryset(request)
  41. queryset = queryset.annotate(
  42. issue_events=Sum("projects__issue__count"),
  43. transaction_events=Count("projects__transactionevent"),
  44. )
  45. return queryset
  46. class OrganizationUserAdmin(BaseOrganizationUserAdmin):
  47. list_display = ["user", "organization", "role"]
  48. admin.site.register(Organization, OrganizationAdmin)
  49. admin.site.register(OrganizationUser, OrganizationUserAdmin)