Skip to content

Commit

Permalink
Merge pull request #439 from devsagul/408-basequeryset-get-or-404-inc…
Browse files Browse the repository at this point in the history
…orrectly-handles-message

FIXED: BaseQuerySet.get_or_404 use 'message' keyword argument for Document searching
  • Loading branch information
insspb committed Jul 1, 2022
2 parents 2bdd46c + 2c89ba3 commit 46e5643
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/api/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ flask_mongoengine.sessions module
Module contents
---------------

.. note::
Parent `mongoengine <https://docs.mongoengine.org/>`_ project docs/docstrings has
some formatting issues. If class/function/method link not clickable, search on
provided parent documentation manually.

.. automodule:: flask_mongoengine
:members:
:undoc-members:
:show-inheritance:
:private-members: _abort_404
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,12 @@
"flask": ("https://flask.palletsprojects.com/en/2.1.x/", None),
"werkzeug": ("https://werkzeug.palletsprojects.com/en/2.1.x/", None),
"pymongo": ("https://pymongo.readthedocs.io/en/stable/", None),
"mongoengine": ("https://docs.mongoengine.org/", None),
}
myst_enable_extensions = [
"tasklist",
"strikethrough",
"fieldlist",
]
myst_heading_anchors = 3
suppress_warnings=["autodoc"]
37 changes: 27 additions & 10 deletions flask_mongoengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,40 @@ def connection(self):


class BaseQuerySet(QuerySet):
"""Mongoengine's queryset extended with handy extras."""
"""Extends :class:`~mongoengine.queryset.QuerySet` class with handly methods."""

def get_or_404(self, *args, **kwargs):
def _abort_404(self, _message_404):
"""Returns 404 error with message, if message provided.
:param _message_404: Message for 404 comment
"""
Get a document and raise a 404 Not Found error if it doesn't
exist.
abort(404, _message_404) if _message_404 else abort(404)

def get_or_404(self, *args, _message_404=None, **kwargs):
"""Get a document and raise a 404 Not Found error if it doesn't exist.
:param _message_404: Message for 404 comment, not forwarded to
:func:`~mongoengine.queryset.QuerySet.get`
:param args: args list, silently forwarded to
:func:`~mongoengine.queryset.QuerySet.get`
:param kwargs: keywords arguments, silently forwarded to
:func:`~mongoengine.queryset.QuerySet.get`
"""
try:
return self.get(*args, **kwargs)
except DoesNotExist:
message = kwargs.get("message", None)
abort(404, message) if message else abort(404)
self._abort_404(_message_404)

def first_or_404(self, message=None):
"""Same as get_or_404, but uses .first, not .get."""
obj = self.first()
return obj if obj else abort(404, message) if message else abort(404)
def first_or_404(self, _message_404=None):
"""
Same as :func:`~BaseQuerySet.get_or_404`, but uses
:func:`~mongoengine.queryset.QuerySet.first`, not
:func:`~mongoengine.queryset.QuerySet.get`.
:param _message_404: Message for 404 comment, not forwarded to
:func:`~mongoengine.queryset.QuerySet.get`
"""
return self.first() or self._abort_404(_message_404)

def paginate(self, page, per_page, **kwargs):
"""
Expand Down

0 comments on commit 46e5643

Please sign in to comment.