Browse Source

search view inherits from modified public view

apkallum 5 years ago
parent
commit
c50af04cce

+ 3 - 2
archivebox/core/urls.py

@@ -5,7 +5,7 @@ from django.views import static
 from django.conf import settings
 from django.conf import settings
 from django.views.generic.base import RedirectView
 from django.views.generic.base import RedirectView
 
 
-from core.views import MainIndex, OldIndex, LinkDetails, PublicArchiveView
+from core.views import MainIndex, OldIndex, LinkDetails, PublicArchiveView, SearchResultsView
 
 
 
 
 # print('DEBUG', settings.DEBUG)
 # print('DEBUG', settings.DEBUG)
@@ -31,5 +31,6 @@ urlpatterns = [
     path('index.html', RedirectView.as_view(url='/')),
     path('index.html', RedirectView.as_view(url='/')),
     path('index.json', static.serve, {'document_root': settings.OUTPUT_DIR, 'path': 'index.json'}),
     path('index.json', static.serve, {'document_root': settings.OUTPUT_DIR, 'path': 'index.json'}),
     path('', MainIndex.as_view(), name='Home'),
     path('', MainIndex.as_view(), name='Home'),
-    path('public/', PublicArchiveView.as_view())
+    path('public/', PublicArchiveView.as_view(), name='public-index'),
+    path('search_results/', SearchResultsView.as_view(), name='search-results'),
 ]
 ]

+ 35 - 6
archivebox/core/views.py

@@ -3,9 +3,12 @@ __package__ = 'archivebox.core'
 from django.shortcuts import render, redirect
 from django.shortcuts import render, redirect
 
 
 from django.http import HttpResponse
 from django.http import HttpResponse
+from django.db.models import Q
 from django.views import View, static
 from django.views import View, static
 from django.views.generic.list import ListView
 from django.views.generic.list import ListView
 
 
+from django_datatables_view.base_datatable_view import BaseDatatableView
+
 from core.models import Snapshot
 from core.models import Snapshot
 
 
 from ..index import load_main_index, load_main_index_meta
 from ..index import load_main_index, load_main_index_meta
@@ -107,9 +110,35 @@ class LinkDetails(View):
 class PublicArchiveView(ListView):
 class PublicArchiveView(ListView):
     template = 'snapshot_list.html'
     template = 'snapshot_list.html'
     model = Snapshot
     model = Snapshot
-    context_object_name = 'links'
-    paginate_by = 2
-    def get_context_data(self, *args, **kwargs):
-        context = super(PublicArchiveView, self).get_context_data(*args, **kwargs)
-        context['links'] = [snapshot.as_link for snapshot in Snapshot.objects.all()]
-        return context
+    paginate_by = 50
+
+    def get_queryset(self, *args, **kwargs): 
+        qs = super(PublicArchiveView, self).get_queryset(*args, **kwargs) 
+        for snapshot in qs:
+            snapshot.canonical_outputs = snapshot.as_link().canonical_outputs() 
+        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}')
+
+# should we use it?
+class SnapshotDatatableView(BaseDatatableView):
+    model = Snapshot
+    columns = ['url', 'timestamp', 'title', 'tags', 'added']
+
+    def filter_queryset(self, qs):
+        sSearch = self.request.GET.get('sSearch', None)
+        if sSearch:
+            qs = qs.filter(Q(title__icontains=sSearch))
+        return qs
+
+class SearchResultsView(PublicArchiveView):
+    def get_queryset(self, *args, **kwargs):
+        qs = super(PublicArchiveView, self).get_queryset(*args, **kwargs) 
+        query = self.request.GET.get('q')
+        results = qs.filter(title__icontains=query)
+        return results

+ 32 - 5
archivebox/themes/default/core/snapshot_list.html

