Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update views.py- Added missing GET request parameters to swagger #734

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 119 additions & 19 deletions core/orgs/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db.models import Count
from django.http import Http404
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from pydash import get
from rest_framework import mixins, status, generics
Expand Down Expand Up @@ -30,10 +31,8 @@

TRUTHY = get_truthy_values()


class OrganizationListView(BaseAPIView,
ListWithHeadersMixin,
mixins.CreateModelMixin):
class OrganizationListView(BaseAPIView, ListWithHeadersMixin, mixins.CreateModelMixin):

model = Organization
queryset = Organization.objects.filter(is_active=True)
es_fields = Organization.es_fields
Expand Down Expand Up @@ -76,7 +75,15 @@ def get_serializer_class(self):

return OrganizationListSerializer

@swagger_auto_schema(manual_parameters=[org_no_members_param])
@swagger_auto_schema(
manual_parameters=[
openapi.Parameter('user', openapi.IN_PATH, description="Filter by username", type=openapi.TYPE_STRING),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user param in path cannot be added here. That comes from routes (urls.py), if you check users/urls.py its already added there. This will have no effect in the results and also its not mandatory.

openapi.Parameter(NO_MEMBERS, openapi.IN_QUERY, description="Filter organizations with no members", type=openapi.TYPE_BOOLEAN),
openapi.Parameter('updated_since', openapi.IN_QUERY, description="Filter by update date", type=openapi.TYPE_STRING, format=openapi.FORMAT_DATETIME),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated_since -- updatedSince --> UPDATED_SINCE_PARAM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly provide more context

openapi.Parameter(UPDATED_BY_USERNAME_PARAM, openapi.IN_QUERY, description="Filter by username of user who updated", type=openapi.TYPE_STRING),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

description=Filter Orgs by the update by user

],
snyaggarwal marked this conversation as resolved.
Show resolved Hide resolved
responses={status.HTTP_200_OK: OrganizationListSerializer(many=True)}
)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)

Expand Down Expand Up @@ -106,7 +113,6 @@ class OrganizationBaseView(BaseAPIView, RetrieveAPIView, DestroyAPIView):
model = Organization
queryset = Organization.objects.filter(is_active=True)


class OrganizationLogoView(OrganizationBaseView, BaseLogoView):
serializer_class = OrganizationDetailSerializer

Expand All @@ -115,7 +121,15 @@ def get_permissions(self):
return [HasPrivateAccess(), ]

return [CanViewConceptDictionary(), ]


