adds drf-yasg and automated generation of Swagger Schemas (#165)

* adds drf-yasg and automated generation of Swagger Schemas

* swagger url

* swagger docs

* adds swagger url on Readme

* swagger API

* Code of Conduct file

* doc
This commit is contained in:
Markos Gogoulos
2021-05-29 16:34:36 +03:00
committed by GitHub
parent 110695ae2f
commit 5602422d29
10 changed files with 347 additions and 19 deletions

View File

@ -3,6 +3,8 @@ from django.contrib.auth.decorators import login_required
from django.core.mail import EmailMessage
from django.http import HttpResponseRedirect
from django.shortcuts import render
from drf_yasg import openapi as openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import permissions, status
from rest_framework.decorators import api_view
from rest_framework.exceptions import PermissionDenied
@ -131,6 +133,13 @@ def edit_channel(request, friendly_token):
return render(request, "cms/channel_edit.html", {"form": form})
@swagger_auto_schema(
methods=['post'],
manual_parameters=[],
tags=['Users'],
operation_summary='Contact user',
operation_description='Contact user through email, if user has set this option',
)
@api_view(["POST"])
def contact_user(request, username):
if not request.user.is_authenticated:
@ -167,9 +176,18 @@ Sender email: %s\n
class UserList(APIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
parser_classes = (JSONParser, MultiPartParser, FormParser, FileUploadParser)
@swagger_auto_schema(
manual_parameters=[
openapi.Parameter(name='page', type=openapi.TYPE_INTEGER, in_=openapi.IN_QUERY, description='Page number'),
],
tags=['Users'],
operation_summary='List users',
operation_description='Paginated listing of users',
)
def get(self, request, format=None):
pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
paginator = pagination_class()
@ -202,6 +220,14 @@ class UserDetail(APIView):
except User.DoesNotExist:
return Response({"detail": "user does not exist"}, status=status.HTTP_400_BAD_REQUEST)
@swagger_auto_schema(
manual_parameters=[
openapi.Parameter(name='username', type=openapi.TYPE_STRING, in_=openapi.IN_PATH, description='username', required=True),
],
tags=['Users'],
operation_summary='List user details',
operation_description='Get user details',
)
def get(self, request, username, format=None):
# Get user details
user = self.get_user(username)
@ -211,9 +237,24 @@ class UserDetail(APIView):
serializer = UserDetailSerializer(user, context={"request": request})
return Response(serializer.data)
def post(self, request, uid, format=None):
@swagger_auto_schema(
manual_parameters=[
openapi.Parameter(name='username', type=openapi.TYPE_STRING, in_=openapi.IN_PATH, description='username', required=True),
],
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
'description': openapi.Schema(type=openapi.TYPE_STRING, description='description'),
'name': openapi.Schema(type=openapi.TYPE_STRING, description='name'),
},
),
tags=['Users'],
operation_summary='Edit user details',
operation_description='Post user details - authenticated view',
)
def post(self, request, username, format=None):
# USER
user = self.get_user(uid)
user = self.get_user(username)
if isinstance(user, Response):
return user
@ -228,6 +269,12 @@ class UserDetail(APIView):
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@swagger_auto_schema(
manual_parameters=[],
tags=['Users'],
operation_summary='Xto_be_written',
operation_description='to_be_written',
)
def put(self, request, uid, format=None):
# ADMIN
user = self.get_user(uid)
@ -248,6 +295,12 @@ class UserDetail(APIView):
serializer = UserDetailSerializer(user, context={"request": request})
return Response(serializer.data)
@swagger_auto_schema(
manual_parameters=[],
tags=['Users'],
operation_summary='to_be_written',
operation_description='to_be_written',
)
def delete(self, request, username, format=None):
# Delete a user
user = self.get_user(username)