Skip to content

Commit 0125617

Browse files
author
Colton Provias
committed
Get Collection finalized.
1 parent ddf18e0 commit 0125617

1 file changed

Lines changed: 34 additions & 12 deletions

File tree

sqlalchemy_jsonapi/serializer.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,43 +595,65 @@ def delete_resource(self, session, data, model, resource):
595595

596596
@inject_model
597597
def get_collection(self, session, query, model):
598+
"""
599+
Fetch a collection of resources of a specified type.
600+
601+
:param session: SQLAlchemy session
602+
:param data: Dict of query args
603+
:param params: Keyword arguments
604+
"""
598605
include = self._parse_include(query.get('include', '').split(','))
599606
fields = self._parse_fields(query)
600-
response = JSONAPIResponse()
601-
response.data = {'data': []}
602607
included = {}
603-
view_perm = get_permission_test(model, None, Permissions.VIEW)
604-
collection = session.query(model)
605608
sorts = query.get('sort', '').split(',')
606609
order_by = []
610+
611+
collection = session.query(model)
612+
607613
for attr in sorts:
608614
if attr == '':
609615
break
610-
attr_name, is_asc = [attr[1:], False] if attr[0] == '-' else [attr,
611-
True]
612-
if attr_name not in model.__mapper__.all_orm_descriptors.keys(
613-
) or not hasattr(
614-
model,
615-
attr_name) or attr_name in model.__mapper__.relationships.keys(
616-
):
616+
617+
attr_name, is_asc = [attr[1:], False]\
618+
if attr[0] == '-'\
619+
else [attr, True]
620+
621+
if attr_name not in model.__mapper__.all_orm_descriptors.keys()\
622+
or not hasattr(model, attr_name)\
623+
or attr_name in model.__mapper__.relationships.keys():
617624
return NotSortableError(model, attr_name)
625+
618626
attr = getattr(model, attr_name)
619627
if not hasattr(attr, 'asc'):
620628
return NotSortableError(model, attr_name)
629+
630+
check_permission(model, attr_name, Permissions.VIEW)
631+
621632
order_by.append(attr.asc() if is_asc else attr.desc())
633+
622634
if len(order_by) > 0:
623635
collection = collection.order_by(*order_by)
636+
624637
pos = -1
625638
start, end = self._parse_page(query)
639+
640+
response = JSONAPIResponse()
641+
response.data = {'data': []}
642+
626643
for instance in collection:
627-
if not view_perm(instance):
644+
try:
645+
check_permission(instance, None, Permissions.VIEW)
646+
except PermissionDeniedError:
628647
continue
648+
629649
pos += 1
630650
if end is not None and (pos < start or pos > end):
631651
continue
652+
632653
built = self._render_full_resource(instance, include, fields)
633654
included.update(built.pop('included'))
634655
response.data['data'].append(built)
656+
635657
response.data['included'] = list(included.values())
636658
return response
637659

0 commit comments

Comments
 (0)