Skip to content

Commit

Permalink
oai-pmh follows the switch
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed May 5, 2021
1 parent 2e2f385 commit 7c31d76
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
20 changes: 15 additions & 5 deletions share/oaipmh/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from share.oaipmh.legacy.repository import OAIRepository as LegacyRepository
from share.oaipmh.fmr_repository import OaiPmhRepository
from share.search.elastic_manager import ElasticManager


class OAIPMHView(View):
Expand All @@ -16,14 +17,23 @@ def post(self, request):
return self.oai_response(**request.POST)

def oai_response(self, **kwargs):
use_legacy_pipeline = (
settings.SHARE_LEGACY_PIPELINE
and not kwargs.pop('pls_trove', False)
)
if use_legacy_pipeline:
use_legacy_repository = self._should_use_legacy_repository(kwargs.pop('pls_trove', False))

if use_legacy_repository:
repository = LegacyRepository()
else:
repository = OaiPmhRepository()

xml = repository.handle_request(self.request, kwargs)
return HttpResponse(xml, content_type=self.CONTENT_TYPE)

def _should_use_legacy_repository(self, pls_trove):
if pls_trove or not settings.SHARE_LEGACY_PIPELINE:
return False

# TEMPORARY HACK -- i mean it this time -- very specific to May 2021
# check whether the primary elasticsearch alias points at the "old" or "new" index
primary_indexes = ElasticManager().get_primary_indexes()

# old index name from settings.ELASTICSEARCH['INDEXES']
return (primary_indexes == ['share_customtax_1'])
15 changes: 9 additions & 6 deletions share/search/elastic_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,19 @@ def send_actions_sync(self, actions):
def refresh_indexes(self, index_names):
self.es_client.indices.refresh(index=','.join(index_names))

def update_primary_alias(self, primary_index_name):
def get_primary_indexes(self):
alias = settings.ELASTICSEARCH['PRIMARY_INDEX']

previous_indexes = []

try:
existing_aliases = self.es_client.indices.get_alias(name=alias)
previous_indexes = list(existing_aliases.keys())
aliases = self.es_client.indices.get_alias(name=alias)
return list(aliases.keys())
except NotFoundError:
pass
return []

def update_primary_alias(self, primary_index_name):
alias = settings.ELASTICSEARCH['PRIMARY_INDEX']

previous_indexes = self.get_primary_indexes()

if previous_indexes == [primary_index_name]:
logger.warn(f'index {primary_index_name} is already the primary')
Expand Down
8 changes: 8 additions & 0 deletions tests/share/test_oaipmh.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import pytest
import random
from lxml import etree
from unittest.mock import patch

from django.test.client import Client

from share import models
from share.oaipmh.util import format_datetime
from share.oaipmh.views import OAIPMHView
from share.util import IDObfuscator

from tests.share.models import factories
Expand All @@ -20,6 +22,12 @@
}


@pytest.fixture(autouse=True)
def ensure_legacy_oaipmh():
with patch.object(OAIPMHView, '_should_use_legacy_repository', return_value=True):
yield


@pytest.fixture
def oai_works():
return [factories.PreprintFactory() for i in range(17)]
Expand Down
12 changes: 8 additions & 4 deletions tests/share/test_oaipmh_trove.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import pytest
import random
from lxml import etree
from unittest.mock import patch

from django.test.client import Client

from share.models import SourceUniqueIdentifier, FormattedMetadataRecord
from share.oaipmh.util import format_datetime
from share.oaipmh.views import OAIPMHView
from share.util import IDObfuscator

from tests.factories import FormattedMetadataRecordFactory, SourceFactory
Expand All @@ -20,12 +22,14 @@
}


@pytest.fixture(autouse=True)
def ensure_non_legacy_oaipmh():
with patch.object(OAIPMHView, '_should_use_legacy_repository', return_value=False):
yield


def oai_request(data, pls_post, expect_errors=False):
client = Client()
data = {
**data,
'pls_trove': True,
}
if pls_post:
response = client.post('/oai-pmh/', data)
else:
Expand Down

0 comments on commit 7c31d76

Please sign in to comment.