Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #294 from CSCfi/stable
Browse files Browse the repository at this point in the history
Merge stable to master
  • Loading branch information
hannu40k authored Jun 20, 2018
2 parents df9f809 + 643956f commit 44e355e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
18 changes: 17 additions & 1 deletion src/metax_api/api/rest/base/views/common_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,23 @@ def paginate_queryset(self, queryset):
return super(CommonViewSet, self).paginate_queryset(queryset)

def get_queryset(self):
"""
Apply parameters received in the request to the queryset, and return it.
Inheriting methods can still further filter and modify the queryset, since it is
not immediately evaluated.
"""
additional_filters = {}
q_filters = []

CS.set_if_modified_since_filter(self.request, additional_filters)

if hasattr(self, 'queryset_search_params'):
additional_filters.update(**self.queryset_search_params)

if 'q_filters' in additional_filters:
# Q-filter objects, which can contain more complex filter options such as OR-clauses
q_filters = additional_filters.pop('q_filters')

if CS.get_boolean_query_param(self.request, 'removed'):
additional_filters.update({'removed': True})
Expand All @@ -84,7 +100,7 @@ def get_queryset(self):

return super(CommonViewSet, self).get_queryset() \
.select_related(*self.select_related) \
.filter(**additional_filters)
.filter(*q_filters, **additional_filters)

def get_object(self, search_params=None):
"""
Expand Down
15 changes: 0 additions & 15 deletions src/metax_api/api/rest/base/views/dataset_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,6 @@ def get_object(self):

return self._search_using_dataset_identifiers()

def get_queryset(self):
additional_filters = {}
q_filters = []

if hasattr(self, 'queryset_search_params'):
additional_filters.update(**self.queryset_search_params)

if 'q_filters' in additional_filters:
# Q-filter objects, which can contain more complex filter options such as OR-clauses
q_filters = additional_filters.pop('q_filters')

CS.set_if_modified_since_filter(self.request, additional_filters)

return super(DatasetViewSet, self).get_queryset().filter(*q_filters, **additional_filters)

def retrieve(self, request, *args, **kwargs):
self.queryset_search_params = {}
CS.set_if_modified_since_filter(self.request, self.queryset_search_params)
Expand Down
8 changes: 6 additions & 2 deletions src/metax_api/api/rest/base/views/file_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def dispatch(self, request, **kwargs):
with transaction.atomic():
return super().dispatch(request, **kwargs)

def list(self, request, *args, **kwargs):
self.queryset_search_params = FileService.get_queryset_search_params(request)
return super().list(request, *args, **kwargs)

@list_route(methods=['post'], url_path="datasets")
def datasets(self, request):
"""
Expand Down Expand Up @@ -189,8 +193,8 @@ def flush_project(self, request): # pragma: no cover
for host in app_config_dict['ALLOWED_HOSTS']:
if 'metax.csc.local' in host or 'metax-test' in host or 'metax-stable' in host:
break
else:
raise ValidationError('API currently allowed only in test environments')
# else:
# raise ValidationError('API currently allowed only in test environments')

if 'project' not in request.query_params:
raise ValidationError('project is a required query parameter')
Expand Down
16 changes: 16 additions & 0 deletions src/metax_api/services/file_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ class MaxRecursionDepthExceeded(Exception):

class FileService(CommonService):

@classmethod
def get_queryset_search_params(cls, request):
"""
Get and validate parameters from request.query_params that will be used for filtering
in view.get_queryset()
"""
if not request.query_params:
return {}

queryset_search_params = {}

if request.query_params.get('project_identifier', False):
queryset_search_params['project_identifier'] = request.query_params['project_identifier']

return queryset_search_params

@classmethod
def get_datasets_where_file_belongs_to(cls, file_identifiers):
"""
Expand Down
7 changes: 7 additions & 0 deletions src/metax_api/tests/api/rest/base/views/files/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def test_read_file_list(self):
response = self.client.get('/rest/files')
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_read_file_list_filter_by_project(self):
proj = File.objects.get(pk=1).project_identifier
file_count = File.objects.filter(project_identifier=proj).count()
response = self.client.get('/rest/files?project_identifier=%s' % proj)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], file_count)

def test_read_file_details_by_pk(self):
response = self.client.get('/rest/files/%s' % self.pk)
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down
5 changes: 5 additions & 0 deletions swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ paths:
get:
summary: get list of files
parameters:
- name: project_identifier
in: query
description: filter files by project
required: false
type: string
- name: offset
in: query
description: offset for paging
Expand Down

0 comments on commit 44e355e

Please sign in to comment.