@@ -202,6 +202,8 @@
             }, true)
             }, true)
             jQuery(document).ready(function() {
             jQuery(document).ready(function() {
                 jQuery('#table-bookmarks').DataTable({
                 jQuery('#table-bookmarks').DataTable({
+                    searching: false,
+                    paging: false,
                     stateSave: true, // save state (filtered input, number of entries shown, etc) in localStorage
                     stateSave: true, // save state (filtered input, number of entries shown, etc) in localStorage
                     dom: '<lf<t>ip>', // how to show the table and its helpers (filter, etc) in the DOM
                     dom: '<lf<t>ip>', // how to show the table and its helpers (filter, etc) in the DOM
                     order: [[0, 'desc']],
                     order: [[0, 'desc']],
@@ -209,14 +211,14 @@
                 });
                 });
             });
             });
         </script>
         </script>
-        <base href="{% url 'Home' %}" target="_blank">
+        <base href="{% url 'Home' %}">
     </head>
     </head>
     <body data-status="finished">
     <body data-status="finished">
         <header>
         <header>
             <div class="header-top container-fluid">
             <div class="header-top container-fluid">
                 <div class="row nav">
                 <div class="row nav">
                     <div class="col-sm-2">
                     <div class="col-sm-2">
-                        <a href="/" class="header-archivebox" title="Last updated: {{updated}}">
+                        <a href="{% url 'public-index' %}" class="header-archivebox" title="Last updated: {{updated}}">
                             <img src="{% static 'archive.png' %}" alt="Logo"/>
                             <img src="{% static 'archive.png' %}" alt="Logo"/>
                             ArchiveBox: Index
                             ArchiveBox: Index
                         </a>
                         </a>
@@ -229,7 +231,14 @@
                 </div>
                 </div>
             </div>
             </div>
         </header>
         </header>
-        <table id="table-bookmarks">
+        <br>
+        <form action="{% url 'search-results' %}" method="get">
+            <input name="q" type="text" placeholder="Search...">
+            <button type="submit">Search</button>
+            <button onclick="location.href='{% url 'public-index' %}'" type="button">
+                Reload Index</button>
+          </form>
+            <table id="table-bookmarks">
             <thead>
             <thead>
                 <tr>
                 <tr>
                     <th style="width: 100px;">Bookmarked</th>
                     <th style="width: 100px;">Bookmarked</th>
@@ -239,9 +248,9 @@
                 </tr>
                 </tr>
             </thead>
             </thead>
             <tbody>
             <tbody>
-                {% for link in links %}
+                {% for link in object_list %}
                     <tr>
                     <tr>
-                        <td title="{{link.timestamp}}">{{link.bookmarked_date}}</td>
+                        <td title="{{link.timestamp}}">{{link.added}}</td>
                         <td class="title-col">
                         <td class="title-col">
                             {% if link.is_archived %}
                             {% if link.is_archived %}
                                 <a href="archive/{{link.timestamp}}/index.html"><img src="archive/{{link.timestamp}}/favicon.ico" class="link-favicon" decoding="async"></a>
                                 <a href="archive/{{link.timestamp}}/index.html"><img src="archive/{{link.timestamp}}/favicon.ico" class="link-favicon" decoding="async"></a>
@@ -263,6 +272,24 @@
                 {% endfor %}
                 {% endfor %}
             </tbody>
             </tbody>
         </table>
         </table>
+        <center>
+            <span class="step-links">
+                {% if page_obj.has_previous %}
+                    <a href="{% url 'public-index' %}?page=1">&laquo; first</a>
+                    <a href="{% url 'public-index' %}?page={{ page_obj.previous_page_number }}">previous</a>
+                {% endif %}
+        
+                <span class="current">
+                    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
+                </span>
+        
+                {% if page_obj.has_next %}
+                    <a href="{% url 'public-index' %}?page={{ page_obj.next_page_number }}">next </a>
+                    <a href="{% url 'public-index' %}?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
+                {% endif %}
+            </span>
+            <br>
+    </center>
         <footer>
         <footer>
             <br/>
             <br/>
             <center>
             <center>