Skip to content

Commit 854934a

Browse files
authored
Merge branch 'master' into master
2 parents 9d65dd4 + 1fda952 commit 854934a

File tree

6 files changed

+33
-18
lines changed

6 files changed

+33
-18
lines changed

docs/filtering.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ equals to:
149149

150150
.. sourcecode:: http
151151

152-
GET /persons?filter[name]=[{"name":"name","op":"eq","val":"John"}] HTTP/1.1
152+
GET /persons?filter=[{"name":"name","op":"eq","val":"John"}] HTTP/1.1
153153
Accept: application/vnd.api+json
154154

155155

@@ -164,4 +164,4 @@ which equals to:
164164

165165
.. sourcecode:: http
166166

167-
GET /persons?filter[name]=[{"name":"name","op":"eq","val":"John"}, {"name":"gender","op":"eq","val":"male"}] HTTP/1.1
167+
GET /persons?filter=[{"name":"name","op":"eq","val":"John"}, {"name":"gender","op":"eq","val":"male"}] HTTP/1.1

examples/api_nested.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ class PersonRelationship(ResourceRelationship):
144144
data_layer = {'session': db.session,
145145
'model': Person}
146146

147+
class PersonTagList(ResourceList):
148+
schema = PersonTagSchema
149+
data_layer = {'session': db.session,
150+
'model': Person_Tag}
151+
152+
class PersonTagDetail(ResourceDetail):
153+
schema = PersonTagSchema
154+
data_layer = {'session': db.session,
155+
'model': Person_Tag}
147156

148157
class ComputerList(ResourceList):
149158
def query(self, view_kwargs):
@@ -190,6 +199,9 @@ class ComputerRelationship(ResourceRelationship):
190199
api.route(ComputerDetail, 'computer_detail', '/computers/<int:id>')
191200
api.route(ComputerRelationship, 'computer_person', '/computers/<int:id>/relationships/owner')
192201

202+
api.route(PersonTagList, 'person_tag_list', '/persontags')
203+
api.route(PersonTagDetail, 'person_tag_detail', '/persontags/<int:id>')
204+
193205
if __name__ == '__main__':
194206
# Start application
195207
app.run(debug=True)

flask_rest_jsonapi/data_layers/alchemy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def get_object(self, view_kwargs, qs=None):
8787

8888
query = self.retrieve_object_query(view_kwargs, filter_field, filter_value)
8989

90-
if qs is not None:
90+
if qs is not None and getattr(self, 'eagerload_includes', True):
9191
query = self.eagerload_includes(query, qs)
9292

9393
try:

flask_rest_jsonapi/data_layers/filtering/alchemy.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sqlalchemy import and_, or_, not_
66

77
from flask_rest_jsonapi.exceptions import InvalidFilters
8-
from flask_rest_jsonapi.schema import get_relationships, get_model_field
8+
from flask_rest_jsonapi.schema import get_relationships, get_nested_fields, get_model_field
99

1010

1111
def create_filters(model, filter_info, resource):
@@ -143,26 +143,27 @@ def value(self):
143143

144144
@property
145145
def related_model(self):
146-
"""Get the related model of a relationship field
146+
"""Get the related model of a related (relationship or nested) field
147147
148148
:return DeclarativeMeta: the related model
149149
"""
150-
relationship_field = self.name
150+
related_field_name = self.name
151151

152-
if relationship_field not in get_relationships(self.schema):
153-
raise InvalidFilters("{} has no relationship attribute {}".format(self.schema.__name__, relationship_field))
152+
related_fields = get_relationships(self.schema) + get_nested_fields(self.schema)
153+
if related_field_name not in related_fields:
154+
raise InvalidFilters("{} has no relationship or nested attribute {}".format(self.schema.__name__, related_field_name))
154155

155-
return getattr(self.model, get_model_field(self.schema, relationship_field)).property.mapper.class_
156+
return getattr(self.model, get_model_field(self.schema, related_field_name)).property.mapper.class_
156157

157158
@property
158159
def related_schema(self):
159-
"""Get the related schema of a relationship field
160+
"""Get the related schema of a related (relationship or nested) field
160161
161162
:return Schema: the related schema
162163
"""
163-
relationship_field = self.name
164+
related_field_name = self.name
165+
related_fields = get_relationships(self.schema) + get_nested_fields(self.schema)
166+
if related_field_name not in related_fields:
167+
raise InvalidFilters("{} has no relationship or nested attribute {}".format(self.schema.__name__, related_field_name))
164168

165-
if relationship_field not in get_relationships(self.schema):
166-
raise InvalidFilters("{} has no relationship attribute {}".format(self.schema.__name__, relationship_field))
167-
168-
return self.schema._declared_fields[relationship_field].schema.__class__
169+
return self.schema._declared_fields[related_field_name].schema.__class__

flask_rest_jsonapi/decorators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def check_headers(func):
2121
@wraps(func)
2222
def wrapper(*args, **kwargs):
2323
if request.method in ('POST', 'PATCH'):
24-
if 'Content-Type' in request.headers and\
25-
'application/vnd.api+json' in request.headers['Content-Type'] and\
24+
if 'Content-Type' not in request.headers or\
25+
'application/vnd.api+json' not in request.headers['Content-Type'] or\
2626
request.headers['Content-Type'] != 'application/vnd.api+json':
2727
error = json.dumps(jsonapi_errors([{'source': '',
2828
'detail': "Content-Type header must be application/vnd.api+json",
@@ -80,7 +80,7 @@ def wrapper(*args, **kwargs):
8080
e.status,
8181
headers)
8282
except Exception as e:
83-
if current_app.config['DEBUG'] is True:
83+
if current_app.config['DEBUG'] is True or current_app.config.get('PROPAGATE_EXCEPTIONS') is True:
8484
raise
8585

8686
if 'sentry' in current_app.extensions:

flask_rest_jsonapi/resource.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ def post(self, *args, **kwargs):
148148
json_data = request.get_json() or {}
149149

150150
qs = QSManager(request.args, self.schema)
151+
152+
self.before_marshmallow(args, kwargs)
151153

152154
schema = compute_schema(self.schema,
153155
getattr(self, 'post_schema_kwargs', dict()),

0 commit comments

Comments
 (0)