admin.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from allauth.account.decorators import secure_admin_login
  2. from django.contrib import admin
  3. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  4. from django.db.models.functions import Collate
  5. from django.utils.translation import gettext_lazy as _
  6. from import_export.admin import ImportExportModelAdmin
  7. from .models import User
  8. from .resources import UserResource
  9. class UserAdmin(BaseUserAdmin, ImportExportModelAdmin):
  10. ordering = ("email",)
  11. list_display = (
  12. "email",
  13. "name",
  14. "organizations",
  15. )
  16. fieldsets = (
  17. (
  18. None,
  19. {
  20. "fields": (
  21. "email",
  22. "password",
  23. )
  24. },
  25. ),
  26. (
  27. _("Personal info"),
  28. {"fields": ("name", "subscribe_by_default", "analytics", "options")},
  29. ),
  30. (
  31. _("Permissions"),
  32. {
  33. "fields": (
  34. "is_active",
  35. "is_staff",
  36. "is_superuser",
  37. "groups",
  38. "user_permissions",
  39. ),
  40. },
  41. ),
  42. (_("Important dates"), {"fields": ("last_login",)}),
  43. )
  44. add_fieldsets = (
  45. (
  46. None,
  47. {
  48. "classes": ("wide",),
  49. "fields": ("email", "password1", "password2"),
  50. },
  51. ),
  52. )
  53. search_fields = ("email_deterministic", "name")
  54. readonly_fields = ("analytics",)
  55. resource_class = UserResource
  56. def get_queryset(self, request):
  57. return (
  58. super()
  59. .get_queryset(request)
  60. .annotate(email_deterministic=Collate("email", "und-x-icu"))
  61. .prefetch_related("organizations_ext_organization")
  62. )
  63. def organizations(self, obj):
  64. return ", ".join([org.name for org in obj.organizations_ext_organization.all()])
  65. admin.site.register(User, UserAdmin)
  66. admin.site.login = secure_admin_login(admin.site.login)