2
0

admin.py 2.9 KB

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