Skip to content

Commit

Permalink
handle eve req.projection (#97)
Browse files Browse the repository at this point in the history
* handle eve req.projection

* fix flake8 error
  • Loading branch information
petrjasek authored Mar 29, 2021
1 parent 1a66348 commit 0762f89
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
19 changes: 12 additions & 7 deletions eve_elastic/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,8 @@ def find(self, resource, req, sub_resource_lookup, **kwargs):
query["highlight"] = highlights
query["highlight"].setdefault("require_field_match", False)

source_projections = None
if self.should_project(req):
source_projections = self.get_projected_fields(req)

args = self._es_args(resource, source_projections=source_projections)
source_projection = self.get_projected_fields(req, resource)
args = self._es_args(resource, source_projections=source_projection)
default_params = self._get_default_search_params()
if args is not None:
default_params.update(args)
Expand Down Expand Up @@ -664,7 +661,7 @@ def should_project(self, req):
except (AttributeError, TypeError):
return False

def get_projected_fields(self, req):
def get_projected_fields(self, req, resource=None):
"""
Returns the projected fields from request.
Expand All @@ -673,7 +670,15 @@ def get_projected_fields(self, req):
args = getattr(req, "args", {})
return ",".join(json.loads(args.get("projections")) + [RESOURCE_FIELD])
except (AttributeError, TypeError):
return None
pass
if (
resource and req.projection
): # eve default behaviour, only allow for now when projection is set via req
client_projection = self._client_projection(req)
projection = self._datasource_ex(
resource, client_projection=client_projection
)[2]
return ",".join([key for key, val in projection.items() if val])

def find_one(self, resource, req, **lookup):
"""Find single document, if there is _id in lookup use that, otherwise filter."""
Expand Down
32 changes: 32 additions & 0 deletions test/test_elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,38 @@ def test_get_projected_fields(self):
fields, "priority,urgency,word_count,slugline,highlights,_resource"
)

def test_eve_projection(self):
with self.app.app_context():
self.app.data.insert(
"items",
[
{
"uri": "test",
"name": "foo",
"firstcreated": "2020-12-12T10:10:10+0000",
"_etag": "foo",
"_created": "2020-12-12T10:10:10+0000",
"_updated": "2020-12-12T10:10:10+0000",
}
],
)

req = ParsedRequest()
req.projection = json.dumps(
{
"name": 1,
}
)

items = self.app.data.find("items", req, None)
fields = items[0].keys()
self.assertIn("name", fields)
self.assertIn("_id", fields)
self.assertIn("_etag", fields)
self.assertIn("_created", fields)
self.assertIn("_updated", fields)
self.assertNotIn("firstcreated", fields)

def test_should_highlight(self):
with self.app.app_context():
req = ParsedRequest()
Expand Down

0 comments on commit 0762f89

Please sign in to comment.