This is an automated email from the ASF dual-hosted git repository. machristie pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git
commit 75a6925a84bafdd95e59ca48e724b15f6b58e5ba Author: Marcus Christie <machr...@iu.edu> AuthorDate: Wed Jul 11 12:28:59 2018 -0400 AIRAVATA-2835 Load all of the groups (disable pagination) --- .../django_airavata_api/js/service_config.js | 14 +++++++- .../js/services/ServiceFactory.js | 38 +++++++++++++++++----- django_airavata/apps/api/view_utils.py | 6 ++++ .../static/common/js/components/ShareButton.vue | 4 +-- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/django_airavata/apps/api/static/django_airavata_api/js/service_config.js b/django_airavata/apps/api/static/django_airavata_api/js/service_config.js index 55dc68f..d60f05f 100644 --- a/django_airavata/apps/api/static/django_airavata_api/js/service_config.js +++ b/django_airavata/apps/api/static/django_airavata_api/js/service_config.js @@ -1,3 +1,5 @@ +import Group from './models/Group' + const post = "post"; const get = "get"; const put = "put"; @@ -9,7 +11,9 @@ Generating Services based on the API view set { serviceName:{ url:'/example/api', -viewSet:true +viewSet:true, +pagination: true/false, +modelClass: ModelClass, } } Normal service configuration: @@ -20,6 +24,7 @@ url:'/example/api/<look_up>', # the <look_up> implies a path parameter lok_up requestType:'post', bodyParams:[...] # body parameter names for json parameter if body params id=s a list of array else an object with the param name for the body object queryParams:[] # list query param names/ query param name to param name mapping +pagination:true # whether to treat the response as a paginated response } @@ -47,4 +52,11 @@ export default { url: "/api/shared-entities", viewSet: true }, + "Groups": { + url: "/api/groups", + viewSet: true, + pagination: true, + queryParams: ['limit', 'offset'], + modelClass: Group, + }, } \ No newline at end of file diff --git a/django_airavata/apps/api/static/django_airavata_api/js/services/ServiceFactory.js b/django_airavata/apps/api/static/django_airavata_api/js/services/ServiceFactory.js index fd43220..cb78a37 100644 --- a/django_airavata/apps/api/static/django_airavata_api/js/services/ServiceFactory.js +++ b/django_airavata/apps/api/static/django_airavata_api/js/services/ServiceFactory.js @@ -38,11 +38,17 @@ const parseServiceMapping = function (serviceConfiguration) { if (!url.endsWith("/")) { url = url + "/"; } + let modelClass = serviceConfiguration.modelClass; + let queryParams = serviceConfiguration.queryParams; + let defaultPagination = (serviceConfiguration.pagination) ? true : false; delete serviceConfiguration.viewSet; delete serviceConfiguration.url; + delete serviceConfiguration.modelClass; + delete serviceConfiguration.queryParams; + delete serviceConfiguration.pagination; for (let supportedFunction of supportedFunctions) { let supportedFunctionName = supportedFunction - let pagination = false + let pagination = defaultPagination; if (typeof(supportedFunctionName) !== "string") { supportedFunctionName = supportedFunction.name pagination = supportedFunction.pagination @@ -52,6 +58,8 @@ const parseServiceMapping = function (serviceConfiguration) { serviceConfiguration["list"] = { url: url, requestType: getKey, + modelClass: modelClass, + queryParams: queryParams, } break; case "create": @@ -60,7 +68,9 @@ const parseServiceMapping = function (serviceConfiguration) { requestType: postKey, bodyParams: { name: "data" - } + }, + modelClass: modelClass, + queryParams: queryParams, } break; case "update": @@ -69,19 +79,25 @@ const parseServiceMapping = function (serviceConfiguration) { requestType: putKey, bodyParams: { name: "data" - } + }, + modelClass: modelClass, + queryParams: queryParams, } break; case "retrieve": serviceConfiguration["retrieve"] = { url: url + "<lookup>/", requestType: getKey, + modelClass: modelClass, + queryParams: queryParams, } break; case "delete": serviceConfiguration["delete"] = { url: url + "<lookup>/", requestType: delKey, + modelClass: modelClass, + queryParams: queryParams, } } serviceConfiguration[supportedFunctionName].pagination = pagination @@ -164,20 +180,24 @@ class ServiceFactory { } } let paginationHandler = (data) => { - if (config.pagination === true) { - return new PaginationIterator(data); + if (config.pagination === true && data.next) { + return new PaginationIterator(data, config.modelClass); + } else if (data instanceof Array) { + return data.map(item => resultHandler(item)); } else { - return data; + return resultHandler(data); } }; + let resultHandler = (data) => { + return (config.modelClass) ? new config.modelClass(data) : data; + } switch (config.requestType.toLowerCase()) { - // TODO: convert response to instances of model class case postKey: - return FetchUtils.post(url, bodyParams, queryParams).then(paginationHandler); + return FetchUtils.post(url, bodyParams, queryParams).then(resultHandler); case getKey: return FetchUtils.get(url, queryParams).then(paginationHandler); case putKey: - return FetchUtils.put(url, bodyParams); + return FetchUtils.put(url, bodyParams).then(resultHandler); case delKey: return FetchUtils.delete(url); } diff --git a/django_airavata/apps/api/view_utils.py b/django_airavata/apps/api/view_utils.py index 55c1b23..4278a1b 100644 --- a/django_airavata/apps/api/view_utils.py +++ b/django_airavata/apps/api/view_utils.py @@ -131,6 +131,12 @@ class APIResultPagination(pagination.LimitOffsetPagination): return list(queryset[self.offset:self.offset + self.limit]) + def get_limit(self, request): + # If limit <= 0 then don't paginate + if self.limit_query_param in request.query_params and int(request.query_params[self.limit_query_param]) <= 0: + return None + return super().get_limit(request) + def get_paginated_response(self, data): has_next_link = len(data) >= self.limit return Response(OrderedDict([ diff --git a/django_airavata/static/common/js/components/ShareButton.vue b/django_airavata/static/common/js/components/ShareButton.vue index 494f003..f161cbc 100644 --- a/django_airavata/static/common/js/components/ShareButton.vue +++ b/django_airavata/static/common/js/components/ShareButton.vue @@ -128,8 +128,8 @@ export default { } }, mounted: function() { - // TODO: fetch all groups, not just the first page of them - services.GroupService.list().then(groups => this.groups = groups.results); + // Load all of the groups + services.ServiceFactory.service("Groups").list({limit: -1}).then(groups => this.groups = groups); } } </script>