2020-07-28 03:26:45 +00:00
|
|
|
|
__package__ = 'archivebox.core'
|
|
|
|
|
|
|
|
|
|
from io import StringIO
|
2021-02-16 20:47:49 +00:00
|
|
|
|
from pathlib import Path
|
2020-07-28 03:26:45 +00:00
|
|
|
|
from contextlib import redirect_stdout
|
2021-04-10 08:19:30 +00:00
|
|
|
|
from datetime import datetime, timezone
|
2020-07-28 03:26:45 +00:00
|
|
|
|
|
2019-04-02 20:36:41 +00:00
|
|
|
|
from django.contrib import admin
|
2024-01-19 10:44:59 +00:00
|
|
|
|
from django.db.models import Count
|
2020-07-28 03:26:45 +00:00
|
|
|
|
from django.urls import path
|
2020-06-30 09:56:17 +00:00
|
|
|
|
from django.utils.html import format_html
|
2020-07-28 09:59:54 +00:00
|
|
|
|
from django.utils.safestring import mark_safe
|
2020-07-28 03:56:35 +00:00
|
|
|
|
from django.shortcuts import render, redirect
|
2020-07-28 03:26:45 +00:00
|
|
|
|
from django.contrib.auth import get_user_model
|
2020-10-12 18:47:03 +00:00
|
|
|
|
from django import forms
|
2024-05-06 18:06:42 +00:00
|
|
|
|
|
|
|
|
|
|
2024-05-13 09:36:15 +00:00
|
|
|
|
from signal_webhooks.admin import WebhookAdmin, get_webhook_model
|
2024-05-06 18:06:42 +00:00
|
|
|
|
|
2021-01-21 00:02:31 +00:00
|
|
|
|
from ..util import htmldecode, urldecode, ansi_to_html
|
2021-01-20 23:44:28 +00:00
|
|
|
|
|
2021-02-16 11:13:27 +00:00
|
|
|
|
from core.models import Snapshot, ArchiveResult, Tag
|
2021-03-01 03:54:04 +00:00
|
|
|
|
from core.forms import AddLinkForm
|
2020-07-28 03:26:45 +00:00
|
|
|
|
|
2020-11-17 23:42:57 +00:00
|
|
|
|
from core.mixins import SearchResultsAdminMixin
|
2024-04-25 10:56:22 +00:00
|
|
|
|
from api.models import APIToken
|
2020-11-17 23:42:57 +00:00
|
|
|
|
|
2020-11-28 07:12:27 +00:00
|
|
|
|
from index.html import snapshot_icons
|
2020-07-28 10:50:03 +00:00
|
|
|
|
from logging_util import printable_filesize
|
|
|
|
|
from main import add, remove
|
|
|
|
|
from extractors import archive_links
|
2024-01-09 04:55:30 +00:00
|
|
|
|
from config import (
|
|
|
|
|
OUTPUT_DIR,
|
|
|
|
|
SNAPSHOTS_PER_PAGE,
|
|
|
|
|
VERSION,
|
|
|
|
|
VERSIONS_AVAILABLE,
|
|
|
|
|
CAN_UPGRADE
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
GLOBAL_CONTEXT = {'VERSION': VERSION, 'VERSIONS_AVAILABLE': VERSIONS_AVAILABLE, 'CAN_UPGRADE': CAN_UPGRADE}
|
2020-06-30 09:56:17 +00:00
|
|
|
|
|
2021-01-30 10:35:17 +00:00
|
|
|
|
# Admin URLs
|
|
|
|
|
# /admin/
|
|
|
|
|
# /admin/login/
|
|
|
|
|
# /admin/core/
|
|
|
|
|
# /admin/core/snapshot/
|
|
|
|
|
# /admin/core/snapshot/:uuid/
|
|
|
|
|
# /admin/core/tag/
|
|
|
|
|
# /admin/core/tag/:uuid/
|
|
|
|
|
|
|
|
|
|
|
2020-06-30 09:56:17 +00:00
|
|
|
|
# TODO: https://stackoverflow.com/questions/40760880/add-custom-button-to-django-admin-panel
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
2019-04-22 17:20:19 +00:00
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
class ArchiveBoxAdmin(admin.AdminSite):
|
|
|
|
|
site_header = 'ArchiveBox'
|
|
|
|
|
index_title = 'Links'
|
|
|
|
|
site_title = 'Index'
|
|
|
|
|
namespace = 'admin'
|
|
|
|
|
|
|
|
|
|
def get_urls(self):
|
|
|
|
|
return [
|
|
|
|
|
path('core/snapshot/add/', self.add_view, name='Add'),
|
|
|
|
|
] + super().get_urls()
|
|
|
|
|
|
|
|
|
|
def add_view(self, request):
|
|
|
|
|
if not request.user.is_authenticated:
|
|
|
|
|
return redirect(f'/admin/login/?next={request.path}')
|
|
|
|
|
|
|
|
|
|
request.current_app = self.name
|
|
|
|
|
context = {
|
|
|
|
|
**self.each_context(request),
|
|
|
|
|
'title': 'Add URLs',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
context['form'] = AddLinkForm()
|
|
|
|
|
|
|
|
|
|
elif request.method == 'POST':
|
|
|
|
|
form = AddLinkForm(request.POST)
|
|
|
|
|
if form.is_valid():
|
|
|
|
|
url = form.cleaned_data["url"]
|
|
|
|
|
print(f'[+] Adding URL: {url}')
|
|
|
|
|
depth = 0 if form.cleaned_data["depth"] == "0" else 1
|
|
|
|
|
input_kwargs = {
|
|
|
|
|
"urls": url,
|
|
|
|
|
"depth": depth,
|
|
|
|
|
"update_all": False,
|
|
|
|
|
"out_dir": OUTPUT_DIR,
|
|
|
|
|
}
|
|
|
|
|
add_stdout = StringIO()
|
|
|
|
|
with redirect_stdout(add_stdout):
|
|
|
|
|
add(**input_kwargs)
|
|
|
|
|
print(add_stdout.getvalue())
|
|
|
|
|
|
|
|
|
|
context.update({
|
|
|
|
|
"stdout": ansi_to_html(add_stdout.getvalue().strip()),
|
|
|
|
|
"form": AddLinkForm()
|
|
|
|
|
})
|
|
|
|
|
else:
|
|
|
|
|
context["form"] = form
|
|
|
|
|
|
|
|
|
|
return render(template_name='add.html', request=request, context=context)
|
|
|
|
|
|
2024-05-06 15:11:01 +00:00
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
archivebox_admin = ArchiveBoxAdmin()
|
|
|
|
|
archivebox_admin.register(get_user_model())
|
2024-04-25 10:56:22 +00:00
|
|
|
|
archivebox_admin.register(APIToken)
|
2024-05-13 09:36:15 +00:00
|
|
|
|
archivebox_admin.register(get_webhook_model(), WebhookAdmin)
|
2024-03-26 00:46:01 +00:00
|
|
|
|
archivebox_admin.disable_action('delete_selected')
|
|
|
|
|
|
2024-05-06 18:06:42 +00:00
|
|
|
|
|
2024-05-13 09:36:15 +00:00
|
|
|
|
# patch admin with methods to add data views (implemented by admin_data_views package)
|
2024-05-06 18:06:42 +00:00
|
|
|
|
from admin_data_views.admin import get_app_list, admin_data_index_view, get_admin_data_urls, get_urls
|
|
|
|
|
|
|
|
|
|
archivebox_admin.get_app_list = get_app_list.__get__(archivebox_admin, ArchiveBoxAdmin)
|
|
|
|
|
archivebox_admin.admin_data_index_view = admin_data_index_view.__get__(archivebox_admin, ArchiveBoxAdmin)
|
|
|
|
|
archivebox_admin.get_admin_data_urls = get_admin_data_urls.__get__(archivebox_admin, ArchiveBoxAdmin)
|
|
|
|
|
archivebox_admin.get_urls = get_urls(archivebox_admin.get_urls).__get__(archivebox_admin, ArchiveBoxAdmin)
|
|
|
|
|
|
|
|
|
|
|
2021-02-16 20:47:49 +00:00
|
|
|
|
class ArchiveResultInline(admin.TabularInline):
|
|
|
|
|
model = ArchiveResult
|
|
|
|
|
|
2021-02-18 13:04:14 +00:00
|
|
|
|
class TagInline(admin.TabularInline):
|
|
|
|
|
model = Snapshot.tags.through
|
|
|
|
|
|
2021-02-18 07:32:58 +00:00
|
|
|
|
from django.contrib.admin.helpers import ActionForm
|
2021-04-10 08:11:32 +00:00
|
|
|
|
from django.contrib.admin.widgets import AutocompleteSelectMultiple
|
|
|
|
|
|
2024-01-04 03:00:07 +00:00
|
|
|
|
class AutocompleteTags:
|
|
|
|
|
model = Tag
|
|
|
|
|
search_fields = ['name']
|
|
|
|
|
name = 'tags'
|
2024-03-26 00:46:01 +00:00
|
|
|
|
remote_field = TagInline
|
2021-04-10 08:11:32 +00:00
|
|
|
|
|
2024-01-04 03:00:07 +00:00
|
|
|
|
class AutocompleteTagsAdminStub:
|
|
|
|
|
name = 'admin'
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SnapshotActionForm(ActionForm):
|
2021-04-10 08:11:32 +00:00
|
|
|
|
tags = forms.ModelMultipleChoiceField(
|
|
|
|
|
queryset=Tag.objects.all(),
|
|
|
|
|
required=False,
|
2024-01-04 03:00:07 +00:00
|
|
|
|
widget=AutocompleteSelectMultiple(
|
|
|
|
|
AutocompleteTags(),
|
|
|
|
|
AutocompleteTagsAdminStub(),
|
|
|
|
|
),
|
2021-04-10 08:11:32 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# TODO: allow selecting actions for specific extractors? is this useful?
|
|
|
|
|
# EXTRACTOR_CHOICES = [
|
|
|
|
|
# (name, name.title())
|
|
|
|
|
# for name, _, _ in get_default_archive_methods()
|
|
|
|
|
# ]
|
|
|
|
|
# extractor = forms.ChoiceField(
|
|
|
|
|
# choices=EXTRACTOR_CHOICES,
|
|
|
|
|
# required=False,
|
|
|
|
|
# widget=forms.MultileChoiceField(attrs={'class': "form-control"})
|
|
|
|
|
# )
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
2020-10-12 18:47:03 +00:00
|
|
|
|
|
2024-05-13 14:50:07 +00:00
|
|
|
|
def get_abid_info(self, obj):
|
|
|
|
|
return format_html(
|
|
|
|
|
# URL Hash: <code style="font-size: 10px; user-select: all">{}</code><br/>
|
|
|
|
|
'''
|
|
|
|
|
ABID: <code style="font-size: 16px; user-select: all"><b>{}</b></code><br/>
|
|
|
|
|
TS: <code style="font-size: 10px; user-select: all"><b>{}</b></code> ({})<br/>
|
|
|
|
|
URI: <code style="font-size: 10px; user-select: all"><b>{}</b></code> ({})<br/>
|
|
|
|
|
SUBTYPE: <code style="font-size: 10px; user-select: all"><b>{}</b></code> ({})<br/>
|
|
|
|
|
RAND: <code style="font-size: 10px; user-select: all"><b>{}</b></code> ({})<br/><br/>
|
|
|
|
|
ABID AS UUID: <code style="font-size: 10px; user-select: all">{}</code> <br/><br/>
|
|
|
|
|
|
|
|
|
|
.uuid: <code style="font-size: 10px; user-select: all">{}</code> <br/>
|
|
|
|
|
.id: <code style="font-size: 10px; user-select: all">{}</code> <br/>
|
|
|
|
|
.pk: <code style="font-size: 10px; user-select: all">{}</code> <br/><br/>
|
|
|
|
|
''',
|
|
|
|
|
obj.abid,
|
|
|
|
|
obj.ABID.ts, obj.abid_values['ts'].isoformat() if isinstance(obj.abid_values['ts'], datetime) else obj.abid_values['ts'],
|
|
|
|
|
obj.ABID.uri, str(obj.abid_values['uri']),
|
|
|
|
|
obj.ABID.subtype, str(obj.abid_values['subtype']),
|
|
|
|
|
obj.ABID.rand, str(obj.abid_values['rand'])[-7:],
|
|
|
|
|
obj.ABID.uuid,
|
|
|
|
|
obj.uuid,
|
|
|
|
|
obj.id,
|
|
|
|
|
obj.pk,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.register(Snapshot, site=archivebox_admin)
|
2020-11-17 23:42:57 +00:00
|
|
|
|
class SnapshotAdmin(SearchResultsAdminMixin, admin.ModelAdmin):
|
2021-04-10 08:12:30 +00:00
|
|
|
|
list_display = ('added', 'title_str', 'files', 'size', 'url_str')
|
|
|
|
|
sort_fields = ('title_str', 'url_str', 'added', 'files')
|
2024-05-13 14:50:07 +00:00
|
|
|
|
readonly_fields = ('admin_actions', 'status_info', 'bookmarked', 'added', 'updated', 'created', 'modified', 'identifiers')
|
2024-05-18 03:11:31 +00:00
|
|
|
|
search_fields = ('id', 'url', 'abid', 'uuid', 'timestamp', 'title', 'tags__name')
|
2024-05-13 14:50:07 +00:00
|
|
|
|
fields = ('url', 'timestamp', 'created_by', 'tags', 'title', *readonly_fields)
|
|
|
|
|
list_filter = ('added', 'updated', 'tags', 'archiveresult__status', 'created_by')
|
2020-06-30 09:56:17 +00:00
|
|
|
|
ordering = ['-added']
|
2021-04-10 08:11:32 +00:00
|
|
|
|
actions = ['add_tags', 'remove_tags', 'update_titles', 'update_snapshots', 'resnapshot_snapshot', 'overwrite_snapshots', 'delete_snapshots']
|
2021-02-18 13:04:14 +00:00
|
|
|
|
autocomplete_fields = ['tags']
|
2021-02-16 20:47:49 +00:00
|
|
|
|
inlines = [ArchiveResultInline]
|
2021-02-18 13:04:14 +00:00
|
|
|
|
list_per_page = SNAPSHOTS_PER_PAGE
|
2020-06-30 09:56:17 +00:00
|
|
|
|
|
2021-02-18 07:32:58 +00:00
|
|
|
|
action_form = SnapshotActionForm
|
|
|
|
|
|
2024-01-09 04:55:30 +00:00
|
|
|
|
def changelist_view(self, request, extra_context=None):
|
|
|
|
|
extra_context = extra_context or {}
|
|
|
|
|
return super().changelist_view(request, extra_context | GLOBAL_CONTEXT)
|
|
|
|
|
|
2020-12-12 04:03:46 +00:00
|
|
|
|
def get_urls(self):
|
|
|
|
|
urls = super().get_urls()
|
|
|
|
|
custom_urls = [
|
2021-04-10 08:12:30 +00:00
|
|
|
|
path('grid/', self.admin_site.admin_view(self.grid_view), name='grid')
|
2020-12-12 04:03:46 +00:00
|
|
|
|
]
|
|
|
|
|
return custom_urls + urls
|
|
|
|
|
|
2020-09-21 16:50:26 +00:00
|
|
|
|
def get_queryset(self, request):
|
2021-02-18 07:32:58 +00:00
|
|
|
|
self.request = request
|
2024-01-19 10:44:59 +00:00
|
|
|
|
return super().get_queryset(request).prefetch_related('tags').annotate(archiveresult_count=Count('archiveresult'))
|
2020-09-21 16:50:26 +00:00
|
|
|
|
|
|
|
|
|
def tag_list(self, obj):
|
2020-09-24 20:11:17 +00:00
|
|
|
|
return ', '.join(obj.tags.values_list('name', flat=True))
|
2020-09-21 16:50:26 +00:00
|
|
|
|
|
2021-02-18 07:32:58 +00:00
|
|
|
|
# TODO: figure out a different way to do this, you cant nest forms so this doenst work
|
|
|
|
|
# def action(self, obj):
|
|
|
|
|
# # csrfmiddlewaretoken: Wa8UcQ4fD3FJibzxqHN3IYrrjLo4VguWynmbzzcPYoebfVUnDovon7GEMYFRgsh0
|
|
|
|
|
# # action: update_snapshots
|
|
|
|
|
# # select_across: 0
|
|
|
|
|
# # _selected_action: 76d29b26-2a88-439e-877c-a7cca1b72bb3
|
|
|
|
|
# return format_html(
|
|
|
|
|
# '''
|
|
|
|
|
# <form action="/admin/core/snapshot/" method="post" onsubmit="e => e.stopPropagation()">
|
|
|
|
|
# <input type="hidden" name="csrfmiddlewaretoken" value="{}">
|
|
|
|
|
# <input type="hidden" name="_selected_action" value="{}">
|
|
|
|
|
# <button name="update_snapshots">Check</button>
|
|
|
|
|
# <button name="update_titles">Pull title + favicon</button>
|
|
|
|
|
# <button name="update_snapshots">Update</button>
|
|
|
|
|
# <button name="overwrite_snapshots">Re-Archive (overwrite)</button>
|
|
|
|
|
# <button name="delete_snapshots">Permanently delete</button>
|
|
|
|
|
# </form>
|
|
|
|
|
# ''',
|
|
|
|
|
# csrf.get_token(self.request),
|
2024-05-13 12:12:12 +00:00
|
|
|
|
# obj.pk,
|
2021-02-18 07:32:58 +00:00
|
|
|
|
# )
|
|
|
|
|
|
2024-05-13 14:50:07 +00:00
|
|
|
|
def admin_actions(self, obj):
|
2020-06-30 09:56:17 +00:00
|
|
|
|
return format_html(
|
2024-05-13 14:50:07 +00:00
|
|
|
|
# URL Hash: <code style="font-size: 10px; user-select: all">{}</code><br/>
|
|
|
|
|
'''
|
2024-05-18 03:11:31 +00:00
|
|
|
|
<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>
|
|
|
|
|
<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>
|
|
|
|
|
<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>
|
2024-05-13 14:50:07 +00:00
|
|
|
|
''',
|
|
|
|
|
obj.timestamp,
|
|
|
|
|
obj.timestamp,
|
|
|
|
|
obj.pk,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def status_info(self, obj):
|
|
|
|
|
return format_html(
|
|
|
|
|
# URL Hash: <code style="font-size: 10px; user-select: all">{}</code><br/>
|
2021-04-10 08:12:30 +00:00
|
|
|
|
'''
|
|
|
|
|
Archived: {} ({} files {})
|
|
|
|
|
Favicon: <img src="{}" style="height: 20px"/>
|
2024-05-13 14:50:07 +00:00
|
|
|
|
Status code: {} <br/>
|
2021-04-10 08:12:30 +00:00
|
|
|
|
Server: {}
|
|
|
|
|
Content type: {}
|
|
|
|
|
Extension: {}
|
|
|
|
|
''',
|
|
|
|
|
'✅' if obj.is_archived else '❌',
|
|
|
|
|
obj.num_outputs,
|
2024-05-18 03:11:31 +00:00
|
|
|
|
self.size(obj) or '0kb',
|
2021-04-10 08:12:30 +00:00
|
|
|
|
f'/archive/{obj.timestamp}/favicon.ico',
|
2024-05-18 03:11:31 +00:00
|
|
|
|
obj.status_code or '-',
|
|
|
|
|
obj.headers and obj.headers.get('Server') or '-',
|
|
|
|
|
obj.headers and obj.headers.get('Content-Type') or '-',
|
|
|
|
|
obj.extension or '-',
|
2020-06-30 09:56:17 +00:00
|
|
|
|
)
|
|
|
|
|
|
2024-05-13 14:50:07 +00:00
|
|
|
|
def identifiers(self, obj):
|
|
|
|
|
return get_abid_info(self, obj)
|
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.display(
|
|
|
|
|
description='Title',
|
|
|
|
|
ordering='title',
|
|
|
|
|
)
|
2020-06-30 09:56:17 +00:00
|
|
|
|
def title_str(self, obj):
|
|
|
|
|
canon = obj.as_link().canonical_outputs()
|
2020-07-28 09:59:54 +00:00
|
|
|
|
tags = ''.join(
|
2021-04-01 07:31:05 +00:00
|
|
|
|
format_html('<a href="/admin/core/snapshot/?tags__id__exact={}"><span class="tag">{}</span></a> ', tag.id, tag)
|
2020-09-21 16:50:26 +00:00
|
|
|
|
for tag in obj.tags.all()
|
2020-10-31 11:55:27 +00:00
|
|
|
|
if str(tag).strip()
|
2020-10-07 15:15:56 +00:00
|
|
|
|
)
|
2020-06-30 09:56:17 +00:00
|
|
|
|
return format_html(
|
|
|
|
|
'<a href="/{}">'
|
2020-07-28 03:26:45 +00:00
|
|
|
|
'<img src="/{}/{}" class="favicon" onerror="this.remove()">'
|
2020-06-30 09:56:17 +00:00
|
|
|
|
'</a>'
|
2020-08-18 13:17:21 +00:00
|
|
|
|
'<a href="/{}/index.html">'
|
2020-07-28 03:26:45 +00:00
|
|
|
|
'<b class="status-{}">{}</b>'
|
|
|
|
|
'</a>',
|
2020-06-30 09:56:17 +00:00
|
|
|
|
obj.archive_path,
|
2020-07-27 23:29:57 +00:00
|
|
|
|
obj.archive_path, canon['favicon_path'],
|
2020-08-18 13:17:21 +00:00
|
|
|
|
obj.archive_path,
|
2020-07-28 03:26:45 +00:00
|
|
|
|
'fetched' if obj.latest_title or obj.title else 'pending',
|
2020-07-28 09:59:54 +00:00
|
|
|
|
urldecode(htmldecode(obj.latest_title or obj.title or ''))[:128] or 'Pending...'
|
2020-10-31 11:55:27 +00:00
|
|
|
|
) + mark_safe(f' <span class="tags">{tags}</span>')
|
2020-06-30 09:56:17 +00:00
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.display(
|
|
|
|
|
description='Files Saved',
|
|
|
|
|
ordering='archiveresult_count',
|
|
|
|
|
)
|
2020-06-30 09:56:17 +00:00
|
|
|
|
def files(self, obj):
|
2020-11-28 07:12:27 +00:00
|
|
|
|
return snapshot_icons(obj)
|
2020-07-27 23:29:57 +00:00
|
|
|
|
|
2021-04-10 08:12:30 +00:00
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.display(
|
|
|
|
|
ordering='archiveresult_count'
|
|
|
|
|
)
|
2020-07-27 23:29:57 +00:00
|
|
|
|
def size(self, obj):
|
2021-02-16 20:51:17 +00:00
|
|
|
|
archive_size = (Path(obj.link_dir) / 'index.html').exists() and obj.archive_size
|
2020-10-31 11:56:19 +00:00
|
|
|
|
if archive_size:
|
|
|
|
|
size_txt = printable_filesize(archive_size)
|
|
|
|
|
if archive_size > 52428800:
|
|
|
|
|
size_txt = mark_safe(f'<b>{size_txt}</b>')
|
|
|
|
|
else:
|
2020-10-31 23:32:43 +00:00
|
|
|
|
size_txt = mark_safe('<span style="opacity: 0.3">...</span>')
|
2020-07-27 23:29:57 +00:00
|
|
|
|
return format_html(
|
|
|
|
|
'<a href="/{}" title="View all files">{}</a>',
|
2020-06-30 09:56:17 +00:00
|
|
|
|
obj.archive_path,
|
2020-10-31 11:56:19 +00:00
|
|
|
|
size_txt,
|
2020-06-30 09:56:17 +00:00
|
|
|
|
)
|
|
|
|
|
|
2021-04-10 08:12:30 +00:00
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.display(
|
|
|
|
|
description='Original URL',
|
|
|
|
|
ordering='url',
|
|
|
|
|
)
|
2020-06-30 09:56:17 +00:00
|
|
|
|
def url_str(self, obj):
|
|
|
|
|
return format_html(
|
2021-04-10 08:12:30 +00:00
|
|
|
|
'<a href="{}"><code style="user-select: all;">{}</code></a>',
|
|
|
|
|
obj.url,
|
2024-05-13 14:50:07 +00:00
|
|
|
|
obj.url[:128],
|
2020-06-30 09:56:17 +00:00
|
|
|
|
)
|
2019-04-22 17:20:19 +00:00
|
|
|
|
|
2021-04-10 08:12:30 +00:00
|
|
|
|
def grid_view(self, request, extra_context=None):
|
2020-12-12 04:03:46 +00:00
|
|
|
|
|
|
|
|
|
# cl = self.get_changelist_instance(request)
|
|
|
|
|
|
|
|
|
|
# Save before monkey patching to restore for changelist list view
|
|
|
|
|
saved_change_list_template = self.change_list_template
|
|
|
|
|
saved_list_per_page = self.list_per_page
|
|
|
|
|
saved_list_max_show_all = self.list_max_show_all
|
|
|
|
|
|
|
|
|
|
# Monkey patch here plus core_tags.py
|
2021-01-30 10:35:07 +00:00
|
|
|
|
self.change_list_template = 'private_index_grid.html'
|
2021-04-10 08:12:30 +00:00
|
|
|
|
self.list_per_page = SNAPSHOTS_PER_PAGE
|
2020-12-12 04:03:46 +00:00
|
|
|
|
self.list_max_show_all = self.list_per_page
|
|
|
|
|
|
|
|
|
|
# Call monkey patched view
|
2021-04-10 08:12:30 +00:00
|
|
|
|
rendered_response = self.changelist_view(request, extra_context=extra_context)
|
2020-12-12 04:03:46 +00:00
|
|
|
|
|
|
|
|
|
# Restore values
|
2021-03-27 09:12:54 +00:00
|
|
|
|
self.change_list_template = saved_change_list_template
|
2020-12-12 04:03:46 +00:00
|
|
|
|
self.list_per_page = saved_list_per_page
|
|
|
|
|
self.list_max_show_all = saved_list_max_show_all
|
|
|
|
|
|
|
|
|
|
return rendered_response
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
2021-04-10 08:12:30 +00:00
|
|
|
|
# for debugging, uncomment this to print all requests:
|
|
|
|
|
# def changelist_view(self, request, extra_context=None):
|
|
|
|
|
# print('[*] Got request', request.method, request.POST)
|
|
|
|
|
# return super().changelist_view(request, extra_context=None)
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.action(
|
|
|
|
|
description="Pull"
|
|
|
|
|
)
|
2021-02-18 09:29:24 +00:00
|
|
|
|
def update_snapshots(self, request, queryset):
|
2021-02-18 07:32:58 +00:00
|
|
|
|
archive_links([
|
|
|
|
|
snapshot.as_link()
|
|
|
|
|
for snapshot in queryset
|
|
|
|
|
], out_dir=OUTPUT_DIR)
|
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.action(
|
|
|
|
|
description="⬇️ Title"
|
|
|
|
|
)
|
2021-02-18 09:29:24 +00:00
|
|
|
|
def update_titles(self, request, queryset):
|
2021-02-18 07:32:58 +00:00
|
|
|
|
archive_links([
|
|
|
|
|
snapshot.as_link()
|
|
|
|
|
for snapshot in queryset
|
|
|
|
|
], overwrite=True, methods=('title','favicon'), out_dir=OUTPUT_DIR)
|
2021-04-10 08:12:30 +00:00
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.action(
|
|
|
|
|
description="Re-Snapshot"
|
|
|
|
|
)
|
2021-04-10 08:12:30 +00:00
|
|
|
|
def resnapshot_snapshot(self, request, queryset):
|
|
|
|
|
for snapshot in queryset:
|
|
|
|
|
timestamp = datetime.now(timezone.utc).isoformat('T', 'seconds')
|
|
|
|
|
new_url = snapshot.url.split('#')[0] + f'#{timestamp}'
|
|
|
|
|
add(new_url, tag=snapshot.tags_str())
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.action(
|
|
|
|
|
description="Reset"
|
|
|
|
|
)
|
2021-02-18 09:29:24 +00:00
|
|
|
|
def overwrite_snapshots(self, request, queryset):
|
2021-02-18 07:32:58 +00:00
|
|
|
|
archive_links([
|
|
|
|
|
snapshot.as_link()
|
|
|
|
|
for snapshot in queryset
|
|
|
|
|
], overwrite=True, out_dir=OUTPUT_DIR)
|
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.action(
|
|
|
|
|
description="Delete"
|
|
|
|
|
)
|
2021-02-18 09:29:24 +00:00
|
|
|
|
def delete_snapshots(self, request, queryset):
|
2021-02-18 07:32:58 +00:00
|
|
|
|
remove(snapshots=queryset, yes=True, delete=True, out_dir=OUTPUT_DIR)
|
|
|
|
|
|
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.action(
|
|
|
|
|
description="+"
|
|
|
|
|
)
|
2021-04-10 08:11:32 +00:00
|
|
|
|
def add_tags(self, request, queryset):
|
|
|
|
|
tags = request.POST.getlist('tags')
|
|
|
|
|
print('[+] Adding tags', tags, 'to Snapshots', queryset)
|
2021-03-27 09:27:02 +00:00
|
|
|
|
for obj in queryset:
|
2021-04-10 08:11:32 +00:00
|
|
|
|
obj.tags.add(*tags)
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.action(
|
|
|
|
|
description="–"
|
|
|
|
|
)
|
2021-04-10 08:11:32 +00:00
|
|
|
|
def remove_tags(self, request, queryset):
|
|
|
|
|
tags = request.POST.getlist('tags')
|
|
|
|
|
print('[-] Removing tags', tags, 'to Snapshots', queryset)
|
2021-02-18 07:32:58 +00:00
|
|
|
|
for obj in queryset:
|
2021-04-10 08:11:32 +00:00
|
|
|
|
obj.tags.remove(*tags)
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
|
|
|
|
|
2020-12-12 04:03:46 +00:00
|
|
|
|
|
|
|
|
|
|
2019-04-22 17:20:19 +00:00
|
|
|
|
|
2019-04-23 01:40:42 +00:00
|
|
|
|
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.register(Tag, site=archivebox_admin)
|
2020-10-31 11:55:27 +00:00
|
|
|
|
class TagAdmin(admin.ModelAdmin):
|
2024-05-13 14:50:07 +00:00
|
|
|
|
list_display = ('slug', 'name', 'num_snapshots', 'snapshots', 'abid')
|
|
|
|
|
sort_fields = ('id', 'name', 'slug', 'abid')
|
|
|
|
|
readonly_fields = ('created', 'modified', 'identifiers', 'num_snapshots', 'snapshots')
|
|
|
|
|
search_fields = ('id', 'abid', 'uuid', 'name', 'slug')
|
|
|
|
|
fields = ('name', 'slug', 'created_by', *readonly_fields, )
|
2021-02-18 07:32:58 +00:00
|
|
|
|
actions = ['delete_selected']
|
2021-02-18 13:04:14 +00:00
|
|
|
|
ordering = ['-id']
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
2024-05-13 14:50:07 +00:00
|
|
|
|
def identifiers(self, obj):
|
|
|
|
|
return get_abid_info(self, obj)
|
|
|
|
|
|
2024-05-13 12:12:12 +00:00
|
|
|
|
def num_snapshots(self, tag):
|
2021-02-18 07:32:58 +00:00
|
|
|
|
return format_html(
|
|
|
|
|
'<a href="/admin/core/snapshot/?tags__id__exact={}">{} total</a>',
|
2024-05-13 12:12:12 +00:00
|
|
|
|
tag.id,
|
|
|
|
|
tag.snapshot_set.count(),
|
2021-02-18 07:32:58 +00:00
|
|
|
|
)
|
|
|
|
|
|
2024-05-13 12:12:12 +00:00
|
|
|
|
def snapshots(self, tag):
|
|
|
|
|
total_count = tag.snapshot_set.count()
|
2021-02-18 07:32:58 +00:00
|
|
|
|
return mark_safe('<br/>'.join(
|
|
|
|
|
format_html(
|
|
|
|
|
'{} <code><a href="/admin/core/snapshot/{}/change"><b>[{}]</b></a> {}</code>',
|
2021-03-01 05:40:41 +00:00
|
|
|
|
snap.updated.strftime('%Y-%m-%d %H:%M') if snap.updated else 'pending...',
|
2024-05-13 12:12:12 +00:00
|
|
|
|
snap.pk,
|
|
|
|
|
snap.abid,
|
2021-02-18 07:32:58 +00:00
|
|
|
|
snap.url,
|
|
|
|
|
)
|
2024-05-13 12:12:12 +00:00
|
|
|
|
for snap in tag.snapshot_set.order_by('-updated')[:10]
|
|
|
|
|
) + (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 ''))
|
2021-02-18 07:32:58 +00:00
|
|
|
|
|
2020-07-28 03:26:45 +00:00
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.register(ArchiveResult, site=archivebox_admin)
|
2021-02-16 11:13:27 +00:00
|
|
|
|
class ArchiveResultAdmin(admin.ModelAdmin):
|
2024-05-13 14:50:07 +00:00
|
|
|
|
list_display = ('start_ts', 'snapshot_info', 'tags_str', 'extractor', 'cmd_str', 'status', 'output_str')
|
2021-02-16 11:13:27 +00:00
|
|
|
|
sort_fields = ('start_ts', 'extractor', 'status')
|
2024-05-13 14:50:07 +00:00
|
|
|
|
readonly_fields = ('snapshot_info', 'tags_str', 'created_by', 'created', 'modified', 'identifiers')
|
2024-05-18 03:11:31 +00:00
|
|
|
|
search_fields = ('id', 'uuid', 'abid', 'snapshot__url', 'extractor', 'output', 'cmd_version', 'cmd', 'snapshot__timestamp')
|
2024-05-13 14:50:07 +00:00
|
|
|
|
fields = ('snapshot', 'extractor', 'status', 'output', 'pwd', 'cmd', 'start_ts', 'end_ts', 'cmd_version', *readonly_fields)
|
2021-02-18 13:04:14 +00:00
|
|
|
|
autocomplete_fields = ['snapshot']
|
2021-02-16 20:47:49 +00:00
|
|
|
|
|
|
|
|
|
list_filter = ('status', 'extractor', 'start_ts', 'cmd_version')
|
|
|
|
|
ordering = ['-start_ts']
|
|
|
|
|
list_per_page = SNAPSHOTS_PER_PAGE
|
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.display(
|
2024-05-13 14:50:07 +00:00
|
|
|
|
description='Snapshot Info'
|
2024-03-26 00:46:01 +00:00
|
|
|
|
)
|
2024-05-13 14:50:07 +00:00
|
|
|
|
def snapshot_info(self, result):
|
2021-02-16 20:47:49 +00:00
|
|
|
|
return format_html(
|
2024-05-13 14:50:07 +00:00
|
|
|
|
'<a href="/archive/{}/index.html"><b><code>[{}]</code></b> {} {}</a><br/>',
|
2024-05-13 12:12:12 +00:00
|
|
|
|
result.snapshot.timestamp,
|
2024-05-13 14:50:07 +00:00
|
|
|
|
result.snapshot.abid,
|
|
|
|
|
result.snapshot.added.strftime('%Y-%m-%d %H:%M'),
|
2024-05-13 12:12:12 +00:00
|
|
|
|
result.snapshot.url[:128],
|
2021-02-16 20:47:49 +00:00
|
|
|
|
)
|
|
|
|
|
|
2024-05-13 14:50:07 +00:00
|
|
|
|
def identifiers(self, obj):
|
|
|
|
|
return get_abid_info(self, obj)
|
|
|
|
|
|
2024-03-26 00:46:01 +00:00
|
|
|
|
@admin.display(
|
2024-05-13 14:50:07 +00:00
|
|
|
|
description='Snapshot Tags'
|
2024-03-26 00:46:01 +00:00
|
|
|
|
)
|
2024-05-13 12:12:12 +00:00
|
|
|
|
def tags_str(self, result):
|
|
|
|
|
return result.snapshot.tags_str()
|
2021-04-01 05:39:34 +00:00
|
|
|
|
|
2024-05-13 12:12:12 +00:00
|
|
|
|
def cmd_str(self, result):
|
2021-02-16 20:47:49 +00:00
|
|
|
|
return format_html(
|
|
|
|
|
'<pre>{}</pre>',
|
2024-05-13 12:12:12 +00:00
|
|
|
|
' '.join(result.cmd) if isinstance(result.cmd, list) else str(result.cmd),
|
2021-02-16 20:47:49 +00:00
|
|
|
|
)
|
|
|
|
|
|
2024-05-13 12:12:12 +00:00
|
|
|
|
def output_str(self, result):
|
2021-02-16 20:47:49 +00:00
|
|
|
|
return format_html(
|
2021-02-18 07:32:58 +00:00
|
|
|
|
'<a href="/archive/{}/{}" class="output-link">↗️</a><pre>{}</pre>',
|
2024-05-13 12:12:12 +00:00
|
|
|
|
result.snapshot.timestamp,
|
|
|
|
|
result.output if (result.status == 'succeeded') and result.extractor not in ('title', 'archive_org') else 'index.html',
|
|
|
|
|
result.output,
|
2021-02-16 20:47:49 +00:00
|
|
|
|
)
|