Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Some attributes don't make sense if we don't have a request (#126)
Browse files Browse the repository at this point in the history
If we try to resolve `params` or `raw_data` without an active request context, raise `AttributeError`.
  • Loading branch information
jpmelos committed Feb 25, 2020
1 parent 9a04099 commit 268455c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion flask_mongorest/resources.py
Expand Up @@ -3,7 +3,7 @@

from bson.dbref import DBRef
from bson.objectid import ObjectId
from flask import request, url_for
from flask import has_request_context, request, url_for
try:
from urllib.parse import urlparse
except ImportError: # Python 2
Expand Down Expand Up @@ -131,6 +131,10 @@ def params(self):
2. As a _params property in the JSON payload. For example:
{ '_params': { 'status': 'active', '_limit': '10' } }
"""
if not has_request_context():
# `params` doesn't make sense if we don't have a request
raise AttributeError

if not hasattr(self, '_params'):
if '_params' in self.raw_data:
self._params = self.raw_data['_params']
Expand All @@ -157,6 +161,10 @@ def _enforce_strict_json(self, val):
@property
def raw_data(self):
"""Validate and return parsed JSON payload."""
if not has_request_context():
# `raw_data` doesn't make sense if we don't have a request
raise AttributeError

if not hasattr(self, '_raw_data'):
if request.method in ('PUT', 'POST') or request.data:
if request.mimetype and 'json' not in request.mimetype:
Expand Down

0 comments on commit 268455c

Please sign in to comment.