@swagger_auto_schema(
manual_parameters=[
openapi.Parameter('org', openapi.IN_PATH, description="Organization mnemonic", type=openapi.TYPE_STRING, required=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

org is already part of this API /orgs/{org}/logo/ path. This will have no effect.

],
responses={status.HTTP_200_OK: "Logo image"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also "logo image: is hardcoded, not needed as GET returns OrganizationDetailSerializer which swagger will pick up automatically.

)
def get(self, request, *args, **kwargs):
return super().get(request, *args, **kwargs)

class OrganizationOverviewView(OrganizationBaseView, RetrieveAPIView, UpdateAPIView):
serializer_class = OrganizationOverviewSerializer
Expand All @@ -128,7 +142,15 @@ def get_permissions(self):

def get_queryset(self):
return super().get_queryset().filter(mnemonic=self.kwargs['org'])


@swagger_auto_schema(
manual_parameters=[
openapi.Parameter('org', openapi.IN_PATH, description="Organization mnemonic", type=openapi.TYPE_STRING, required=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev -- org is already part of this API /orgs/{org}/overview/ path. This will have no effect.

],
responses={status.HTTP_200_OK: OrganizationOverviewSerializer()}
WestOnyinsi marked this conversation as resolved.
Show resolved Hide resolved
)
def get(self, request, *args, **kwargs):
return super().get(request, *args, **kwargs)

class OrganizationDetailView(OrganizationBaseView, mixins.UpdateModelMixin, mixins.CreateModelMixin, TaskMixin):
def get_permissions(self):
Expand Down Expand Up @@ -170,14 +192,30 @@ def destroy(self, request, *args, **kwargs):
return result

return Response(status=status.HTTP_204_NO_CONTENT)


@swagger_auto_schema(
manual_parameters=[
openapi.Parameter('org', openapi.IN_PATH, description="Organization mnemonic", type=openapi.TYPE_STRING, required=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev

],
responses={status.HTTP_200_OK: OrganizationDetailSerializer()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev

)
def get(self, request, *args, **kwargs):
return super().get(request, *args, **kwargs)

class OrganizationClientConfigsView(ResourceClientConfigsView):
lookup_field = 'org'
model = Organization
queryset = Organization.objects.filter(is_active=True)
permission_classes = (CanViewConceptDictionary, )

@swagger_auto_schema(
manual_parameters=[
openapi.Parameter('org', openapi.IN_PATH, description="Organization mnemonic", type=openapi.TYPE_STRING, required=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev

],
responses={status.HTTP_200_OK: "Client configs"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev

)
def get(self, request, *args, **kwargs):
return super().get(request, *args, **kwargs)

class OrganizationMemberView(generics.GenericAPIView):
userprofile = None
Expand Down Expand Up @@ -235,8 +273,22 @@ def delete(self, request, **kwargs): # pylint: disable=unused-argument
self.userprofile.save()

return Response(status=status.HTTP_204_NO_CONTENT)




@swagger_auto_schema(
manual_parameters=[
openapi.Parameter('org', openapi.IN_PATH, description="Organization mnemonic", type=openapi.TYPE_STRING, required=True),
openapi.Parameter('user', openapi.IN_PATH, description="Username", type=openapi.TYPE_STRING, required=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

org and user doesnt need to be defined here, they are already part of path in through urls.py. This will have no effect.

],
responses={
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need for this.

status.HTTP_204_NO_CONTENT: "User is a member",
status.HTTP_404_NOT_FOUND: "User is not a member",
status.HTTP_403_FORBIDDEN: "Permission denied"
}
)
def get(self, request, *args, **kwargs):
WestOnyinsi marked this conversation as resolved.
Show resolved Hide resolved
return super().get(request, *args, **kwargs)

class OrganizationResourceAbstractListView:
def get_queryset(self):
username = self.kwargs.get('user', None)
Expand All @@ -249,34 +301,53 @@ def get_queryset(self):

return self.queryset.filter(organization__in=user.organizations.all(), version=HEAD)


class OrganizationSourceListView(OrganizationResourceAbstractListView, SourceListView):
pass



class OrganizationCollectionListView(OrganizationResourceAbstractListView, CollectionListView):
pass



class OrganizationExtrasBaseView(APIView):
def get_object(self):
instance = Organization.objects.filter(is_active=True, mnemonic=self.kwargs['org']).first()

if not instance:
raise Http404()
return instance



class OrganizationExtrasView(OrganizationExtrasBaseView):
serializer_class = OrganizationDetailSerializer

@swagger_auto_schema(
manual_parameters=[
openapi.Parameter('org', openapi.IN_PATH, description="Organization mnemonic", type=openapi.TYPE_STRING, required=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev

],
responses={
status.HTTP_200_OK: openapi.Response(
description="Organization extras",
schema=openapi.Schema(type=openapi.TYPE_OBJECT, additional_properties=openapi.Schema(type=openapi.TYPE_STRING))
)
}
)
def get(self, request, org): # pylint: disable=unused-argument
return Response(get(self.get_object(), 'extras', {}))


class OrganizationExtraRetrieveUpdateDestroyView(OrganizationExtrasBaseView, RetrieveUpdateDestroyAPIView):
serializer_class = OrganizationDetailSerializer

@swagger_auto_schema(
manual_parameters=[
openapi.Parameter('org', openapi.IN_PATH, description="Organization mnemonic", type=openapi.TYPE_STRING, required=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev

openapi.Parameter('extra', openapi.IN_PATH, description="Extra field key", type=openapi.TYPE_STRING, required=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev

],
responses={
status.HTTP_200_OK: openapi.Response(
description="Extra field value",
schema=openapi.Schema(type=openapi.TYPE_OBJECT, properties={'extra_key': openapi.Schema(type=openapi.TYPE_STRING)})
),
status.HTTP_404_NOT_FOUND: openapi.Response(description="Extra field not found")
}
)
def retrieve(self, request, *args, **kwargs):
key = kwargs.get('extra')
instance = self.get_object()
Expand All @@ -286,6 +357,25 @@ def retrieve(self, request, *args, **kwargs):

return Response({'detail': NOT_FOUND}, status=status.HTTP_404_NOT_FOUND)

@swagger_auto_schema(
manual_parameters=[
openapi.Parameter('org', openapi.IN_PATH, description="Organization mnemonic", type=openapi.TYPE_STRING, required=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev

openapi.Parameter('extra', openapi.IN_PATH, description="Extra field key", type=openapi.TYPE_STRING, required=True),
],
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'extra_key': openapi.Schema(type=openapi.TYPE_STRING, description="Value for the extra field")
}
),
responses={
status.HTTP_200_OK: openapi.Response(
description="Updated extra field",
schema=openapi.Schema(type=openapi.TYPE_OBJECT, properties={'extra_key': openapi.Schema(type=openapi.TYPE_STRING)})
),
status.HTTP_400_BAD_REQUEST: openapi.Response(description="Invalid input")
}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels like a typo, not needed

def update(self, request, **kwargs): # pylint: disable=arguments-differ
key = kwargs.get('extra')
value = request.data.get(key)
Expand All @@ -299,6 +389,16 @@ def update(self, request, **kwargs): # pylint: disable=arguments-differ
instance.set_checksums()
return Response({key: value})

@swagger_auto_schema(
manual_parameters=[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prev

openapi.Parameter('org', openapi.IN_PATH, description="Organization mnemonic", type=openapi.TYPE_STRING, required=True),
openapi.Parameter('extra', openapi.IN_PATH, description="Extra field key", type=openapi.TYPE_STRING, required=True),
],
responses={
status.HTTP_204_NO_CONTENT: openapi.Response(description="Extra field deleted"),
status.HTTP_404_NOT_FOUND: openapi.Response(description="Extra field not found")
}
)
def delete(self, request, *args, **kwargs):
key = kwargs.get('extra')
instance = self.get_object()
Expand Down