admin.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.admin import ImportExportModelAdmin
  5. from .models import User
  6. from .resources import UserResource
  7. class UserAdmin(BaseUserAdmin, ImportExportModelAdmin):
  8. ordering = ("email",)
  9. list_display = (
  10. "email",
  11. "name",
  12. "organizations",
  13. )
  14. fieldsets = (
  15. (
  16. None,
  17. {
  18. "fields": (
  19. "email",
  20. "password",
  21. )
  22. },
  23. ),
  24. (
  25. _("Personal info"),
  26. {"fields": ("name", "subscribe_by_default", "analytics", "options")},
  27. ),
  28. (
  29. _("Permissions"),
  30. {
  31. "fields": (
  32. "is_active",
  33. "is_staff",
  34. "is_superuser",
  35. "groups",
  36. "user_permissions",
  37. ),
  38. },
  39. ),
  40. (_("Important dates"), {"fields": ("last_login",)}),
  41. )
  42. add_fieldsets = (
  43. (
  44. None,
  45. {
  46. "classes": ("wide",),
  47. "fields": ("email", "password1", "password2"),
  48. },
  49. ),
  50. )
  51. search_fields = ("email", "name")
  52. readonly_fields = ("analytics",)
  53. resource_class = UserResource
  54. def get_queryset(self, request):
  55. return (
  56. super()
  57. .get_queryset(request)
  58. .prefetch_related("organizations_ext_organization")
  59. )
  60. def organizations(self, obj):
  61. return ", ".join([org.name for org in obj.organizations_ext_organization.all()])
  62. admin.site.register(User, UserAdmin)