Skip to content

Commit

Permalink
SNO2-21-files-crash (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
keenangraham authored Jan 24, 2023
1 parent dcc9618 commit 56ece8d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1

orbs:
browser-tools: circleci/browser-tools@1.0.1
browser-tools: circleci/browser-tools@1.4.0

executors:
snovault-executor:
Expand Down
12 changes: 12 additions & 0 deletions src/snovault/elasticsearch/searches/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ def render(self, *args, **kwargs):
return {
NON_SORTABLE: TEXT_FIELDS
}


class PassThroughResponseField(ResponseField):
'''
Passes input values (dictionary) to output.
'''
def __init__(self, *args, **kwargs):
self.values_to_pass_through = kwargs.pop('values_to_pass_through', {})
super().__init__(*args, **kwargs)

def render(self, *args, **kwargs):
return self.values_to_pass_through
43 changes: 33 additions & 10 deletions src/snovault/resource_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from past.builtins import basestring
from pyramid.exceptions import PredicateMismatch
from pyramid.httpexceptions import HTTPNotFound
from pyramid.httpexceptions import HTTPMovedPermanently
from pyramid.settings import asbool
from pyramid.view import (
render_view_to_response,
Expand All @@ -23,6 +24,9 @@
from snosearch.parsers import ParamsParser
from snosearch.parsers import QueryString
from snosearch.responses import FieldedResponse

from snovault.elasticsearch.searches.fields import PassThroughResponseField

from .calculated import calculate_properties
from .calculated import calculate_select_properties
from .calculated import calculate_filtered_properties
Expand Down Expand Up @@ -51,9 +55,7 @@ def remove_item_keys(item, request):
return item


@view_config(context=AbstractCollection, permission='list', request_method='GET',
name='listing_db')
def collection_view_listing_db(context, request):
def collection_view_listing_db_with_additional_properties(context, request, additional_properties):
result = {}

frame = request.params.get('frame', 'columns')
Expand Down Expand Up @@ -85,13 +87,20 @@ def collection_view_listing_db(context, request):
params.append(('limit', 'all'))
result['all'] = '%s?%s' % (request.resource_path(context), urlencode(params))

result.update(additional_properties)

return result


@view_config(context=AbstractCollection, permission='list', request_method='GET', name='listing')
def collection_view_listing_es(context, request):
@view_config(context=AbstractCollection, permission='list', request_method='GET',
name='listing_db')
def collection_view_listing_db(context, request):
return collection_view_listing_db_with_additional_properties(context, request, {})


def collection_view_listing_es_with_additional_properties(context, request, additional_properties):
if not hasattr(request, 'datastore') or request.datastore != ELASTIC_SEARCH:
return collection_view_listing_db(context, request)
return collection_view_listing_db_with_additional_properties(context, request, additional_properties)
fr = FieldedResponse(
_meta={
'params_parser': ParamsParser(request)
Expand All @@ -103,12 +112,20 @@ def collection_view_listing_es(context, request):
NotificationResponseField(),
FiltersResponseField(),
CollectionClearFiltersResponseField(),
ColumnsResponseField()
ColumnsResponseField(),
PassThroughResponseField(
values_to_pass_through=additional_properties,
),
]
)
return fr.render()


@view_config(context=AbstractCollection, permission='list', request_method='GET', name='listing')
def collection_view_listing_es(context, request):
return collection_view_listing_es_with_additional_properties(context, request, {})


@view_config(context=Root, request_method='GET', name='page')
def home(context, request):
properties = request.embed(request.resource_path(context), '@@object')
Expand Down Expand Up @@ -136,9 +153,15 @@ def collection_list(context, request):
if request.query_string:
properties['@id'] += '?' + request.query_string

result = request.embed(path, '@@listing?' + request.query_string, as_user=True)
result.update(properties)
return result
# Must check if canonical redirect needed before streaming response.
if request.path_qs != properties['@id']:
raise HTTPMovedPermanently(location=properties['@id'])

return collection_view_listing_es_with_additional_properties(
context,
request,
properties,
)


@view_config(context=Root, request_method='GET')
Expand Down
17 changes: 17 additions & 0 deletions src/snovault/tests/test_searches_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ def test_searches_fields_non_sortable_response_field(dummy_parent):
nrf = NonSortableResponseField()
r = nrf.render(parent=dummy_parent)
assert r['non_sortable'] == ['pipeline_error_detail', 'description', 'notes']


def test_searches_fields_pass_through_response_field():
from snovault.elasticsearch.searches.fields import PassThroughResponseField
ptrf = PassThroughResponseField(
values_to_pass_through={
'some': 'values',
'in': 'a',
'dictionary': [],
}
)
result = ptrf.render()
assert result == {
'some': 'values',
'in': 'a',
'dictionary': []
}

0 comments on commit 56ece8d

Please sign in to comment.