admin_users.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. __package__ = 'archivebox.core'
  2. from django.contrib import admin
  3. from django.contrib.auth.admin import UserAdmin
  4. from django.utils.html import format_html, mark_safe
  5. from django.contrib.auth import get_user_model
  6. import abx
  7. class CustomUserAdmin(UserAdmin):
  8. sort_fields = ['id', 'email', 'username', 'is_superuser', 'last_login', 'date_joined']
  9. list_display = ['username', 'id', 'email', 'is_superuser', 'last_login', 'date_joined']
  10. readonly_fields = ('snapshot_set', 'archiveresult_set', 'tag_set', 'apitoken_set', 'outboundwebhook_set')
  11. fieldsets = [*UserAdmin.fieldsets, ('Data', {'fields': readonly_fields})]
  12. @admin.display(description='Snapshots')
  13. def snapshot_set(self, obj):
  14. total_count = obj.snapshot_set.count()
  15. return mark_safe('<br/>'.join(
  16. format_html(
  17. '<code><a href="/admin/core/snapshot/{}/change"><b>[{}]</b></a></code> <b>📅 {}</b> {}',
  18. snap.pk,
  19. snap.abid,
  20. snap.downloaded_at.strftime('%Y-%m-%d %H:%M') if snap.downloaded_at else 'pending...',
  21. snap.url[:64],
  22. )
  23. for snap in obj.snapshot_set.order_by('-modified_at')[:10]
  24. ) + f'<br/><a href="/admin/core/snapshot/?created_by__id__exact={obj.pk}">{total_count} total records...<a>')
  25. @admin.display(description='Archive Result Logs')
  26. def archiveresult_set(self, obj):
  27. total_count = obj.archiveresult_set.count()
  28. return mark_safe('<br/>'.join(
  29. format_html(
  30. '<code><a href="/admin/core/archiveresult/{}/change"><b>[{}]</b></a></code> <b>📅 {}</b> <b>📄 {}</b> {}',
  31. result.pk,
  32. result.abid,
  33. result.snapshot.downloaded_at.strftime('%Y-%m-%d %H:%M') if result.snapshot.downloaded_at else 'pending...',
  34. result.extractor,
  35. result.snapshot.url[:64],
  36. )
  37. for result in obj.archiveresult_set.order_by('-modified_at')[:10]
  38. ) + f'<br/><a href="/admin/core/archiveresult/?created_by__id__exact={obj.pk}">{total_count} total records...<a>')
  39. @admin.display(description='Tags')
  40. def tag_set(self, obj):
  41. total_count = obj.tag_set.count()
  42. return mark_safe(', '.join(
  43. format_html(
  44. '<code><a href="/admin/core/tag/{}/change"><b>{}</b></a></code>',
  45. tag.pk,
  46. tag.name,
  47. )
  48. for tag in obj.tag_set.order_by('-modified_at')[:10]
  49. ) + f'<br/><a href="/admin/core/tag/?created_by__id__exact={obj.pk}">{total_count} total records...<a>')
  50. @admin.display(description='API Tokens')
  51. def apitoken_set(self, obj):
  52. total_count = obj.apitoken_set.count()
  53. return mark_safe('<br/>'.join(
  54. format_html(
  55. '<code><a href="/admin/api/apitoken/{}/change"><b>[{}]</b></a></code> {} (expires {})',
  56. apitoken.pk,
  57. apitoken.abid,
  58. apitoken.token_redacted[:64],
  59. apitoken.expires,
  60. )
  61. for apitoken in obj.apitoken_set.order_by('-modified_at')[:10]
  62. ) + f'<br/><a href="/admin/api/apitoken/?created_by__id__exact={obj.pk}">{total_count} total records...<a>')
  63. @admin.display(description='API Outbound Webhooks')
  64. def outboundwebhook_set(self, obj):
  65. total_count = obj.outboundwebhook_set.count()
  66. return mark_safe('<br/>'.join(
  67. format_html(
  68. '<code><a href="/admin/api/outboundwebhook/{}/change"><b>[{}]</b></a></code> {} -> {}',
  69. outboundwebhook.pk,
  70. outboundwebhook.abid,
  71. outboundwebhook.referenced_model,
  72. outboundwebhook.endpoint,
  73. )
  74. for outboundwebhook in obj.outboundwebhook_set.order_by('-modified_at')[:10]
  75. ) + f'<br/><a href="/admin/api/outboundwebhook/?created_by__id__exact={obj.pk}">{total_count} total records...<a>')
  76. @abx.hookimpl
  77. def register_admin(admin_site):
  78. admin_site.register(get_user_model(), CustomUserAdmin)