|
@@ -1,21 +1,28 @@
|
|
|
__package__ = 'archivebox.core'
|
|
__package__ = 'archivebox.core'
|
|
|
|
|
|
|
|
|
|
+from io import StringIO
|
|
|
|
|
+from contextlib import redirect_stdout
|
|
|
|
|
+
|
|
|
from django.shortcuts import render, redirect
|
|
from django.shortcuts import render, redirect
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
from django.http import HttpResponse
|
|
|
from django.views import View, static
|
|
from django.views import View, static
|
|
|
|
|
+from django.views.generic.list import ListView
|
|
|
|
|
+from django.views.generic import FormView
|
|
|
|
|
+from django.contrib.auth.mixins import UserPassesTestMixin
|
|
|
|
|
|
|
|
from core.models import Snapshot
|
|
from core.models import Snapshot
|
|
|
|
|
+from core.utils import get_icons
|
|
|
|
|
+from core.forms import AddLinkForm
|
|
|
|
|
|
|
|
-from ..index import load_main_index, load_main_index_meta
|
|
|
|
|
from ..config import (
|
|
from ..config import (
|
|
|
OUTPUT_DIR,
|
|
OUTPUT_DIR,
|
|
|
- VERSION,
|
|
|
|
|
- FOOTER_INFO,
|
|
|
|
|
PUBLIC_INDEX,
|
|
PUBLIC_INDEX,
|
|
|
PUBLIC_SNAPSHOTS,
|
|
PUBLIC_SNAPSHOTS,
|
|
|
|
|
+ PUBLIC_ADD_VIEW
|
|
|
)
|
|
)
|
|
|
-from ..util import base_url
|
|
|
|
|
|
|
+from main import add
|
|
|
|
|
+from ..util import base_url, ansi_to_html
|
|
|
|
|
|
|
|
|
|
|
|
|
class MainIndex(View):
|
|
class MainIndex(View):
|
|
@@ -26,32 +33,10 @@ class MainIndex(View):
|
|
|
return redirect('/admin/core/snapshot/')
|
|
return redirect('/admin/core/snapshot/')
|
|
|
|
|
|
|
|
if PUBLIC_INDEX:
|
|
if PUBLIC_INDEX:
|
|
|
- return redirect('OldHome')
|
|
|
|
|
|
|
+ return redirect('public-index')
|
|
|
|
|
|
|
|
return redirect(f'/admin/login/?next={request.path}')
|
|
return redirect(f'/admin/login/?next={request.path}')
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-class OldIndex(View):
|
|
|
|
|
- template = 'main_index.html'
|
|
|
|
|
-
|
|
|
|
|
- def get(self, request):
|
|
|
|
|
- if PUBLIC_INDEX or request.user.is_authenticated:
|
|
|
|
|
- all_links = load_main_index(out_dir=OUTPUT_DIR)
|
|
|
|
|
- meta_info = load_main_index_meta(out_dir=OUTPUT_DIR)
|
|
|
|
|
-
|
|
|
|
|
- context = {
|
|
|
|
|
- 'updated': meta_info['updated'],
|
|
|
|
|
- 'num_links': meta_info['num_links'],
|
|
|
|
|
- 'links': all_links,
|
|
|
|
|
- 'VERSION': VERSION,
|
|
|
|
|
- 'FOOTER_INFO': FOOTER_INFO,
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return render(template_name=self.template, request=request, context=context)
|
|
|
|
|
-
|
|
|
|
|
- return redirect(f'/admin/login/?next={request.path}')
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
class LinkDetails(View):
|
|
class LinkDetails(View):
|
|
|
def get(self, request, path):
|
|
def get(self, request, path):
|
|
@@ -102,3 +87,60 @@ class LinkDetails(View):
|
|
|
content_type="text/plain",
|
|
content_type="text/plain",
|
|
|
status=404,
|
|
status=404,
|
|
|
)
|
|
)
|
|
|
|
|
+
|
|
|
|
|
+class PublicArchiveView(ListView):
|
|
|
|
|
+ template = 'snapshot_list.html'
|
|
|
|
|
+ model = Snapshot
|
|
|
|
|
+ paginate_by = 100
|
|
|
|
|
+
|
|
|
|
|
+ def get_queryset(self, **kwargs):
|
|
|
|
|
+ qs = super().get_queryset(**kwargs)
|
|
|
|
|
+ query = self.request.GET.get('q')
|
|
|
|
|
+ if query:
|
|
|
|
|
+ qs = Snapshot.objects.filter(title__icontains=query)
|
|
|
|
|
+ for snapshot in qs:
|
|
|
|
|
+ snapshot.icons = get_icons(snapshot)
|
|
|
|
|
+ return qs
|
|
|
|
|
+
|
|
|
|
|
+ def get(self, *args, **kwargs):
|
|
|
|
|
+ if PUBLIC_INDEX or self.request.user.is_authenticated:
|
|
|
|
|
+ response = super().get(*args, **kwargs)
|
|
|
|
|
+ return response
|
|
|
|
|
+ else:
|
|
|
|
|
+ return redirect(f'/admin/login/?next={self.request.path}')
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class AddView(UserPassesTestMixin, FormView):
|
|
|
|
|
+ template_name = "add_links.html"
|
|
|
|
|
+ form_class = AddLinkForm
|
|
|
|
|
+
|
|
|
|
|
+ def test_func(self):
|
|
|
|
|
+ return PUBLIC_ADD_VIEW or self.request.user.is_authenticated
|
|
|
|
|
+
|
|
|
|
|
+ def get_context_data(self, *args, **kwargs):
|
|
|
|
|
+ context = super().get_context_data(*args, **kwargs)
|
|
|
|
|
+ context["title"] = "Add URLs"
|
|
|
|
|
+ return context
|
|
|
|
|
+
|
|
|
|
|
+ def form_valid(self, form):
|
|
|
|
|
+ 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 = self.get_context_data()
|
|
|
|
|
+
|
|
|
|
|
+ context.update({
|
|
|
|
|
+ "stdout": ansi_to_html(add_stdout.getvalue().strip()),
|
|
|
|
|
+ "form": AddLinkForm()
|
|
|
|
|
+ })
|
|
|
|
|
+ return render(template_name=self.template_name, request=self.request, context=context)
|