admin.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from django.contrib import admin
  2. from django.utils.html import format_html
  3. from core.models import Snapshot
  4. from cli.logging import printable_filesize
  5. # TODO: https://stackoverflow.com/questions/40760880/add-custom-button-to-django-admin-panel
  6. class SnapshotAdmin(admin.ModelAdmin):
  7. list_display = ('id_str', 'title_str', 'url_str', 'tags', 'files', 'added', 'updated', 'timestamp')
  8. # sort_fields = ('id_str', 'files', 'url_str', 'title_str', 'tags', 'added', 'updated', 'timestamp')
  9. readonly_fields = ('id', 'num_outputs', 'is_archived', 'url_hash', 'added', 'updated')
  10. search_fields = ('url', 'timestamp', 'title', 'tags')
  11. fields = ('url', 'timestamp', 'title', 'tags', *readonly_fields)
  12. list_filter = ('added', 'updated', 'tags')
  13. ordering = ['-added']
  14. def id_str(self, obj):
  15. return format_html(
  16. '<code style="font-size: 10px">{}</code>',
  17. obj.url_hash[:8],
  18. )
  19. def title_str(self, obj):
  20. canon = obj.as_link().canonical_outputs()
  21. return format_html(
  22. '<a href="/{}">'
  23. '<img src="/{}/{}" style="height: 20px; width: 20px;" onerror="this.style.opacity=0">'
  24. '</a>'
  25. '<a href="/{}/{}">'
  26. ' &nbsp; &nbsp; <b>{}</b></a>',
  27. obj.archive_path,
  28. obj.archive_path, canon['favicon_path'],
  29. obj.archive_path, canon['wget_path'] or '',
  30. (obj.title or '...')[:128],
  31. )
  32. def files(self, obj):
  33. canon = obj.as_link().canonical_outputs()
  34. return format_html(
  35. '<span style="font-size: 1.2em; opacity: 0.8">'
  36. '<a href="/{}/{}">🌐 </a> '
  37. '<a href="/{}/{}">📄</a> '
  38. '<a href="/{}/{}">🖥 </a> '
  39. '<a href="/{}/{}">🅷 </a> '
  40. '<a href="/{}/{}">📼 </a> '
  41. '<a href="/{}/{}">📦 </a> '
  42. '<a href="/{}/{}">🏛 </a> '
  43. '</span>'
  44. '<a href="/{}">{}</a>',
  45. obj.archive_path, canon['wget_path'] or '',
  46. obj.archive_path, canon['pdf_path'],
  47. obj.archive_path, canon['screenshot_path'],
  48. obj.archive_path, canon['dom_path'],
  49. obj.archive_path, canon['media_path'],
  50. obj.archive_path, canon['git_path'],
  51. obj.archive_path, canon['archive_org_path'],
  52. obj.archive_path,
  53. printable_filesize(obj.archive_size),
  54. )
  55. def url_str(self, obj):
  56. return format_html(
  57. '<a href="{}"><code>{}</code></a>',
  58. obj.url,
  59. obj.url.split('://', 1)[-1][:128],
  60. )
  61. id_str.short_description = 'ID'
  62. title_str.short_description = 'Title'
  63. url_str.short_description = 'URL'
  64. id_str.admin_order_field = 'id'
  65. title_str.admin_order_field = 'title'
  66. url_str.admin_order_field = 'url'
  67. admin.site.register(Snapshot, SnapshotAdmin)