admin.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. __package__ = 'archivebox.core'
  2. from io import StringIO
  3. from contextlib import redirect_stdout
  4. from django.contrib import admin
  5. from django.urls import path
  6. from django.utils.html import format_html
  7. from django.utils.safestring import mark_safe
  8. from django.shortcuts import render, redirect
  9. from django.contrib.auth import get_user_model
  10. from django import forms
  11. from core.models import Snapshot, Tag
  12. from core.forms import AddLinkForm, TagField
  13. from index.html import snapshot_icons
  14. from util import htmldecode, urldecode, ansi_to_html
  15. from logging_util import printable_filesize
  16. from main import add, remove
  17. from config import OUTPUT_DIR
  18. from extractors import archive_links
  19. # TODO: https://stackoverflow.com/questions/40760880/add-custom-button-to-django-admin-panel
  20. def update_snapshots(modeladmin, request, queryset):
  21. archive_links([
  22. snapshot.as_link()
  23. for snapshot in queryset
  24. ], out_dir=OUTPUT_DIR)
  25. update_snapshots.short_description = "Archive"
  26. def update_titles(modeladmin, request, queryset):
  27. archive_links([
  28. snapshot.as_link()
  29. for snapshot in queryset
  30. ], overwrite=True, methods=('title','favicon'), out_dir=OUTPUT_DIR)
  31. update_titles.short_description = "Pull title"
  32. def overwrite_snapshots(modeladmin, request, queryset):
  33. archive_links([
  34. snapshot.as_link()
  35. for snapshot in queryset
  36. ], overwrite=True, out_dir=OUTPUT_DIR)
  37. overwrite_snapshots.short_description = "Re-archive (overwrite)"
  38. def verify_snapshots(modeladmin, request, queryset):
  39. for snapshot in queryset:
  40. print(snapshot.timestamp, snapshot.url, snapshot.is_archived, snapshot.archive_size, len(snapshot.history))
  41. verify_snapshots.short_description = "Check"
  42. def delete_snapshots(modeladmin, request, queryset):
  43. remove(snapshots=queryset, yes=True, delete=True, out_dir=OUTPUT_DIR)
  44. delete_snapshots.short_description = "Delete"
  45. class SnapshotAdminForm(forms.ModelForm):
  46. tags = TagField(required=False)
  47. class Meta:
  48. model = Snapshot
  49. fields = "__all__"
  50. def save(self, commit=True):
  51. # Based on: https://stackoverflow.com/a/49933068/3509554
  52. # Get the unsave instance
  53. instance = forms.ModelForm.save(self, False)
  54. tags = self.cleaned_data.pop("tags")
  55. #update save_m2m
  56. def new_save_m2m():
  57. instance.save_tags(tags)
  58. # Do we need to save all changes now?
  59. self.save_m2m = new_save_m2m
  60. if commit:
  61. instance.save()
  62. return instance
  63. class SnapshotAdmin(admin.ModelAdmin):
  64. list_display = ('added', 'title_str', 'url_str', 'files', 'size')
  65. sort_fields = ('title_str', 'url_str', 'added')
  66. readonly_fields = ('id', 'url', 'timestamp', 'num_outputs', 'is_archived', 'url_hash', 'added', 'updated')
  67. search_fields = ['url', 'timestamp', 'title', 'tags__name']
  68. fields = (*readonly_fields, 'title', 'tags')
  69. list_filter = ('added', 'updated', 'tags')
  70. ordering = ['-added']
  71. actions = [delete_snapshots, overwrite_snapshots, update_snapshots, update_titles, verify_snapshots]
  72. actions_template = 'admin/actions_as_select.html'
  73. form = SnapshotAdminForm
  74. def get_queryset(self, request):
  75. return super().get_queryset(request).prefetch_related('tags')
  76. def tag_list(self, obj):
  77. return ', '.join(obj.tags.values_list('name', flat=True))
  78. def id_str(self, obj):
  79. return format_html(
  80. '<code style="font-size: 10px">{}</code>',
  81. obj.url_hash[:8],
  82. )
  83. def title_str(self, obj):
  84. canon = obj.as_link().canonical_outputs()
  85. tags = ''.join(
  86. format_html('<a href="/admin/core/snapshot/?tags__id__exact={}"><span class="tag">{}</span></a> ', tag.id, tag)
  87. for tag in obj.tags.all()
  88. if str(tag).strip()
  89. )
  90. return format_html(
  91. '<a href="/{}">'
  92. '<img src="/{}/{}" class="favicon" onerror="this.remove()">'
  93. '</a>'
  94. '<a href="/{}/index.html">'
  95. '<b class="status-{}">{}</b>'
  96. '</a>',
  97. obj.archive_path,
  98. obj.archive_path, canon['favicon_path'],
  99. obj.archive_path,
  100. 'fetched' if obj.latest_title or obj.title else 'pending',
  101. urldecode(htmldecode(obj.latest_title or obj.title or ''))[:128] or 'Pending...'
  102. ) + mark_safe(f' <span class="tags">{tags}</span>')
  103. def files(self, obj):
  104. return snapshot_icons(obj)
  105. def size(self, obj):
  106. archive_size = obj.archive_size
  107. if archive_size:
  108. size_txt = printable_filesize(archive_size)
  109. if archive_size > 52428800:
  110. size_txt = mark_safe(f'<b>{size_txt}</b>')
  111. else:
  112. size_txt = mark_safe('<span style="opacity: 0.3">...</span>')
  113. return format_html(
  114. '<a href="/{}" title="View all files">{}</a>',
  115. obj.archive_path,
  116. size_txt,
  117. )
  118. def url_str(self, obj):
  119. return format_html(
  120. '<a href="{}"><code>{}</code></a>',
  121. obj.url,
  122. obj.url.split('://www.', 1)[-1].split('://', 1)[-1][:64],
  123. )
  124. id_str.short_description = 'ID'
  125. title_str.short_description = 'Title'
  126. url_str.short_description = 'Original URL'
  127. id_str.admin_order_field = 'id'
  128. title_str.admin_order_field = 'title'
  129. url_str.admin_order_field = 'url'
  130. class TagAdmin(admin.ModelAdmin):
  131. list_display = ('slug', 'name', 'id')
  132. sort_fields = ('id', 'name', 'slug')
  133. readonly_fields = ('id',)
  134. search_fields = ('id', 'name', 'slug')
  135. fields = (*readonly_fields, 'name', 'slug')
  136. class ArchiveBoxAdmin(admin.AdminSite):
  137. site_header = 'ArchiveBox'
  138. index_title = 'Links'
  139. site_title = 'Index'
  140. def get_urls(self):
  141. return [
  142. path('core/snapshot/add/', self.add_view, name='Add'),
  143. ] + super().get_urls()
  144. def add_view(self, request):
  145. if not request.user.is_authenticated:
  146. return redirect(f'/admin/login/?next={request.path}')
  147. request.current_app = self.name
  148. context = {
  149. **self.each_context(request),
  150. 'title': 'Add URLs',
  151. }
  152. if request.method == 'GET':
  153. context['form'] = AddLinkForm()
  154. elif request.method == 'POST':
  155. form = AddLinkForm(request.POST)
  156. if form.is_valid():
  157. url = form.cleaned_data["url"]
  158. print(f'[+] Adding URL: {url}')
  159. depth = 0 if form.cleaned_data["depth"] == "0" else 1
  160. input_kwargs = {
  161. "urls": url,
  162. "depth": depth,
  163. "update_all": False,
  164. "out_dir": OUTPUT_DIR,
  165. }
  166. add_stdout = StringIO()
  167. with redirect_stdout(add_stdout):
  168. add(**input_kwargs)
  169. print(add_stdout.getvalue())
  170. context.update({
  171. "stdout": ansi_to_html(add_stdout.getvalue().strip()),
  172. "form": AddLinkForm()
  173. })
  174. else:
  175. context["form"] = form
  176. return render(template_name='add_links.html', request=request, context=context)
  177. admin.site = ArchiveBoxAdmin()
  178. admin.site.register(get_user_model())
  179. admin.site.register(Snapshot, SnapshotAdmin)
  180. admin.site.register(Tag, TagAdmin)
  181. admin.site.disable_action('delete_selected')