search view inherits from modified public view

This commit is contained in:
apkallum 2020-08-20 16:43:28 -04:00 committed by Cristian
parent 948b2469f6
commit c50af04cce
3 changed files with 70 additions and 13 deletions

View file

@ -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
from core.views import MainIndex, OldIndex, LinkDetails, PublicArchiveView, SearchResultsView
# print('DEBUG', settings.DEBUG)
@ -31,5 +31,6 @@ urlpatterns = [
path('index.html', RedirectView.as_view(url='/')),
path('index.json', static.serve, {'document_root': settings.OUTPUT_DIR, 'path': 'index.json'}),
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'),
]

View file

@ -3,9 +3,12 @@ __package__ = 'archivebox.core'
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.db.models import Q
from django.views import View, static
from django.views.generic.list import ListView
from django_datatables_view.base_datatable_view import BaseDatatableView
from core.models import Snapshot
from ..index import load_main_index, load_main_index_meta
@ -107,9 +110,35 @@ class LinkDetails(View):
class PublicArchiveView(ListView):
template = 'snapshot_list.html'
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