utils.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from pathlib import Path
  2. from django.utils.html import format_html
  3. from core.models import Snapshot
  4. def get_icons(snapshot: Snapshot) -> str:
  5. link = snapshot.as_link()
  6. canon = link.canonical_outputs()
  7. out_dir = Path(link.link_dir)
  8. # slow version: highlights icons based on whether files exist or not for that output
  9. # link_tuple = lambda link, method: (link.archive_path, canon[method] or '', canon[method] and (out_dir / (canon[method] or 'notdone')).exists())
  10. # fast version: all icons are highlighted without checking for outputs in filesystem
  11. link_tuple = lambda link, method: (link.archive_path, canon[method] or '', canon[method] and (out_dir / (canon[method] or 'notdone')).exists())
  12. return format_html(
  13. '<span class="files-icons" style="font-size: 1.2em; opacity: 0.8">'
  14. '<a href="/{}/{}" class="exists-{}" title="SingleFile">❶ </a>'
  15. '<a href="/{}/{}" class="exists-{}" title="Wget clone">🆆 </a> '
  16. '<a href="/{}/{}" class="exists-{}" title="HTML dump">🅷 </a> '
  17. '<a href="/{}/{}" class="exists-{}" title="PDF">📄 </a> '
  18. '<a href="/{}/{}" class="exists-{}" title="Screenshot">💻 </a> '
  19. '<a href="/{}/{}" class="exists-{}" title="WARC">📦 </a> '
  20. '<a href="/{}/{}/" class="exists-{}" title="Media files">📼 </a> '
  21. '<a href="/{}/{}/" class="exists-{}" title="Git repos">🅶 </a> '
  22. '<a href="{}" class="exists-{}" title="Archive.org snapshot">🏛 </a> '
  23. '</span>',
  24. *link_tuple(link, 'singlefile_path'),
  25. *link_tuple(link, 'wget_path')[:2], any((out_dir / link.domain).glob('*')),
  26. *link_tuple(link, 'dom_path'),
  27. *link_tuple(link, 'pdf_path'),
  28. *link_tuple(link, 'screenshot_path'),
  29. *link_tuple(link, 'warc_path')[:2], any((out_dir / canon['warc_path']).glob('*.warc.gz')),
  30. *link_tuple(link, 'media_path')[:2], any((out_dir / canon['media_path']).glob('*')),
  31. *link_tuple(link, 'git_path')[:2], any((out_dir / canon['git_path']).glob('*')),
  32. canon['archive_org_path'], (out_dir / 'archive.org.txt').exists(),
  33. )