From 5e8c115f3f3e579d524993cd6fd608b0a701b186 Mon Sep 17 00:00:00 2001
From: apkallum <apkallum@protonmail.com>
Date: Tue, 25 Aug 2020 15:31:09 -0400
Subject: [PATCH] unify public archive view

---
 archivebox/core/urls.py                       |  3 +-
 archivebox/core/views.py                      | 72 +++++++++----------
 .../themes/default/core/snapshot_list.html    |  2 +-
 3 files changed, 36 insertions(+), 41 deletions(-)

diff --git a/archivebox/core/urls.py b/archivebox/core/urls.py
index a078ed94..60548c2d 100644
--- a/archivebox/core/urls.py
+++ b/archivebox/core/urls.py
@@ -5,7 +5,7 @@ from django.views import static
 from django.conf import settings
 from django.views.generic.base import RedirectView
 
-from core.views import MainIndex, OldIndex, LinkDetails, PublicArchiveView, SearchResultsView, add_view
+from core.views import MainIndex, OldIndex, LinkDetails, PublicArchiveView, add_view
 
 
 # print('DEBUG', settings.DEBUG)
@@ -32,5 +32,4 @@ urlpatterns = [
     path('index.json', static.serve, {'document_root': settings.OUTPUT_DIR, 'path': 'index.json'}),
     path('', MainIndex.as_view(), name='Home'),
     path('public/', PublicArchiveView.as_view(), name='public-index'),
-    path('search_results/', SearchResultsView.as_view(), name='search-results'),
 ]
diff --git a/archivebox/core/views.py b/archivebox/core/views.py
index c7a0890f..4dee4612 100644
--- a/archivebox/core/views.py
+++ b/archivebox/core/views.py
@@ -123,6 +123,9 @@ class PublicArchiveView(ListView):
 
     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
@@ -134,45 +137,38 @@ class PublicArchiveView(ListView):
         else:
             return redirect(f'/admin/login/?next={self.request.path}')
 
-class SearchResultsView(PublicArchiveView):
-    def get_queryset(self):
-        query = self.request.GET.get('q')
-        results = Snapshot.objects.filter(title__icontains=query)
-        for snapshot in results:
-            snapshot.icons = get_icons(snapshot) 
-        return results
 
 def add_view(request):
-        if PUBLIC_ADD_VIEW or request.user.is_authenticated:
-                context = {
-                    'title': 'Add URLs',
-                }
-                if request.method == 'GET':
-                    context['form'] = AddLinkForm()
+    if PUBLIC_ADD_VIEW or request.user.is_authenticated:
+            context = {
+                '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())
+            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_links.html', request=request, context=context)
-        else:
-            return redirect(f'/admin/login/?next={request.path}')
\ No newline at end of file
+                    context.update({
+                        "stdout": ansi_to_html(add_stdout.getvalue().strip()),
+                        "form": AddLinkForm()
+                    })
+                else:
+                    context["form"] = form
+            return render(template_name='add_links.html', request=request, context=context)
+    else:
+        return redirect(f'/admin/login/?next={request.path}')
\ No newline at end of file
diff --git a/archivebox/themes/default/core/snapshot_list.html b/archivebox/themes/default/core/snapshot_list.html
index befec1be..ec92db14 100644
--- a/archivebox/themes/default/core/snapshot_list.html
+++ b/archivebox/themes/default/core/snapshot_list.html
@@ -232,7 +232,7 @@
             </div>
         </header>
         <br>
-        <form action="{% url 'search-results' %}" method="get">
+        <form action="{% url 'public-index' %}" method="get">
             <input name="q" type="text" placeholder="Search...">
             <button type="submit">Search</button>
             <button onclick="location.href='{% url 'public-index' %}'" type="button">