admin.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from django.contrib import admin
  2. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  3. from django.utils.translation import gettext_lazy as _
  4. from import_export import resources
  5. from import_export.admin import ImportExportModelAdmin
  6. from .models import User, UserProjectAlert
  7. class UserResource(resources.ModelResource):
  8. class Meta:
  9. model = User
  10. skip_unchanged = True
  11. fields = (
  12. "id",
  13. # "password",
  14. "is_superuser",
  15. "email",
  16. "name",
  17. "is_staff",
  18. "is_active",
  19. "created",
  20. )
  21. class UserAdmin(BaseUserAdmin, ImportExportModelAdmin):
  22. ordering = ("email",)
  23. list_display = (
  24. "email",
  25. "name",
  26. "organizations",
  27. )
  28. fieldsets = (
  29. (
  30. None,
  31. {
  32. "fields": (
  33. "email",
  34. "password",
  35. )
  36. },
  37. ),
  38. (
  39. _("Personal info"),
  40. {"fields": ("name", "subscribe_by_default", "analytics", "options")},
  41. ),
  42. (
  43. _("Permissions"),
  44. {
  45. "fields": (
  46. "is_active",
  47. "is_staff",
  48. "is_superuser",
  49. "groups",
  50. "user_permissions",
  51. ),
  52. },
  53. ),
  54. (_("Important dates"), {"fields": ("last_login",)}),
  55. )
  56. add_fieldsets = (
  57. (
  58. None,
  59. {
  60. "classes": ("wide",),
  61. "fields": ("email", "password1", "password2"),
  62. },
  63. ),
  64. )
  65. search_fields = ("email", "name")
  66. readonly_fields = ("analytics",)
  67. resource_class = UserResource
  68. def get_queryset(self, request):
  69. return (
  70. super()
  71. .get_queryset(request)
  72. .prefetch_related("organizations_ext_organization")
  73. )
  74. def organizations(self, obj):
  75. return ", ".join([org.name for org in obj.organizations_ext_organization.all()])
  76. class UserProjectAlertAdmin(admin.ModelAdmin):
  77. list_display = ("user", "project", "status")
  78. list_filter = ("status",)
  79. search_fields = ("project__name", "user__email")
  80. raw_id_fields = (
  81. "user",
  82. "project",
  83. )
  84. admin.site.register(User, UserAdmin)
  85. admin.site.register(UserProjectAlert, UserProjectAlertAdmin)