Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #11 from banders/master
Browse files Browse the repository at this point in the history
Fixed a bug that arises when new resources of type 'openapi-json' are created
  • Loading branch information
kfishwick authored Jan 19, 2017
2 parents 9f10048 + b1a9ead commit 875c4fc
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions ckanext/openapi/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class OpenApiViewPlugin(p.SingletonPlugin):
p.implements(p.IResourceView, inherit=True)
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IConfigurable, inherit=True)
p.implements(p.IPackageController, inherit=True)
p.implements(p.IResourceController, inherit=True)

#constants
supported_formats = ['openapi-json']
view_type = 'openapi_view';

# IConfigurer

Expand All @@ -38,7 +39,7 @@ def configure(self, config):
# IResourceView (CKAN >=2.3)

def info(self):
return {'name': 'openapi_view',
return {'name': self.view_type,
'title': 'OpenAPI Console',
'icon': 'file',
'iframed': True,
Expand All @@ -56,24 +57,40 @@ def can_view(self, data_dict):
def view_template(self, context, data_dict):
return 'dataviewer/openapi_view.html'

def add_default_views(self, context, data_dict):
if "resources" not in data_dict:
return

resources = data_dict["resources"]
for resource in resources:
if self.can_view({'package': data_dict, 'resource': resource}):
#add the openapi resource view for this resource
view = {'title': 'OpenAPI Console',
'description': '',
'resource_id': resource['id'],
'view_type': 'openapi_view'}
p.toolkit.get_action('resource_view_create')(
{'defer_commit': True}, view
# IResourceController

def after_update(self, context, resource):
self.add_default_views(context, resource)

def after_create(self, context, resource):
self.add_default_views(context, resource)

# Other functions

#returns true if the resource already has a view of type 'openapi_view',
#and false otherwise
def has_view_already(self, context, resource):
if 'id' in resource:
params = {'id': resource['id']}
resourceViews = p.toolkit.get_action('resource_view_list')(
context, params
)
for resourceView in resourceViews:
if "view_type" in resourceView and resourceView["view_type"] == self.view_type:
return True

return False

def add_default_views(self, context, resource):

if 'id' in resource and self.can_view({'resource': resource}) and not self.has_view_already(context, resource):

def after_update(self, context, data_dict):
self.add_default_views(context, data_dict)
#add the openapi resource view for this resource
view = {'title': 'OpenAPI Console',
'description': '',
'resource_id': resource['id'],
'view_type': self.view_type}
p.toolkit.get_action('resource_view_create')(
context, view
)

def after_create(self, context, data_dict):
self.add_default_views(context, data_dict)

0 comments on commit 875c4fc

Please sign in to comment.