views.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. __package__ = 'archivebox.core'
  2. import os
  3. import inspect
  4. from typing import Callable, get_type_hints
  5. from pathlib import Path
  6. from django.shortcuts import render, redirect
  7. from django.http import HttpRequest, HttpResponse, Http404
  8. from django.utils.html import format_html, mark_safe
  9. from django.views import View
  10. from django.views.generic.list import ListView
  11. from django.views.generic import FormView
  12. from django.db.models import Q
  13. from django.contrib import messages
  14. from django.contrib.auth.mixins import UserPassesTestMixin
  15. from django.views.decorators.csrf import csrf_exempt
  16. from django.utils.decorators import method_decorator
  17. from admin_data_views.typing import TableContext, ItemContext
  18. from admin_data_views.utils import render_with_table_view, render_with_item_view, ItemLink
  19. import archivebox
  20. from core.models import Snapshot
  21. from core.forms import AddLinkForm
  22. from queues.tasks import bg_add
  23. from archivebox.config import CONSTANTS_CONFIG, DATA_DIR, VERSION
  24. from archivebox.config.common import SHELL_CONFIG, SERVER_CONFIG
  25. from archivebox.misc.util import base_url, htmlencode, ts_to_date_str
  26. from archivebox.misc.serve_static import serve_static_with_byterange_support
  27. from archivebox.logging_util import printable_filesize
  28. from archivebox.search import query_search_index
  29. class HomepageView(View):
  30. def get(self, request):
  31. if request.user.is_authenticated:
  32. return redirect('/admin/core/snapshot/')
  33. if SERVER_CONFIG.PUBLIC_INDEX:
  34. return redirect('/public')
  35. return redirect(f'/admin/login/?next={request.path}')
  36. class SnapshotView(View):
  37. # render static html index from filesystem archive/<timestamp>/index.html
  38. @staticmethod
  39. def render_live_index(request, snapshot):
  40. TITLE_LOADING_MSG = 'Not yet archived...'
  41. HIDDEN_RESULTS = ('favicon', 'headers', 'title', 'htmltotext', 'warc', 'archive_org')
  42. archiveresults = {}
  43. results = snapshot.archiveresult_set.all()
  44. for result in results:
  45. embed_path = result.embed_path()
  46. abs_path = result.snapshot_dir / (embed_path or 'None')
  47. if (result.status == 'succeeded'
  48. and (result.extractor not in HIDDEN_RESULTS)
  49. and embed_path
  50. and os.access(abs_path, os.R_OK)
  51. and abs_path.exists()):
  52. if os.path.isdir(abs_path) and not any(abs_path.glob('*.*')):
  53. continue
  54. result_info = {
  55. 'name': result.extractor,
  56. 'path': embed_path,
  57. 'ts': ts_to_date_str(result.end_ts),
  58. 'size': abs_path.stat().st_size or '?',
  59. }
  60. archiveresults[result.extractor] = result_info
  61. existing_files = {result['path'] for result in archiveresults.values()}
  62. min_size_threshold = 10_000 # bytes
  63. allowed_extensions = {
  64. 'txt',
  65. 'html',
  66. 'htm',
  67. 'png',
  68. 'jpg',
  69. 'jpeg',
  70. 'gif',
  71. 'webp'
  72. 'svg',
  73. 'webm',
  74. 'mp4',
  75. 'mp3',
  76. 'opus',
  77. 'pdf',
  78. 'md',
  79. }
  80. # iterate through all the files in the snapshot dir and add the biggest ones to1 the result list
  81. snap_dir = Path(snapshot.link_dir)
  82. assert os.path.isdir(snap_dir) and os.access(snap_dir, os.R_OK)
  83. for result_file in (*snap_dir.glob('*'), *snap_dir.glob('*/*')):
  84. extension = result_file.suffix.lstrip('.').lower()
  85. if result_file.is_dir() or result_file.name.startswith('.') or extension not in allowed_extensions:
  86. continue
  87. if result_file.name in existing_files or result_file.name == 'index.html':
  88. continue
  89. file_size = result_file.stat().st_size or 0
  90. if file_size > min_size_threshold:
  91. archiveresults[result_file.name] = {
  92. 'name': result_file.stem,
  93. 'path': result_file.relative_to(snap_dir),
  94. 'ts': ts_to_date_str(result_file.stat().st_mtime or 0),
  95. 'size': file_size,
  96. }
  97. preferred_types = ('singlefile', 'screenshot', 'wget', 'dom', 'media', 'pdf', 'readability', 'mercury')
  98. all_types = preferred_types + tuple(result_type for result_type in archiveresults.keys() if result_type not in preferred_types)
  99. best_result = {'path': 'None'}
  100. for result_type in preferred_types:
  101. if result_type in archiveresults:
  102. best_result = archiveresults[result_type]
  103. break
  104. link = snapshot.as_link()
  105. link_info = link._asdict(extended=True)
  106. try:
  107. warc_path = 'warc/' + list(Path(snap_dir).glob('warc/*.warc.*'))[0].name
  108. except IndexError:
  109. warc_path = 'warc/'
  110. context = {
  111. **link_info,
  112. **link_info['canonical'],
  113. 'title': htmlencode(
  114. link.title
  115. or (link.base_url if link.is_archived else TITLE_LOADING_MSG)
  116. ),
  117. 'extension': link.extension or 'html',
  118. 'tags': link.tags or 'untagged',
  119. 'size': printable_filesize(link.archive_size) if link.archive_size else 'pending',
  120. 'status': 'archived' if link.is_archived else 'not yet archived',
  121. 'status_color': 'success' if link.is_archived else 'danger',
  122. 'oldest_archive_date': ts_to_date_str(link.oldest_archive_date),
  123. 'warc_path': warc_path,
  124. 'SAVE_ARCHIVE_DOT_ORG': archivebox.pm.hook.get_FLAT_CONFIG().SAVE_ARCHIVE_DOT_ORG,
  125. 'PREVIEW_ORIGINALS': SERVER_CONFIG.PREVIEW_ORIGINALS,
  126. 'archiveresults': sorted(archiveresults.values(), key=lambda r: all_types.index(r['name']) if r['name'] in all_types else -r['size']),
  127. 'best_result': best_result,
  128. # 'tags_str': 'somealskejrewlkrjwer,werlmwrwlekrjewlkrjwer324m532l,4m32,23m324234',
  129. }
  130. return render(template_name='core/snapshot_live.html', request=request, context=context)
  131. def get(self, request, path):
  132. if not request.user.is_authenticated and not SERVER_CONFIG.PUBLIC_SNAPSHOTS:
  133. return redirect(f'/admin/login/?next={request.path}')
  134. snapshot = None
  135. try:
  136. slug, archivefile = path.split('/', 1)
  137. except (IndexError, ValueError):
  138. slug, archivefile = path.split('/', 1)[0], 'index.html'
  139. # slug is a timestamp
  140. if slug.replace('.','').isdigit():
  141. # missing trailing slash -> redirect to index
  142. if '/' not in path:
  143. return redirect(f'{path}/index.html')
  144. try:
  145. try:
  146. snapshot = Snapshot.objects.get(Q(timestamp=slug) | Q(id__startswith=slug))
  147. if archivefile == 'index.html':
  148. # if they requested snapshot index, serve live rendered template instead of static html
  149. response = self.render_live_index(request, snapshot)
  150. else:
  151. response = serve_static_with_byterange_support(
  152. request, archivefile, document_root=snapshot.link_dir, show_indexes=True,
  153. )
  154. response["Link"] = f'<{snapshot.url}>; rel="canonical"'
  155. return response
  156. except Snapshot.DoesNotExist:
  157. if Snapshot.objects.filter(timestamp__startswith=slug).exists():
  158. raise Snapshot.MultipleObjectsReturned
  159. else:
  160. raise
  161. except Snapshot.DoesNotExist:
  162. # Snapshot does not exist
  163. return HttpResponse(
  164. format_html(
  165. (
  166. '<center><br/><br/><br/>'
  167. 'No Snapshot directories match the given timestamp/ID/ABID: <code>{}</code><br/><br/>'
  168. 'You can <a href="/add/" target="_top">add a new Snapshot</a>, or return to the <a href="/" target="_top">Main Index</a>'
  169. '</center>'
  170. ),
  171. slug,
  172. path,
  173. ),
  174. content_type="text/html",
  175. status=404,
  176. )
  177. except Snapshot.MultipleObjectsReturned:
  178. snapshot_hrefs = mark_safe('<br/>').join(
  179. format_html(
  180. '{} <a href="/archive/{}/index.html"><b><code>{}</code></b></a> {} <b>{}</b>',
  181. snap.bookmarked_at.strftime('%Y-%m-%d %H:%M:%S'),
  182. snap.timestamp,
  183. snap.timestamp,
  184. snap.url,
  185. snap.title_stripped[:64] or '',
  186. )
  187. for snap in Snapshot.objects.filter(timestamp__startswith=slug).only('url', 'timestamp', 'title', 'bookmarked_at').order_by('-bookmarked_at')
  188. )
  189. return HttpResponse(
  190. format_html(
  191. (
  192. 'Multiple Snapshots match the given timestamp/ID/ABID <code>{}</code><br/><pre>'
  193. ),
  194. slug,
  195. ) + snapshot_hrefs + format_html(
  196. (
  197. '</pre><br/>'
  198. 'Choose a Snapshot to proceed or go back to the <a href="/" target="_top">Main Index</a>'
  199. )
  200. ),
  201. content_type="text/html",
  202. status=404,
  203. )
  204. except Http404:
  205. assert snapshot # (Snapshot.DoesNotExist is already handled above)
  206. # Snapshot dir exists but file within does not e.g. 124235.324234/screenshot.png
  207. return HttpResponse(
  208. format_html(
  209. (
  210. '<center><br/><br/><br/>'
  211. f'Snapshot <a href="/archive/{snapshot.timestamp}/index.html" target="_top"><b><code>[{snapshot.timestamp}]</code></b></a>: <a href="{snapshot.url}" target="_blank" rel="noreferrer">{snapshot.url}</a><br/>'
  212. f'was queued on {str(snapshot.bookmarked_at).split(".")[0]}, '
  213. f'but no files have been saved yet in:<br/><b><a href="/archive/{snapshot.timestamp}/" target="_top"><code>{snapshot.timestamp}</code></a><code>/'
  214. '{}'
  215. f'</code></b><br/><br/>'
  216. 'It\'s possible {} '
  217. f'during the last capture on {str(snapshot.bookmarked_at).split(".")[0]},<br/>or that the archiving process has not completed yet.<br/>'
  218. f'<pre><code># run this cmd to finish/retry archiving this Snapshot</code><br/>'
  219. f'<code style="user-select: all; color: #333">archivebox update -t timestamp {snapshot.timestamp}</code></pre><br/><br/>'
  220. '<div class="text-align: left; width: 100%; max-width: 400px">'
  221. '<i><b>Next steps:</i></b><br/>'
  222. f'- list all the <a href="/archive/{snapshot.timestamp}/" target="_top">Snapshot files <code>.*</code></a><br/>'
  223. f'- view the <a href="/archive/{snapshot.timestamp}/index.html" target="_top">Snapshot <code>./index.html</code></a><br/>'
  224. f'- go to the <a href="/admin/core/snapshot/{snapshot.pk}/change/" target="_top">Snapshot admin</a> to edit<br/>'
  225. f'- go to the <a href="/admin/core/snapshot/?id__exact={snapshot.id}" target="_top">Snapshot actions</a> to re-archive<br/>'
  226. '- or return to <a href="/" target="_top">the main index...</a></div>'
  227. '</center>'
  228. ),
  229. archivefile if str(archivefile) != 'None' else '',
  230. f'the {archivefile} resource could not be fetched' if str(archivefile) != 'None' else 'the original site was not available',
  231. ),
  232. content_type="text/html",
  233. status=404,
  234. )
  235. # # slud is an ID
  236. # ulid = slug.split('_', 1)[-1]
  237. # try:
  238. # try:
  239. # snapshot = snapshot or Snapshot.objects.get(Q(abid=ulid) | Q(id=ulid))
  240. # except Snapshot.DoesNotExist:
  241. # pass
  242. # try:
  243. # snapshot = Snapshot.objects.get(Q(abid__startswith=slug) | Q(abid__startswith=Snapshot.abid_prefix + slug) | Q(id__startswith=slug))
  244. # except (Snapshot.DoesNotExist, Snapshot.MultipleObjectsReturned):
  245. # pass
  246. # try:
  247. # snapshot = snapshot or Snapshot.objects.get(Q(abid__icontains=snapshot_id) | Q(id__icontains=snapshot_id))
  248. # except Snapshot.DoesNotExist:
  249. # pass
  250. # return redirect(f'/archive/{snapshot.timestamp}/index.html')
  251. # except Snapshot.DoesNotExist:
  252. # pass
  253. # slug is a URL
  254. try:
  255. try:
  256. # try exact match on full url / ABID first
  257. snapshot = Snapshot.objects.get(
  258. Q(url='http://' + path) | Q(url='https://' + path) | Q(id__startswith=path)
  259. | Q(abid__icontains=path) | Q(id__icontains=path)
  260. )
  261. except Snapshot.DoesNotExist:
  262. # fall back to match on exact base_url
  263. try:
  264. snapshot = Snapshot.objects.get(
  265. Q(url='http://' + base_url(path)) | Q(url='https://' + base_url(path))
  266. )
  267. except Snapshot.DoesNotExist:
  268. # fall back to matching base_url as prefix
  269. snapshot = Snapshot.objects.get(
  270. Q(url__startswith='http://' + base_url(path)) | Q(url__startswith='https://' + base_url(path))
  271. )
  272. return redirect(f'/archive/{snapshot.timestamp}/index.html')
  273. except Snapshot.DoesNotExist:
  274. return HttpResponse(
  275. format_html(
  276. (
  277. '<center><br/><br/><br/>'
  278. 'No Snapshots match the given url: <code>{}</code><br/><br/><br/>'
  279. 'Return to the <a href="/" target="_top">Main Index</a>, or:<br/><br/>'
  280. '+ <i><a href="/add/?url={}" target="_top">Add a new Snapshot for <code>{}</code></a><br/><br/></i>'
  281. '</center>'
  282. ),
  283. base_url(path),
  284. path if '://' in path else f'https://{path}',
  285. path,
  286. ),
  287. content_type="text/html",
  288. status=404,
  289. )
  290. except Snapshot.MultipleObjectsReturned:
  291. snapshot_hrefs = mark_safe('<br/>').join(
  292. format_html(
  293. '{} <code style="font-size: 0.8em">{}</code> <a href="/archive/{}/index.html"><b><code>{}</code></b></a> {} <b>{}</b>',
  294. snap.bookmarked_at.strftime('%Y-%m-%d %H:%M:%S'),
  295. snap.abid,
  296. snap.timestamp,
  297. snap.timestamp,
  298. snap.url,
  299. snap.title_stripped[:64] or '',
  300. )
  301. for snap in Snapshot.objects.filter(
  302. Q(url__startswith='http://' + base_url(path)) | Q(url__startswith='https://' + base_url(path))
  303. | Q(abid__icontains=path) | Q(id__icontains=path)
  304. ).only('url', 'timestamp', 'title', 'bookmarked_at').order_by('-bookmarked_at')
  305. )
  306. return HttpResponse(
  307. format_html(
  308. (
  309. 'Multiple Snapshots match the given URL <code>{}</code><br/><pre>'
  310. ),
  311. base_url(path),
  312. ) + snapshot_hrefs + format_html(
  313. (
  314. '</pre><br/>'
  315. 'Choose a Snapshot to proceed or go back to the <a href="/" target="_top">Main Index</a>'
  316. )
  317. ),
  318. content_type="text/html",
  319. status=404,
  320. )
  321. class PublicIndexView(ListView):
  322. template_name = 'public_index.html'
  323. model = Snapshot
  324. paginate_by = SERVER_CONFIG.SNAPSHOTS_PER_PAGE
  325. ordering = ['-bookmarked_at', '-created_at']
  326. def get_context_data(self, **kwargs):
  327. return {
  328. **super().get_context_data(**kwargs),
  329. 'VERSION': VERSION,
  330. 'COMMIT_HASH': SHELL_CONFIG.COMMIT_HASH,
  331. 'FOOTER_INFO': SERVER_CONFIG.FOOTER_INFO,
  332. }
  333. def get_queryset(self, **kwargs):
  334. qs = super().get_queryset(**kwargs)
  335. query = self.request.GET.get('q', default = '').strip()
  336. if not query:
  337. return qs.distinct()
  338. query_type = self.request.GET.get('query_type')
  339. if not query_type or query_type == 'all':
  340. qs = qs.filter(Q(title__icontains=query) | Q(url__icontains=query) | Q(timestamp__icontains=query) | Q(tags__name__icontains=query))
  341. try:
  342. qs = qs | query_search_index(query)
  343. except Exception as err:
  344. print(f'[!] Error while using search backend: {err.__class__.__name__} {err}')
  345. elif query_type == 'fulltext':
  346. try:
  347. qs = qs | query_search_index(query)
  348. except Exception as err:
  349. print(f'[!] Error while using search backend: {err.__class__.__name__} {err}')
  350. elif query_type == 'meta':
  351. qs = qs.filter(Q(title__icontains=query) | Q(url__icontains=query) | Q(timestamp__icontains=query) | Q(tags__name__icontains=query))
  352. elif query_type == 'url':
  353. qs = qs.filter(Q(url__icontains=query))
  354. elif query_type == 'title':
  355. qs = qs.filter(Q(title__icontains=query))
  356. elif query_type == 'timestamp':
  357. qs = qs.filter(Q(timestamp__icontains=query))
  358. elif query_type == 'tags':
  359. qs = qs.filter(Q(tags__name__icontains=query))
  360. else:
  361. print(f'[!] Unknown value for query_type: "{query_type}"')
  362. return qs.distinct()
  363. def get(self, *args, **kwargs):
  364. if SERVER_CONFIG.PUBLIC_INDEX or self.request.user.is_authenticated:
  365. response = super().get(*args, **kwargs)
  366. return response
  367. else:
  368. return redirect(f'/admin/login/?next={self.request.path}')
  369. @method_decorator(csrf_exempt, name='dispatch')
  370. class AddView(UserPassesTestMixin, FormView):
  371. template_name = "add.html"
  372. form_class = AddLinkForm
  373. def get_initial(self):
  374. """Prefill the AddLinkForm with the 'url' GET parameter"""
  375. if self.request.method == 'GET':
  376. url = self.request.GET.get('url', None)
  377. if url:
  378. return {'url': url if '://' in url else f'https://{url}'}
  379. return super().get_initial()
  380. def test_func(self):
  381. return SERVER_CONFIG.PUBLIC_ADD_VIEW or self.request.user.is_authenticated
  382. def get_context_data(self, **kwargs):
  383. return {
  384. **super().get_context_data(**kwargs),
  385. 'title': "Add URLs",
  386. # We can't just call request.build_absolute_uri in the template, because it would include query parameters
  387. 'absolute_add_path': self.request.build_absolute_uri(self.request.path),
  388. 'VERSION': VERSION,
  389. 'FOOTER_INFO': SERVER_CONFIG.FOOTER_INFO,
  390. 'stdout': '',
  391. }
  392. def form_valid(self, form):
  393. from core.admin_archiveresults import result_url
  394. url = form.cleaned_data["url"]
  395. print(f'[+] Adding URL: {url}')
  396. parser = form.cleaned_data["parser"]
  397. tag = form.cleaned_data["tag"]
  398. depth = 0 if form.cleaned_data["depth"] == "0" else 1
  399. extractors = ','.join(form.cleaned_data["archive_methods"])
  400. input_kwargs = {
  401. "urls": url,
  402. "tag": tag,
  403. "depth": depth,
  404. "parser": parser,
  405. "update_all": False,
  406. "out_dir": DATA_DIR,
  407. "created_by_id": self.request.user.pk,
  408. }
  409. if extractors:
  410. input_kwargs.update({"extractors": extractors})
  411. result = bg_add(input_kwargs, parent_task_id=None)
  412. print('Started background add job:', result)
  413. rough_url_count = url.count('://')
  414. messages.success(
  415. self.request,
  416. mark_safe(f"Adding {rough_url_count} URLs in the background. (refresh in a few minutes to see results) {result_url(result)}"),
  417. )
  418. return redirect("/admin/core/snapshot/")
  419. class HealthCheckView(View):
  420. """
  421. A Django view that renders plain text "OK" for service discovery tools
  422. """
  423. def get(self, request):
  424. """
  425. Handle a GET request
  426. """
  427. return HttpResponse(
  428. 'OK',
  429. content_type='text/plain',
  430. status=200
  431. )
  432. def find_config_section(key: str) -> str:
  433. CONFIGS = archivebox.pm.hook.get_CONFIGS()
  434. if key in CONSTANTS_CONFIG:
  435. return 'CONSTANT'
  436. matching_sections = [
  437. section_id for section_id, section in CONFIGS.items() if key in section.model_fields
  438. ]
  439. section = matching_sections[0] if matching_sections else 'DYNAMIC'
  440. return section
  441. def find_config_default(key: str) -> str:
  442. CONFIGS = archivebox.pm.hook.get_CONFIGS()
  443. if key in CONSTANTS_CONFIG:
  444. return str(CONSTANTS_CONFIG[key])
  445. default_val = None
  446. for config in CONFIGS.values():
  447. if key in config.model_fields:
  448. default_val = config.model_fields[key].default
  449. break
  450. if isinstance(default_val, Callable):
  451. default_val = inspect.getsource(default_val).split('lambda', 1)[-1].split(':', 1)[-1].replace('\n', ' ').strip()
  452. if default_val.count(')') > default_val.count('('):
  453. default_val = default_val[:-1]
  454. else:
  455. default_val = str(default_val)
  456. return default_val
  457. def find_config_type(key: str) -> str:
  458. CONFIGS = archivebox.pm.hook.get_CONFIGS()
  459. for config in CONFIGS.values():
  460. if hasattr(config, key):
  461. type_hints = get_type_hints(config)
  462. try:
  463. return str(type_hints[key].__name__)
  464. except AttributeError:
  465. return str(type_hints[key])
  466. return 'str'
  467. def key_is_safe(key: str) -> bool:
  468. for term in ('key', 'password', 'secret', 'token'):
  469. if term in key.lower():
  470. return False
  471. return True
  472. @render_with_table_view
  473. def live_config_list_view(request: HttpRequest, **kwargs) -> TableContext:
  474. CONFIGS = archivebox.pm.hook.get_CONFIGS()
  475. assert request.user.is_superuser, 'Must be a superuser to view configuration settings.'
  476. rows = {
  477. "Section": [],
  478. "Key": [],
  479. "Type": [],
  480. "Value": [],
  481. "Default": [],
  482. # "Documentation": [],
  483. # "Aliases": [],
  484. }
  485. for section_id, section in reversed(list(CONFIGS.items())):
  486. for key, field in section.model_fields.items():
  487. rows['Section'].append(section_id) # section.replace('_', ' ').title().replace(' Config', '')
  488. rows['Key'].append(ItemLink(key, key=key))
  489. rows['Type'].append(format_html('<code>{}</code>', find_config_type(key)))
  490. rows['Value'].append(mark_safe(f'<code>{getattr(section, key)}</code>') if key_is_safe(key) else '******** (redacted)')
  491. rows['Default'].append(mark_safe(f'<a href="https://github.com/search?q=repo%3AArchiveBox%2FArchiveBox+path%3Aconfig+{key}&type=code"><code style="text-decoration: underline">{find_config_default(key) or "See here..."}</code></a>'))
  492. # rows['Documentation'].append(mark_safe(f'Wiki: <a href="https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration#{key.lower()}">{key}</a>'))
  493. # rows['Aliases'].append(', '.join(find_config_aliases(key)))
  494. section = 'CONSTANT'
  495. for key in CONSTANTS_CONFIG.keys():
  496. rows['Section'].append(section) # section.replace('_', ' ').title().replace(' Config', '')
  497. rows['Key'].append(ItemLink(key, key=key))
  498. rows['Type'].append(format_html('<code>{}</code>', getattr(type(CONSTANTS_CONFIG[key]), '__name__', repr(CONSTANTS_CONFIG[key]))))
  499. rows['Value'].append(format_html('<code>{}</code>', CONSTANTS_CONFIG[key]) if key_is_safe(key) else '******** (redacted)')
  500. rows['Default'].append(mark_safe(f'<a href="https://github.com/search?q=repo%3AArchiveBox%2FArchiveBox+path%3Aconfig+{key}&type=code"><code style="text-decoration: underline">{find_config_default(key) or "See here..."}</code></a>'))
  501. # rows['Documentation'].append(mark_safe(f'Wiki: <a href="https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration#{key.lower()}">{key}</a>'))
  502. # rows['Aliases'].append('')
  503. return TableContext(
  504. title="Computed Configuration Values",
  505. table=rows,
  506. )
  507. @render_with_item_view
  508. def live_config_value_view(request: HttpRequest, key: str, **kwargs) -> ItemContext:
  509. CONFIGS = archivebox.pm.hook.get_CONFIGS()
  510. FLAT_CONFIG = archivebox.pm.hook.get_FLAT_CONFIG()
  511. assert request.user.is_superuser, 'Must be a superuser to view configuration settings.'
  512. # aliases = USER_CONFIG.get(key, {}).get("aliases", [])
  513. aliases = []
  514. if key in CONSTANTS_CONFIG:
  515. section_header = mark_safe(f'[CONSTANTS] &nbsp; <b><code style="color: lightgray">{key}</code></b> &nbsp; <small>(read-only, hardcoded by ArchiveBox)</small>')
  516. elif key in FLAT_CONFIG:
  517. section_header = mark_safe(f'data / ArchiveBox.conf &nbsp; [{find_config_section(key)}] &nbsp; <b><code style="color: lightgray">{key}</code></b>')
  518. else:
  519. section_header = mark_safe(f'[DYNAMIC CONFIG] &nbsp; <b><code style="color: lightgray">{key}</code></b> &nbsp; <small>(read-only, calculated at runtime)</small>')
  520. return ItemContext(
  521. slug=key,
  522. title=key,
  523. data=[
  524. {
  525. "name": section_header,
  526. "description": None,
  527. "fields": {
  528. 'Key': key,
  529. 'Type': find_config_type(key),
  530. 'Value': FLAT_CONFIG.get(key, CONFIGS.get(key, None)) if key_is_safe(key) else '********',
  531. },
  532. "help_texts": {
  533. 'Key': mark_safe(f'''
  534. <a href="https://github.com/ArchiveBox/ArchiveBox/wiki/Configuration#{key.lower()}">Documentation</a> &nbsp;
  535. <span style="display: {"inline" if aliases else "none"}">
  536. Aliases: {", ".join(aliases)}
  537. </span>
  538. '''),
  539. 'Type': mark_safe(f'''
  540. <a href="https://github.com/search?q=repo%3AArchiveBox%2FArchiveBox+path%3Aconfig+{key}&type=code">
  541. See full definition in <code>archivebox/config</code>...
  542. </a>
  543. '''),
  544. 'Value': mark_safe(f'''
  545. {'<b style="color: red">Value is redacted for your security. (Passwords, secrets, API tokens, etc. cannot be viewed in the Web UI)</b><br/><br/>' if not key_is_safe(key) else ''}
  546. <br/><hr/><br/>
  547. Default: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  548. <a href="https://github.com/search?q=repo%3AArchiveBox%2FArchiveBox+path%3Aconfig+{key}&type=code">
  549. <code>{find_config_default(key) or '↗️ See in ArchiveBox source code...'}</code>
  550. </a>
  551. <br/><br/>
  552. <p style="display: {"block" if key in FLAT_CONFIG else "none"}">
  553. <i>To change this value, edit <code>data/ArchiveBox.conf</code> or run:</i>
  554. <br/><br/>
  555. <code>archivebox config --set {key}="{
  556. val.strip("'")
  557. if (val := find_config_default(key)) else
  558. (repr(FLAT_CONFIG[key] if key_is_safe(key) else '********')).strip("'")
  559. }"</code>
  560. </p>
  561. '''),
  562. },
  563. },
  564. ],
  565. )