Skip to content

Commit 59ca3ef

Browse files
committed
'add_swagger_and_find_error_in_rest_swagger'
1 parent 03dd97b commit 59ca3ef

File tree

185 files changed

+36046
-37
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+36046
-37
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
errors.txt

Pipfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ verify_ssl = true
44
name = "pypi"
55

66
[packages]
7-
django = "*"
87
djangorestframework = "*"
98
psycopg2-binary = "*"
109
django-rest-auth = "*"
1110
django-allauth = "*"
11+
coreapi = "*"
12+
pyyaml = "*"
13+
django-rest-swagger = "*"
14+
django = "==3.0.2"
1215

1316
[dev-packages]
1417
mypy = "*"

Pipfile.lock

+203-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
130 Bytes
Binary file not shown.
-241 Bytes
Binary file not shown.

blogapi/settings.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
# third-party
4343
'rest_framework',
4444
'rest_framework.authtoken',
45+
'rest_framework_swagger',
4546

4647
'rest_auth',
4748
'rest_auth.registration',
@@ -62,7 +63,8 @@
6263
'DEFAULT_AUTHENTICATION_CLASSES':[
6364
'rest_framework.authentication.SessionAuthentication',
6465
'rest_framework.authentication.TokenAuthentication',
65-
]
66+
],
67+
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
6668
}
6769

6870
# Alluth settings
@@ -152,3 +154,4 @@
152154
# https://docs.djangoproject.com/en/3.1/howto/static-files/
153155

154156
STATIC_URL = '/static/'
157+
STATIC_ROOT = os.path.join(BASE_DIR,'static')

blogapi/urls.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
"""blogapi URL Configuration
2-
3-
The `urlpatterns` list routes URLs to views. For more information please see:
4-
https://docs.djangoproject.com/en/3.1/topics/http/urls/
5-
Examples:
6-
Function views
7-
1. Add an import: from my_app import views
8-
2. Add a URL to urlpatterns: path('', views.home, name='home')
9-
Class-based views
10-
1. Add an import: from other_app.views import Home
11-
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12-
Including another URLconf
13-
1. Import the include() function: from django.urls import include, path
14-
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15-
"""
161
from django.contrib import admin
172
from django.urls import path, include
183

4+
from rest_framework.schemas import get_schema_view
5+
from rest_framework.documentation import include_docs_urls
6+
7+
from rest_framework_swagger.views import get_swagger_view
8+
9+
10+
API_TITLE = 'Blog API'
11+
API_DESCRIPTION = 'A Web API for creating and editing blog posts.'
12+
schema_view = get_swagger_view(title = API_TITLE)
13+
1914
urlpatterns = [
2015
path('admin/', admin.site.urls),
2116
path('api/v1/', include('posts.urls')),
2217
path('api-auth/', include('rest_framework.urls')),
2318
path('api/v1/rest-auth/', include('rest_auth.urls')),
2419
path('api/v1/rest-auth/registration/', include('rest_auth.registration.urls')),
20+
# path('schema/', schema_view),
21+
path('docs/', include_docs_urls(title = API_TITLE, description = API_DESCRIPTION)),
22+
path('swagger-docs/', schema_view),
2523
]
379 Bytes
Binary file not shown.

posts/__pycache__/urls.cpython-39.pyc

71 Bytes
Binary file not shown.
399 Bytes
Binary file not shown.

posts/serializers.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from django.contrib.auth import get_user_model
2+
13
from rest_framework import serializers
24

35
from .models import Post
@@ -11,4 +13,13 @@ class Meta:
1113
'body',
1214
'created_at',
1315
)
14-
model = Post
16+
model = Post
17+
18+
class UserSerializer(serializers.ModelSerializer):
19+
class Meta:
20+
model = get_user_model()
21+
fields = (
22+
'id',
23+
'email',
24+
'username',
25+
)

posts/urls.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
from django.urls import path
22

3-
from .views import PostList, PostDetail
4-
urlpatterns = [
5-
path('<int:pk>/', PostDetail.as_view(), name = 'post_detail'),
6-
path('', PostList.as_view(), name = 'post_list'),
7-
]
3+
from rest_framework.routers import SimpleRouter
4+
5+
from .views import (
6+
PostViewSet,
7+
UserViewSet,
8+
)
9+
10+
router = SimpleRouter()
11+
12+
router.register('', PostViewSet, basename = 'posts')
13+
router.register('users', UserViewSet, basename = 'users')
14+
15+
urlpatterns = router.urls

posts/views.py

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
1-
from rest_framework import generics, permissions
1+
from django.contrib.auth import get_user_model
2+
3+
from rest_framework import generics, viewsets
24

35
# Create your views here.
46

57
from .models import Post
68

7-
from .serializers import PostSerializer
9+
from .serializers import PostSerializer, UserSerializer
810

911
from .permissions import IsAuthorOrReadOnly
10-
class PostList(generics.ListCreateAPIView):
12+
13+
class PostViewSet(viewsets.ModelViewSet):
14+
permission_classes = (
15+
IsAuthorOrReadOnly,
16+
)
1117
queryset = Post.objects.all()
1218
serializer_class = PostSerializer
1319

14-
class PostDetail(generics.RetrieveUpdateDestroyAPIView):
15-
permission_classes = (IsAuthorOrReadOnly,)
16-
queryset = Post.objects.all()
17-
serializer_class = PostSerializer
20+
class UserViewSet(viewsets.ModelViewSet):
21+
queryset = get_user_model().objects.all()
22+
serializer_class = UserSerializer
23+
24+
25+
# class PostList(generics.ListCreateAPIView):
26+
# queryset = Post.objects.all()
27+
# serializer_class = PostSerializer
28+
29+
# class PostDetail(generics.RetrieveUpdateDestroyAPIView):
30+
# permission_classes = (IsAuthorOrReadOnly,)
31+
# queryset = Post.objects.all()
32+
# serializer_class = PostSerializer
33+
34+
35+
class UserList(generics.ListAPIView):
36+
queryset = get_user_model().objects.all()
37+
serializer_class = UserSerializer
38+
39+
class UserDetail(generics.RetrieveUpdateDestroyAPIView):
40+
queryset = get_user_model().objects.all()
41+
serializer_class = UserSerializer

0 commit comments

Comments
 (0)