admin.py 1.9 KB

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