Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parasolr last modified mixin (#375) #669

Merged
merged 9 commits into from
Mar 2, 2022
36 changes: 36 additions & 0 deletions geniza/corpus/tests/test_corpus_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ def test_get_absolute_url(self, document):
document.get_absolute_url()
)

def test_last_modified(self, client, document, join):
"""Ensure that the last modified header is set in the HEAD response"""
SolrClient().update.index([document.index_data()], commit=True)
response = client.head(document.get_absolute_url())
assert response["Last-Modified"]
init_last_modified = response["Last-Modified"]

# Sleeping is required to ensure that the last modified header is different.
sleep(1)

# Ensure that only one last modified header is updated when a document is updated.
SolrClient().update.index([join.index_data()], commit=True)
updated_doc_response = client.head(join.get_absolute_url())
other_doc_response = client.head(document.get_absolute_url())
assert (
updated_doc_response["Last-Modified"] != other_doc_response["Last-Modified"]
)

kmcelwee marked this conversation as resolved.
Show resolved Hide resolved

@pytest.mark.django_db
def test_old_pgp_tabulate_data():
Expand Down Expand Up @@ -601,6 +619,24 @@ def test_dispatch(self, client):
# should not redirect
assert response.status_code == 200

def test_last_modified(self, client, document):
"""Ensure that the last modified header is set in the HEAD response"""
kmcelwee marked this conversation as resolved.
Show resolved Hide resolved
SolrClient().update.index([document.index_data()], commit=True)
response = client.head(reverse("corpus:document-search"))
assert response["Last-Modified"]
init_last_modified = response["Last-Modified"]

# Sleep for 1 second to ensure the last modified header is different
sleep(1)
kmcelwee marked this conversation as resolved.
Show resolved Hide resolved

# Ensure that a document being suppressed changes the last modified header
document.status = Document.SUPPRESSED
document.save()
SolrClient().update.index([document.index_data()], commit=True)
response = client.head(reverse("corpus:document-search"))
new_last_modified = response["Last-Modified"]
assert new_last_modified != init_last_modified


class TestDocumentScholarshipView:
def test_page_title(self, document, client, source):
Expand Down
15 changes: 11 additions & 4 deletions geniza/corpus/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.utils.translation import ngettext
from django.views.generic import DetailView, FormView, ListView
from django.views.generic.edit import FormMixin
from parasolr.django.views import SolrLastModifiedMixin
from piffle.presentation import IIIFPresentation
from tabular_export.admin import export_to_csv_response

Expand All @@ -27,7 +28,7 @@
from geniza.footnotes.models import Footnote


class DocumentSearchView(ListView, FormMixin):
class DocumentSearchView(ListView, FormMixin, SolrLastModifiedMixin):
kmcelwee marked this conversation as resolved.
Show resolved Hide resolved
model = Document
form_class = DocumentSearchForm
context_object_name = "documents"
Expand Down Expand Up @@ -192,8 +193,8 @@ def get_context_data(self, **kwargs):
return context_data


class DocumentPastIdMixin:
"""View mixin to handle redirects for documents with old PGPIDs.
class DocumentDetailBase(SolrLastModifiedMixin):
"""View mixin to handle lastmodified and redirects for documents with old PGPIDs.
Overrides get request in the case of a 404, looking for any records
with passed PGPID in old_pgpids, and if found, redirects to that document
with current PGPID."""
Expand All @@ -213,8 +214,14 @@ def get(self, request, *args, **kwargs):
# otherwise, continue raising the 404
raise

def get_solr_lastmodified_filters(self):
"""Query solr for the object that needs their "last_modified" attribute updated.
Overwrites `SolrLastModifiedMixin`'s builtin method.
"""
kmcelwee marked this conversation as resolved.
Show resolved Hide resolved
return {"pgpid_i": self.kwargs["pk"]}

class DocumentDetailView(DocumentPastIdMixin, DetailView):

class DocumentDetailView(DocumentDetailBase, DetailView):
"""public display of a single :class:`~geniza.corpus.models.Document`"""

model = Document
Expand Down