admin.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. __package__ = 'archivebox.core'
  2. import os
  3. import json
  4. from io import StringIO
  5. from pathlib import Path
  6. from contextlib import redirect_stdout
  7. from datetime import datetime, timezone
  8. from django.contrib import admin
  9. from django.db.models import Count, Q
  10. from django.urls import path, reverse
  11. from django.utils.html import format_html
  12. from django.utils.safestring import mark_safe
  13. from django.shortcuts import render, redirect
  14. from django.contrib.auth import get_user_model
  15. from django.core.exceptions import ValidationError
  16. from django import forms
  17. from signal_webhooks.admin import WebhookAdmin, get_webhook_model
  18. # from plugantic.admin import CustomPlugin
  19. from ..util import htmldecode, urldecode, ansi_to_html
  20. from core.models import Snapshot, ArchiveResult, Tag, SnapshotTag
  21. from core.forms import AddLinkForm
  22. from core.mixins import SearchResultsAdminMixin
  23. from api.models import APIToken
  24. from index.html import snapshot_icons
  25. from logging_util import printable_filesize
  26. from main import add, remove
  27. from extractors import archive_links
  28. from config import (
  29. OUTPUT_DIR,
  30. SNAPSHOTS_PER_PAGE,
  31. VERSION,
  32. VERSIONS_AVAILABLE,
  33. CAN_UPGRADE
  34. )
  35. GLOBAL_CONTEXT = {'VERSION': VERSION, 'VERSIONS_AVAILABLE': VERSIONS_AVAILABLE, 'CAN_UPGRADE': CAN_UPGRADE}
  36. # Admin URLs
  37. # /admin/
  38. # /admin/login/
  39. # /admin/core/
  40. # /admin/core/snapshot/
  41. # /admin/core/snapshot/:uuid/
  42. # /admin/core/tag/
  43. # /admin/core/tag/:uuid/
  44. # TODO: https://stackoverflow.com/questions/40760880/add-custom-button-to-django-admin-panel
  45. class ArchiveBoxAdmin(admin.AdminSite):
  46. site_header = 'ArchiveBox'
  47. index_title = 'Links'
  48. site_title = 'Index'
  49. namespace = 'admin'
  50. def get_urls(self):
  51. return [
  52. path('core/snapshot/add/', self.add_view, name='Add'),
  53. ] + super().get_urls()
  54. def add_view(self, request):
  55. if not request.user.is_authenticated:
  56. return redirect(f'/admin/login/?next={request.path}')
  57. request.current_app = self.name
  58. context = {
  59. **self.each_context(request),
  60. 'title': 'Add URLs',
  61. }
  62. if request.method == 'GET':
  63. context['form'] = AddLinkForm()
  64. elif request.method == 'POST':
  65. form = AddLinkForm(request.POST)
  66. if form.is_valid():
  67. url = form.cleaned_data["url"]
  68. print(f'[+] Adding URL: {url}')
  69. depth = 0 if form.cleaned_data["depth"] == "0" else 1
  70. input_kwargs = {
  71. "urls": url,
  72. "depth": depth,
  73. "update_all": False,
  74. "out_dir": OUTPUT_DIR,
  75. }
  76. add_stdout = StringIO()
  77. with redirect_stdout(add_stdout):
  78. add(**input_kwargs)
  79. print(add_stdout.getvalue())
  80. context.update({
  81. "stdout": ansi_to_html(add_stdout.getvalue().strip()),
  82. "form": AddLinkForm()
  83. })
  84. else:
  85. context["form"] = form
  86. return render(template_name='add.html', request=request, context=context)
  87. archivebox_admin = ArchiveBoxAdmin()
  88. archivebox_admin.register(get_user_model())
  89. archivebox_admin.register(APIToken)
  90. archivebox_admin.register(get_webhook_model(), WebhookAdmin)
  91. archivebox_admin.disable_action('delete_selected')
  92. # archivebox_admin.register(CustomPlugin)
  93. # patch admin with methods to add data views (implemented by admin_data_views package)
  94. ############### Additional sections are defined in settings.ADMIN_DATA_VIEWS #########
  95. from admin_data_views.admin import get_app_list, admin_data_index_view, get_admin_data_urls, get_urls
  96. archivebox_admin.get_app_list = get_app_list.__get__(archivebox_admin, ArchiveBoxAdmin)
  97. archivebox_admin.admin_data_index_view = admin_data_index_view.__get__(archivebox_admin, ArchiveBoxAdmin)
  98. archivebox_admin.get_admin_data_urls = get_admin_data_urls.__get__(archivebox_admin, ArchiveBoxAdmin)
  99. archivebox_admin.get_urls = get_urls(archivebox_admin.get_urls).__get__(archivebox_admin, ArchiveBoxAdmin)
  100. class ArchiveResultInline(admin.TabularInline):
  101. name = 'Archive Results Log'
  102. model = ArchiveResult
  103. # fk_name = 'snapshot'
  104. extra = 1
  105. readonly_fields = ('result_id', 'start_ts', 'end_ts', 'extractor', 'command', 'cmd_version')
  106. fields = ('id', *readonly_fields, 'status', 'output')
  107. show_change_link = True
  108. # # classes = ['collapse']
  109. # # list_display_links = ['abid']
  110. def result_id(self, obj):
  111. return format_html('<a href="{}"><small><code>[{}]</code></small></a>', reverse('admin:core_archiveresult_change', args=(obj.id,)), obj.abid)
  112. def command(self, obj):
  113. return format_html('<small><code>{}</code></small>', " ".join(obj.cmd or []))
  114. class TagInline(admin.TabularInline):
  115. model = Tag.snapshot_set.through
  116. # fk_name = 'snapshot'
  117. fields = ('id', 'tag')
  118. extra = 1
  119. # min_num = 1
  120. max_num = 1000
  121. autocomplete_fields = (
  122. 'tag',
  123. )
  124. from django.contrib.admin.helpers import ActionForm
  125. from django.contrib.admin.widgets import FilteredSelectMultiple
  126. # class AutocompleteTags:
  127. # model = Tag
  128. # search_fields = ['name']
  129. # name = 'name'
  130. # # source_field = 'name'
  131. # remote_field = Tag._meta.get_field('name')
  132. # class AutocompleteTagsAdminStub:
  133. # name = 'admin'
  134. class SnapshotActionForm(ActionForm):
  135. tags = forms.ModelMultipleChoiceField(
  136. queryset=Tag.objects.all(),
  137. required=False,
  138. widget=FilteredSelectMultiple(
  139. 'core_tag__name',
  140. False,
  141. ),
  142. )
  143. # TODO: allow selecting actions for specific extractors? is this useful?
  144. # EXTRACTOR_CHOICES = [
  145. # (name, name.title())
  146. # for name, _, _ in get_default_archive_methods()
  147. # ]
  148. # extractor = forms.ChoiceField(
  149. # choices=EXTRACTOR_CHOICES,
  150. # required=False,
  151. # widget=forms.MultileChoiceField(attrs={'class': "form-control"})
  152. # )
  153. def get_abid_info(self, obj):
  154. return format_html(
  155. # URL Hash: <code style="font-size: 10px; user-select: all">{}</code><br/>
  156. '''
  157. <a href="{}" style="font-size: 16px; font-family: monospace; user-select: all; border-radius: 8px; background-color: #ddf; padding: 3px 5px; border: 1px solid #aaa; margin-bottom: 8px; display: inline-block; vertical-align: top;">{}</a> &nbsp; &nbsp; <a href="{}" style="color: limegreen; font-size: 0.9em; vertical-align: 1px; font-family: monospace;">📖 API DOCS</a>
  158. <br/><hr/>
  159. <div style="opacity: 0.8">
  160. &nbsp; &nbsp; <small style="opacity: 0.8">.abid: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <code style="font-size: 10px; user-select: all">{}</code></small><br/>
  161. &nbsp; &nbsp; <small style="opacity: 0.8">.abid.uuid: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <code style="font-size: 10px; user-select: all">{}</code></small><br/>
  162. &nbsp; &nbsp; <small style="opacity: 0.8">.id: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<code style="font-size: 10px; user-select: all">{}</code></small><br/>
  163. <hr/>
  164. &nbsp; &nbsp; TS: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<code style="font-size: 10px;"><b style="user-select: all">{}</b> &nbsp; {}</code> &nbsp; &nbsp; &nbsp;&nbsp; {}: <code style="user-select: all">{}</code><br/>
  165. &nbsp; &nbsp; URI: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <code style="font-size: 10px; "><b style="user-select: all">{}</b> &nbsp; &nbsp; {}</code> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <span style="display:inline-block; vertical-align: -4px; width: 290px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{}: <code style="user-select: all">{}</code></span>
  166. &nbsp; SALT: &nbsp; <code style="font-size: 10px;"><b style="display:inline-block; user-select: all; width: 50px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{}</b></code><br/>
  167. &nbsp; &nbsp; SUBTYPE: &nbsp; &nbsp; &nbsp; <code style="font-size: 10px;"><b style="user-select: all">{}</b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {}</code> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {}: <code style="user-select: all">{}</code><br/>
  168. &nbsp; &nbsp; RAND: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <code style="font-size: 10px;"><b style="user-select: all">{}</b> &nbsp; &nbsp; &nbsp; {}</code> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {}: <code style="user-select: all">{}</code>
  169. <br/><hr/>
  170. &nbsp; &nbsp; <small style="opacity: 0.5">.old_id: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<code style="font-size: 10px; user-select: all">{}</code></small><br/>
  171. </div>
  172. ''',
  173. obj.api_url, obj.api_url, obj.api_docs_url,
  174. str(obj.abid),
  175. str(obj.ABID.uuid),
  176. str(obj.id),
  177. obj.ABID.ts, str(obj.ABID.uuid)[0:14], obj.abid_ts_src, obj.abid_values['ts'].isoformat() if isinstance(obj.abid_values['ts'], datetime) else obj.abid_values['ts'],
  178. obj.ABID.uri, str(obj.ABID.uuid)[14:26], obj.abid_uri_src, str(obj.abid_values['uri']),
  179. obj.ABID.uri_salt,
  180. obj.ABID.subtype, str(obj.ABID.uuid)[26:28], obj.abid_subtype_src, str(obj.abid_values['subtype']),
  181. obj.ABID.rand, str(obj.ABID.uuid)[28:36], obj.abid_rand_src, str(obj.abid_values['rand'])[-7:],
  182. str(getattr(obj, 'old_id', '')),
  183. )
  184. @admin.register(Snapshot, site=archivebox_admin)
  185. class SnapshotAdmin(SearchResultsAdminMixin, admin.ModelAdmin):
  186. class Meta:
  187. model = Snapshot
  188. list_display = ('added', 'title_str', 'files', 'size', 'url_str')
  189. # list_editable = ('title',)
  190. sort_fields = ('title_str', 'url_str', 'added', 'files')
  191. readonly_fields = ('tags', 'timestamp', 'admin_actions', 'status_info', 'bookmarked', 'added', 'updated', 'created', 'modified', 'API', 'link_dir')
  192. search_fields = ('id', 'url', 'abid', 'old_id', 'timestamp', 'title', 'tags__name')
  193. list_filter = ('added', 'updated', 'archiveresult__status', 'created_by', 'tags')
  194. fields = ('url', 'created_by', 'title', *readonly_fields)
  195. ordering = ['-added']
  196. actions = ['add_tags', 'remove_tags', 'update_titles', 'update_snapshots', 'resnapshot_snapshot', 'overwrite_snapshots', 'delete_snapshots']
  197. autocomplete_fields = ['tags']
  198. inlines = [TagInline, ArchiveResultInline]
  199. list_per_page = SNAPSHOTS_PER_PAGE
  200. action_form = SnapshotActionForm
  201. save_on_top = True
  202. def changelist_view(self, request, extra_context=None):
  203. extra_context = extra_context or {}
  204. try:
  205. return super().changelist_view(request, extra_context | GLOBAL_CONTEXT)
  206. except Exception as e:
  207. self.message_user(request, f'Error occurred while loading the page: {str(e)} {request.GET} {request.POST}')
  208. return super().changelist_view(request, GLOBAL_CONTEXT)
  209. def change_view(self, request, object_id, form_url="", extra_context=None):
  210. snapshot = None
  211. try:
  212. snapshot = snapshot or Snapshot.objects.get(id=object_id)
  213. except (Snapshot.DoesNotExist, Snapshot.MultipleObjectsReturned, ValidationError):
  214. pass
  215. try:
  216. snapshot = snapshot or Snapshot.objects.get(abid=Snapshot.abid_prefix + object_id.split('_', 1)[-1])
  217. except (Snapshot.DoesNotExist, ValidationError):
  218. pass
  219. try:
  220. snapshot = snapshot or Snapshot.objects.get(old_id=object_id)
  221. except (Snapshot.DoesNotExist, Snapshot.MultipleObjectsReturned, ValidationError):
  222. pass
  223. if snapshot:
  224. object_id = str(snapshot.id)
  225. return super().change_view(
  226. request,
  227. object_id,
  228. form_url,
  229. extra_context=extra_context,
  230. )
  231. def get_urls(self):
  232. urls = super().get_urls()
  233. custom_urls = [
  234. path('grid/', self.admin_site.admin_view(self.grid_view), name='grid')
  235. ]
  236. return custom_urls + urls
  237. def get_queryset(self, request):
  238. self.request = request
  239. return super().get_queryset(request).prefetch_related('tags', 'archiveresult_set').annotate(archiveresult_count=Count('archiveresult'))
  240. def tag_list(self, obj):
  241. return ', '.join(obj.tags.values_list('name', flat=True))
  242. # TODO: figure out a different way to do this, you cant nest forms so this doenst work
  243. # def action(self, obj):
  244. # # csrfmiddlewaretoken: Wa8UcQ4fD3FJibzxqHN3IYrrjLo4VguWynmbzzcPYoebfVUnDovon7GEMYFRgsh0
  245. # # action: update_snapshots
  246. # # select_across: 0
  247. # # _selected_action: 76d29b26-2a88-439e-877c-a7cca1b72bb3
  248. # return format_html(
  249. # '''
  250. # <form action="/admin/core/snapshot/" method="post" onsubmit="e => e.stopPropagation()">
  251. # <input type="hidden" name="csrfmiddlewaretoken" value="{}">
  252. # <input type="hidden" name="_selected_action" value="{}">
  253. # <button name="update_snapshots">Check</button>
  254. # <button name="update_titles">Pull title + favicon</button>
  255. # <button name="update_snapshots">Update</button>
  256. # <button name="overwrite_snapshots">Re-Archive (overwrite)</button>
  257. # <button name="delete_snapshots">Permanently delete</button>
  258. # </form>
  259. # ''',
  260. # csrf.get_token(self.request),
  261. # obj.pk,
  262. # )
  263. def admin_actions(self, obj):
  264. return format_html(
  265. # URL Hash: <code style="font-size: 10px; user-select: all">{}</code><br/>
  266. '''
  267. <a class="btn" style="font-size: 18px; display: inline-block; border-radius: 10px; border: 3px solid #eee; padding: 4px 8px" href="/archive/{}">Summary page ➡️</a> &nbsp; &nbsp;
  268. <a class="btn" style="font-size: 18px; display: inline-block; border-radius: 10px; border: 3px solid #eee; padding: 4px 8px" href="/archive/{}/index.html#all">Result files 📑</a> &nbsp; &nbsp;
  269. <a class="btn" style="font-size: 18px; display: inline-block; border-radius: 10px; border: 3px solid #eee; padding: 4px 8px" href="/admin/core/snapshot/?id__exact={}">Admin actions ⚙️</a>
  270. ''',
  271. obj.timestamp,
  272. obj.timestamp,
  273. obj.pk,
  274. )
  275. def status_info(self, obj):
  276. return format_html(
  277. # URL Hash: <code style="font-size: 10px; user-select: all">{}</code><br/>
  278. '''
  279. Archived: {} ({} files {}) &nbsp; &nbsp;
  280. Favicon: <img src="{}" style="height: 20px"/> &nbsp; &nbsp;
  281. Status code: {} &nbsp; &nbsp;<br/>
  282. Server: {} &nbsp; &nbsp;
  283. Content type: {} &nbsp; &nbsp;
  284. Extension: {} &nbsp; &nbsp;
  285. ''',
  286. '✅' if obj.is_archived else '❌',
  287. obj.num_outputs,
  288. self.size(obj) or '0kb',
  289. f'/archive/{obj.timestamp}/favicon.ico',
  290. obj.status_code or '-',
  291. obj.headers and obj.headers.get('Server') or '-',
  292. obj.headers and obj.headers.get('Content-Type') or '-',
  293. obj.extension or '-',
  294. )
  295. def API(self, obj):
  296. try:
  297. return get_abid_info(self, obj)
  298. except Exception as e:
  299. return str(e)
  300. @admin.display(
  301. description='Title',
  302. ordering='title',
  303. )
  304. def title_str(self, obj):
  305. canon = obj.as_link().canonical_outputs()
  306. tags = ''.join(
  307. format_html('<a href="/admin/core/snapshot/?tags__id__exact={}"><span class="tag">{}</span></a> ', tag.id, tag)
  308. for tag in obj.tags.all()
  309. if str(tag).strip()
  310. )
  311. return format_html(
  312. '<a href="/{}">'
  313. '<img src="/{}/{}" class="favicon" onerror="this.remove()">'
  314. '</a>'
  315. '<a href="/{}/index.html">'
  316. '<b class="status-{}">{}</b>'
  317. '</a>',
  318. obj.archive_path,
  319. obj.archive_path, canon['favicon_path'],
  320. obj.archive_path,
  321. 'fetched' if obj.latest_title or obj.title else 'pending',
  322. urldecode(htmldecode(obj.latest_title or obj.title or ''))[:128] or 'Pending...'
  323. ) + mark_safe(f' <span class="tags">{tags}</span>')
  324. @admin.display(
  325. description='Files Saved',
  326. ordering='archiveresult_count',
  327. )
  328. def files(self, obj):
  329. return snapshot_icons(obj)
  330. @admin.display(
  331. ordering='archiveresult_count'
  332. )
  333. def size(self, obj):
  334. archive_size = (Path(obj.link_dir) / 'index.html').exists() and obj.archive_size
  335. if archive_size:
  336. size_txt = printable_filesize(archive_size)
  337. if archive_size > 52428800:
  338. size_txt = mark_safe(f'<b>{size_txt}</b>')
  339. else:
  340. size_txt = mark_safe('<span style="opacity: 0.3">...</span>')
  341. return format_html(
  342. '<a href="/{}" title="View all files">{}</a>',
  343. obj.archive_path,
  344. size_txt,
  345. )
  346. @admin.display(
  347. description='Original URL',
  348. ordering='url',
  349. )
  350. def url_str(self, obj):
  351. return format_html(
  352. '<a href="{}"><code style="user-select: all;">{}</code></a>',
  353. obj.url,
  354. obj.url[:128],
  355. )
  356. def grid_view(self, request, extra_context=None):
  357. # cl = self.get_changelist_instance(request)
  358. # Save before monkey patching to restore for changelist list view
  359. saved_change_list_template = self.change_list_template
  360. saved_list_per_page = self.list_per_page
  361. saved_list_max_show_all = self.list_max_show_all
  362. # Monkey patch here plus core_tags.py
  363. self.change_list_template = 'private_index_grid.html'
  364. self.list_per_page = SNAPSHOTS_PER_PAGE
  365. self.list_max_show_all = self.list_per_page
  366. # Call monkey patched view
  367. rendered_response = self.changelist_view(request, extra_context=extra_context)
  368. # Restore values
  369. self.change_list_template = saved_change_list_template
  370. self.list_per_page = saved_list_per_page
  371. self.list_max_show_all = saved_list_max_show_all
  372. return rendered_response
  373. # for debugging, uncomment this to print all requests:
  374. # def changelist_view(self, request, extra_context=None):
  375. # print('[*] Got request', request.method, request.POST)
  376. # return super().changelist_view(request, extra_context=None)
  377. @admin.action(
  378. description="Pull"
  379. )
  380. def update_snapshots(self, request, queryset):
  381. archive_links([
  382. snapshot.as_link()
  383. for snapshot in queryset
  384. ], out_dir=OUTPUT_DIR)
  385. @admin.action(
  386. description="⬇️ Title"
  387. )
  388. def update_titles(self, request, queryset):
  389. archive_links([
  390. snapshot.as_link()
  391. for snapshot in queryset
  392. ], overwrite=True, methods=('title','favicon'), out_dir=OUTPUT_DIR)
  393. @admin.action(
  394. description="Re-Snapshot"
  395. )
  396. def resnapshot_snapshot(self, request, queryset):
  397. for snapshot in queryset:
  398. timestamp = datetime.now(timezone.utc).isoformat('T', 'seconds')
  399. new_url = snapshot.url.split('#')[0] + f'#{timestamp}'
  400. add(new_url, tag=snapshot.tags_str())
  401. @admin.action(
  402. description="Reset"
  403. )
  404. def overwrite_snapshots(self, request, queryset):
  405. archive_links([
  406. snapshot.as_link()
  407. for snapshot in queryset
  408. ], overwrite=True, out_dir=OUTPUT_DIR)
  409. @admin.action(
  410. description="Delete"
  411. )
  412. def delete_snapshots(self, request, queryset):
  413. remove(snapshots=queryset, yes=True, delete=True, out_dir=OUTPUT_DIR)
  414. @admin.action(
  415. description="+"
  416. )
  417. def add_tags(self, request, queryset):
  418. tags = request.POST.getlist('tags')
  419. print('[+] Adding tags', tags, 'to Snapshots', queryset)
  420. for obj in queryset:
  421. obj.tags.add(*tags)
  422. @admin.action(
  423. description="–"
  424. )
  425. def remove_tags(self, request, queryset):
  426. tags = request.POST.getlist('tags')
  427. print('[-] Removing tags', tags, 'to Snapshots', queryset)
  428. for obj in queryset:
  429. obj.tags.remove(*tags)
  430. # @admin.register(SnapshotTag, site=archivebox_admin)
  431. # class SnapshotTagAdmin(admin.ModelAdmin):
  432. # list_display = ('id', 'snapshot', 'tag')
  433. # sort_fields = ('id', 'snapshot', 'tag')
  434. # search_fields = ('id', 'snapshot_id', 'tag_id')
  435. # fields = ('snapshot', 'id')
  436. # actions = ['delete_selected']
  437. # ordering = ['-id']
  438. # def API(self, obj):
  439. # return get_abid_info(self, obj)
  440. @admin.register(Tag, site=archivebox_admin)
  441. class TagAdmin(admin.ModelAdmin):
  442. list_display = ('abid', 'name', 'created', 'created_by', 'num_snapshots', 'snapshots')
  443. sort_fields = ('name', 'slug', 'abid', 'created_by', 'created')
  444. readonly_fields = ('slug', 'abid', 'created', 'modified', 'API', 'num_snapshots', 'snapshots')
  445. search_fields = ('abid', 'name', 'slug')
  446. fields = ('name', 'created_by', *readonly_fields)
  447. actions = ['delete_selected']
  448. ordering = ['-created']
  449. def API(self, obj):
  450. try:
  451. return get_abid_info(self, obj)
  452. except Exception as e:
  453. return str(e)
  454. def num_snapshots(self, tag):
  455. return format_html(
  456. '<a href="/admin/core/snapshot/?tags__id__exact={}">{} total</a>',
  457. tag.id,
  458. tag.snapshot_set.count(),
  459. )
  460. def snapshots(self, tag):
  461. total_count = tag.snapshot_set.count()
  462. return mark_safe('<br/>'.join(
  463. format_html(
  464. '<code><a href="/admin/core/snapshot/{}/change"><b>[{}]</b></a></code> {}',
  465. snap.pk,
  466. snap.updated.strftime('%Y-%m-%d %H:%M') if snap.updated else 'pending...',
  467. snap.url[:64],
  468. )
  469. for snap in tag.snapshot_set.order_by('-updated')[:10]
  470. ) + (f'<br/><a href="/admin/core/snapshot/?tags__id__exact={tag.id}">and {total_count-10} more...<a>' if tag.snapshot_set.count() > 10 else ''))
  471. @admin.register(ArchiveResult, site=archivebox_admin)
  472. class ArchiveResultAdmin(admin.ModelAdmin):
  473. list_display = ('start_ts', 'snapshot_info', 'tags_str', 'extractor', 'cmd_str', 'status', 'output_str')
  474. sort_fields = ('start_ts', 'extractor', 'status')
  475. readonly_fields = ('cmd_str', 'snapshot_info', 'tags_str', 'created', 'modified', 'API', 'output_summary')
  476. search_fields = ('id', 'old_id', 'abid', 'snapshot__url', 'extractor', 'output', 'cmd_version', 'cmd', 'snapshot__timestamp')
  477. fields = ('snapshot', 'extractor', 'status', 'output', 'pwd', 'start_ts', 'end_ts', 'created_by', 'cmd_version', 'cmd', *readonly_fields)
  478. autocomplete_fields = ['snapshot']
  479. list_filter = ('status', 'extractor', 'start_ts', 'cmd_version')
  480. ordering = ['-start_ts']
  481. list_per_page = SNAPSHOTS_PER_PAGE
  482. @admin.display(
  483. description='Snapshot Info'
  484. )
  485. def snapshot_info(self, result):
  486. return format_html(
  487. '<a href="/archive/{}/index.html"><b><code>[{}]</code></b> &nbsp; {} &nbsp; {}</a><br/>',
  488. result.snapshot.timestamp,
  489. result.snapshot.abid,
  490. result.snapshot.added.strftime('%Y-%m-%d %H:%M'),
  491. result.snapshot.url[:128],
  492. )
  493. def API(self, obj):
  494. try:
  495. return get_abid_info(self, obj)
  496. except Exception as e:
  497. raise e
  498. return str(e)
  499. @admin.display(
  500. description='Snapshot Tags'
  501. )
  502. def tags_str(self, result):
  503. return result.snapshot.tags_str()
  504. def cmd_str(self, result):
  505. return format_html(
  506. '<pre>{}</pre>',
  507. ' '.join(result.cmd) if isinstance(result.cmd, list) else str(result.cmd),
  508. )
  509. def output_str(self, result):
  510. return format_html(
  511. '<a href="/archive/{}/{}" class="output-link">↗️</a><pre>{}</pre>',
  512. result.snapshot.timestamp,
  513. result.output if (result.status == 'succeeded') and result.extractor not in ('title', 'archive_org') else 'index.html',
  514. result.output,
  515. )
  516. def output_summary(self, result):
  517. snapshot_dir = Path(OUTPUT_DIR) / str(result.pwd).split('data/', 1)[-1]
  518. output_str = format_html(
  519. '<pre style="display: inline-block">{}</pre><br/>',
  520. result.output,
  521. )
  522. output_str += format_html('<a href="/archive/{}/index.html#all">See result files ...</a><br/><pre><code>', str(result.snapshot.timestamp))
  523. path_from_output_str = (snapshot_dir / result.output)
  524. output_str += format_html('<i style="padding: 1px">{}</i><b style="padding-right: 20px">/</b><i>{}</i><br/><hr/>', str(snapshot_dir), str(result.output))
  525. if path_from_output_str.exists():
  526. root_dir = str(path_from_output_str)
  527. else:
  528. root_dir = str(snapshot_dir)
  529. # print(root_dir, str(list(os.walk(root_dir))))
  530. for root, dirs, files in os.walk(root_dir):
  531. depth = root.replace(root_dir, '').count(os.sep) + 1
  532. if depth > 2:
  533. continue
  534. indent = ' ' * 4 * (depth)
  535. output_str += format_html('<b style="padding: 1px">{}{}/</b><br/>', indent, os.path.basename(root))
  536. indentation_str = ' ' * 4 * (depth + 1)
  537. for filename in sorted(files):
  538. is_hidden = filename.startswith('.')
  539. output_str += format_html('<span style="opacity: {}.2">{}{}</span><br/>', int(not is_hidden), indentation_str, filename.strip())
  540. return output_str + format_html('</code></pre>')