Skip to content

Commit

Permalink
- Swagger OAS3
Browse files Browse the repository at this point in the history
  • Loading branch information
afabiani committed Dec 2, 2020
1 parent 7c842d0 commit 399b420
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 48 deletions.
29 changes: 9 additions & 20 deletions geonode/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
#########################################################################
from tastypie.api import Api
from dynamic_rest import routers
from drf_spectacular.views import (
SpectacularAPIView,
SpectacularRedocView,
SpectacularSwaggerView
)

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from django.conf.urls import url
from django.urls import path

from . import api as resources
from . import resourcebase_api as resourcebase_resources
Expand All @@ -48,21 +50,8 @@

router = routers.DynamicRouter()

schema_view = get_schema_view(
openapi.Info(
title="GeoNode REST API",
default_version='v2',
description="Application for serving and sharing geospatial data",
terms_of_service="https://github.com/GeoNode/geonode/wiki/Community-Bylaws",
contact=openapi.Contact(email="[email protected]"),
license=openapi.License(name="GPL License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
path('schema/', SpectacularAPIView.as_view(), name='schema'),
path('schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
path('schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
]
34 changes: 17 additions & 17 deletions geonode/base/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from django.conf import settings
from django.contrib.auth import get_user_model

from drf_yasg.utils import swagger_auto_schema
from drf_spectacular.utils import extend_schema
from dynamic_rest.viewsets import DynamicModelViewSet
from dynamic_rest.filters import DynamicFilterBackend, DynamicSortingFilter

Expand Down Expand Up @@ -70,8 +70,8 @@ def get_queryset(self):
queryset = self.get_serializer_class().setup_eager_loading(queryset)
return queryset

@swagger_auto_schema(methods=['get'], responses={200: ResourceBaseSerializer(many=True)},
operation_description="API endpoint allowing to retrieve the Resources visible to the user.")
@extend_schema(methods=['get'], responses={200: ResourceBaseSerializer(many=True)},
description="API endpoint allowing to retrieve the Resources visible to the user.")
@action(detail=True, methods=['get'])
def resources(self, request, pk=None):
user = self.get_object()
Expand All @@ -86,8 +86,8 @@ def resources(self, request, pk=None):
private_groups_not_visibile=settings.GROUP_PRIVATE_RESOURCES)
return Response(ResourceBaseSerializer(embed=True, many=True).to_representation(resources))

@swagger_auto_schema(methods=['get'], responses={200: GroupProfileSerializer(many=True)},
operation_description="API endpoint allowing to retrieve the Groups the user is member of.")
@extend_schema(methods=['get'], responses={200: GroupProfileSerializer(many=True)},
description="API endpoint allowing to retrieve the Groups the user is member of.")
@action(detail=True, methods=['get'])
def groups(self, request, pk=None):
user = self.get_object()
Expand All @@ -106,24 +106,24 @@ class GroupViewSet(DynamicModelViewSet):
serializer_class = GroupProfileSerializer
pagination_class = GeoNodeApiPagination

@swagger_auto_schema(methods=['get'], responses={200: UserSerializer(many=True)},
operation_description="API endpoint allowing to retrieve the Group members.")
@extend_schema(methods=['get'], responses={200: UserSerializer(many=True)},
description="API endpoint allowing to retrieve the Group members.")
@action(detail=True, methods=['get'])
def members(self, request, pk=None):
group = self.get_object()
members = get_user_model().objects.filter(id__in=group.member_queryset().values_list("user", flat=True))
return Response(UserSerializer(embed=True, many=True).to_representation(members))

@swagger_auto_schema(methods=['get'], responses={200: UserSerializer(many=True)},
operation_description="API endpoint allowing to retrieve the Group managers.")
@extend_schema(methods=['get'], responses={200: UserSerializer(many=True)},
description="API endpoint allowing to retrieve the Group managers.")
@action(detail=True, methods=['get'])
def managers(self, request, pk=None):
group = self.get_object()
managers = group.get_managers()
return Response(UserSerializer(embed=True, many=True).to_representation(managers))

@swagger_auto_schema(methods=['get'], responses={200: ResourceBaseSerializer(many=True)},
operation_description="API endpoint allowing to retrieve the Group specific resources.")
@extend_schema(methods=['get'], responses={200: ResourceBaseSerializer(many=True)},
description="API endpoint allowing to retrieve the Group specific resources.")
@action(detail=True, methods=['get'])
def resources(self, request, pk=None):
group = self.get_object()
Expand All @@ -142,8 +142,8 @@ class ResourceBaseViewSet(DynamicModelViewSet):
serializer_class = ResourceBaseSerializer
pagination_class = GeoNodeApiPagination

@swagger_auto_schema(methods=['get'], responses={200: PermSpecSerialiazer()},
operation_description="""
@extend_schema(methods=['get'], responses={200: PermSpecSerialiazer()},
description="""
Gets an object's the permission levels based on the perm_spec JSON.
the mapping looks like:
Expand Down Expand Up @@ -180,10 +180,10 @@ def get_perms(self, request, pk=None):
perms_spec_obj["groups"][str(group)] = perms
return Response(perms_spec_obj)

@swagger_auto_schema(methods=['put'],
request_body=PermSpecSerialiazer(),
responses={200: None},
operation_description="""
@extend_schema(methods=['put'],
request=PermSpecSerialiazer(),
responses={200: None},
description="""
Sets an object's the permission levels based on the perm_spec JSON.
the mapping looks like:
Expand Down
6 changes: 3 additions & 3 deletions geonode/documents/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from drf_yasg.utils import swagger_auto_schema
from drf_spectacular.utils import extend_schema

from dynamic_rest.viewsets import DynamicModelViewSet
from dynamic_rest.filters import DynamicFilterBackend, DynamicSortingFilter
Expand Down Expand Up @@ -55,8 +55,8 @@ class DocumentViewSet(DynamicModelViewSet):
serializer_class = DocumentSerializer
pagination_class = GeoNodeApiPagination

@swagger_auto_schema(methods=['get'], responses={200: ResourceBaseSerializer(many=True)},
operation_description="API endpoint allowing to retrieve the DocumentResourceLink(s).")
@extend_schema(methods=['get'], responses={200: ResourceBaseSerializer(many=True)},
description="API endpoint allowing to retrieve the DocumentResourceLink(s).")
@action(detail=True, methods=['get'])
def linked_resources(self, request, pk=None):
document = self.get_object()
Expand Down
10 changes: 5 additions & 5 deletions geonode/maps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from drf_yasg.utils import swagger_auto_schema
from drf_spectacular.utils import extend_schema

from dynamic_rest.viewsets import DynamicModelViewSet
from dynamic_rest.filters import DynamicFilterBackend, DynamicSortingFilter
Expand Down Expand Up @@ -53,16 +53,16 @@ class MapViewSet(DynamicModelViewSet):
serializer_class = MapSerializer
pagination_class = GeoNodeApiPagination

@swagger_auto_schema(methods=['get'], responses={200: MapLayerSerializer(many=True)},
operation_description="API endpoint allowing to retrieve the MapLayers list.")
@extend_schema(methods=['get'], responses={200: MapLayerSerializer(many=True)},
description="API endpoint allowing to retrieve the MapLayers list.")
@action(detail=True, methods=['get'])
def layers(self, request, pk=None):
map = self.get_object()
resources = map.layers
return Response(MapLayerSerializer(embed=True, many=True).to_representation(resources))

@swagger_auto_schema(methods=['get'], responses={200: LayerSerializer(many=True)},
operation_description="API endpoint allowing to retrieve the local MapLayers.")
@extend_schema(methods=['get'], responses={200: LayerSerializer(many=True)},
description="API endpoint allowing to retrieve the local MapLayers.")
@action(detail=True, methods=['get'])
def local_layers(self, request, pk=None):
map = self.get_object()
Expand Down
3 changes: 2 additions & 1 deletion geonode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@
'rest_framework',
'rest_framework_gis',
'dynamic_rest',
'drf_yasg',
'drf_spectacular',

# Theme
'django_forms_bootstrap',
Expand Down Expand Up @@ -537,6 +537,7 @@
'rest_framework.renderers.JSONRenderer',
'dynamic_rest.renderers.DynamicBrowsableAPIRenderer',
],
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}

REST_API_DEFAULT_PAGE = os.getenv('REST_API_DEFAULT_PAGE', 1)
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ tqdm==4.53.0
Deprecated==1.2.10
wrapt==1.12.1
jsonfield==3.1.0
jsonschema==3.2.0
pyrsistent==0.17.3

# Django Apps
django-allauth==0.43.0
Expand Down Expand Up @@ -76,7 +78,7 @@ djangorestframework-gis==0.16
djangorestframework-guardian==0.3.0
drf-extensions==0.6.0
drf-writable-nested==0.6.2
drf-yasg==1.20.0
drf-spectacular==0.11.1
dynamic-rest==2.0.0
Markdown==3.3.3

Expand Down
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ install_requires =
Deprecated==1.2.10
wrapt==1.12.1
jsonfield==3.1.0
jsonschema==3.2.0
pyrsistent==0.17.3

# Django Apps
django-allauth==0.43.0
Expand Down Expand Up @@ -102,7 +104,7 @@ install_requires =
djangorestframework-guardian==0.3.0
drf-extensions==0.6.0
drf-writable-nested==0.6.2
drf-yasg==1.20.0
drf-spectacular==0.11.1
dynamic-rest==2.0.0
Markdown==3.3.3

Expand Down

0 comments on commit 399b420

Please sign in to comment.