Skip to content

Commit

Permalink
Merge pull request #10 from einarhuseby/deprecation_warning_newer_pym…
Browse files Browse the repository at this point in the history
…ongo_#1202

Deprecation warning newer pymongo pyeve#1202
  • Loading branch information
einarhuseby committed Jan 30, 2019
2 parents 13abdbc + 8eb6100 commit b6f02f9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
14 changes: 11 additions & 3 deletions eve/default_settings.py
Expand Up @@ -11,6 +11,8 @@
:copyright: (c) 2017 by Nicola Iarocci.
:license: BSD, see LICENSE for more details.
.. versionchanged:: 0.8.2
'PAGINATION_STRATEGY' options are None, full and estimate and set to full.
.. versionchanged:: 0.8
'RENDERERS' added with XML and JSON renderers.
'JSON' removed.
Expand Down Expand Up @@ -164,6 +166,15 @@
PAGINATION = True # pagination enabled by default.
PAGINATION_LIMIT = 50
PAGINATION_DEFAULT = 25
HEADER_TOTAL_COUNT = "X-Total-Count"

# http://api.mongodb.com/python/current/api/pymongo/collection.html
# full=count_documents(filter, session=None, **kwargs) interesting options are hint=index name or index spec and maxTimeMS
# estimated=estimated_document_count(**kwargs) also maxTimeMS can be given!
# None=None
PAGINATION_STRATEGY = "full"

OPTIMIZE_PAGINATION_FOR_SPEED = False
VERSIONING = False # turn document versioning on or off.
VERSIONS = "_versions" # suffix for parallel collection w/old versions
VERSION_PARAM = "version" # URL param for specific version of a document.
Expand Down Expand Up @@ -238,9 +249,6 @@
QUERY_EMBEDDED = "embedded"
QUERY_AGGREGATION = "aggregate"

HEADER_TOTAL_COUNT = "X-Total-Count"
OPTIMIZE_PAGINATION_FOR_SPEED = False

# user-restricted resource access is disabled by default.
AUTH_FIELD = None

Expand Down
29 changes: 28 additions & 1 deletion eve/io/mongo/mongo.py
Expand Up @@ -149,6 +149,10 @@ def find(self, resource, req, sub_resource_lookup):
:param req: a :class:`ParsedRequest`instance.
:param sub_resource_lookup: sub-resource lookup from the endpoint url.
.. versionchanged:: 0.8.2
Datalayer breaking changes for count using PyMongo>=3.7
Support for PAGINATION_STRATEGY
.. versionchanged:: 0.6
Support for multiple databases.
Filter soft deleted documents by default
Expand Down Expand Up @@ -249,7 +253,30 @@ def find(self, resource, req, sub_resource_lookup):
if projection:
args["projection"] = projection

return self.pymongo(resource).db[datasource].find(**args)
# pagination_strategy for resource
if config.DOMAIN[resource].get("pagination_strategy", None) is not None:
pagination_strategy = config.DOMAIN[resource]["pagination_strategy"]
# Global PAGINATION_STRATEGY
elif config.PAGINATION_STRATEGY is not None:
pagination_strategy = config.PAGINATION_STRATEGY
else:
pagination_strategy = None

# Execute strategy
if pagination_strategy == "full" or (
pagination_strategy == "estimated" and len(spec) > 0
):
return (
self.pymongo(resource).db[datasource].count_documents(filter=spec),
self.pymongo(resource).db[datasource].find(**args),
)
elif pagination_strategy == "estimated":
return (
self.pymongo(resource).db[datasource].estimated_document_count(),
self.pymongo(resource).db[datasource].find(**args),
)
else:
return None, self.pymongo(resource).db[datasource].find(**args)

def find_one(
self,
Expand Down
10 changes: 6 additions & 4 deletions eve/methods/get.py
Expand Up @@ -214,7 +214,7 @@ def _perform_find(resource, lookup):
# If-Modified-Since disabled on collections (#334)
req.if_modified_since = None

cursor = app.data.find(resource, req, lookup)
count, cursor = app.data.find(resource, req, lookup)
# If soft delete is enabled, data.find will not include items marked
# deleted unless req.show_deleted is True
for document in cursor:
Expand All @@ -231,10 +231,13 @@ def _perform_find(resource, lookup):

response[config.ITEMS] = documents

"""
if config.OPTIMIZE_PAGINATION_FOR_SPEED:
count = None
else:
count = cursor.count(with_limit_and_skip=False)
"""
if count is not None:
headers.append((config.HEADER_TOTAL_COUNT, count))

if config.DOMAIN[resource]["hateoas"]:
Expand Down Expand Up @@ -406,11 +409,11 @@ def getitem_internal(resource, **lookup):
# default sort for 'all', required sort for 'diffs'
req.sort = '[("%s", 1)]' % config.VERSION
req.if_modified_since = None # we always want the full history here
cursor = app.data.find(resource + config.VERSIONS, req, lookup)
count, cursor = app.data.find(resource + config.VERSIONS, req, lookup)

# build all versions
documents = []
if cursor.count() == 0:
if count == 0:
# this is the scenario when the document existed before
# document versioning got turned on
documents.append(latest_doc)
Expand Down Expand Up @@ -462,7 +465,6 @@ def getitem_internal(resource, **lookup):
if config.DOMAIN[resource]["hateoas"]:
# use the id of the latest document for multi-document requests
if cursor:
count = cursor.count(with_limit_and_skip=False)
response[config.LINKS] = _pagination_links(
resource, req, count, latest_doc[resource_def["id_field"]]
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -15,7 +15,7 @@
"cerberus>=1.1",
"events>=0.3,<0.4",
"flask>=1.0",
"pymongo>=3.5",
"pymongo>=3.7",
"simplejson>=3.3.0,<4.0",
]

Expand Down

0 comments on commit b6f02f9

Please sign in to comment.