Skip to content

Commit 18d8dcc

Browse files
author
PRITAM CHAKRABORTY
committed
Merge branch 'documentation'
2 parents c1b211e + fc4a680 commit 18d8dcc

File tree

7 files changed

+141
-8
lines changed

7 files changed

+141
-8
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ gunicorn = "*"
1616
dj-database-url = "*"
1717
psycopg2 = "*"
1818
termcolor = "*"
19+
drf-yasg = "*"
1920

2021
[dev-packages]
2122
autopep8 = "*"

Pipfile.lock

Lines changed: 75 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blogapp/swagger_schema.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from drf_yasg.inspectors import SwaggerAutoSchema
2+
3+
4+
class CustomAutoSchema(SwaggerAutoSchema):
5+
6+
def get_tags(self, operation_keys=None):
7+
tags = self.overrides.get('tags', None) or getattr(
8+
self.view, 'my_tags', [])
9+
if not tags:
10+
tags = [operation_keys[0]]
11+
12+
return tags

blogapp/urls.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# all_blogger_blogs.register('blogs', OwnerBlogListVSet, 'blogs')
1414

1515

16-
1716
comment_router = NestedDefaultRouter(all_blogger_blogs, 'blog', lookup='blog')
1817
comment_router.register('comments', CommentVSet, basename='comments')
1918

blogapp/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222

2323
class BloggerViewSet(ModelViewSet):
24+
""" Blogger can retrive own details as well as others detail, but can only perform update,delete for own
25+
list:
26+
to get all bloggers
27+
"""
2428
http_method_names = ["get", "patch", "delete", "head", "options"]
2529
filter_backends = [
2630
DjangoFilterBackend,
@@ -42,6 +46,7 @@ class BloggerViewSet(ModelViewSet):
4246
permission_classes = [IsAdminUser | (IsAuthenticated & IsSelf)]
4347
queryset = Blogger.objects.all()
4448
serializer_class = BloggerAdminSerializer
49+
my_tags = ["Blogger"]
4550

4651
def get_permissions(self):
4752
method = self.request.method
@@ -67,6 +72,7 @@ class OwnBlogViewSet(ModelViewSet):
6772
ordering_fields = ["title", "created_at"]
6873
serializer_class = BlogReadSerializer
6974
permission_classes = [IsAdminUser | (IsAuthenticated & IsBlogOwner)]
75+
my_tags = ["Blogger-Blog"]
7076
# queryset = Blog.objects.prefetch_related(
7177
# 'comments').select_related('creator').all()
7278

@@ -100,10 +106,12 @@ class AllBlogVSet(ListModelMixin, GenericViewSet):
100106
queryset = Blog.objects.select_related(
101107
'creator').prefetch_related('comments').all()
102108
serializer_class = BlogReadSerializer
109+
my_tags = ["All Blogs"]
103110

104111

105112
class CommentVSet(ModelViewSet):
106113
http_method_names = ["get", "post", "patch", "delete", "option", "head"]
114+
my_tags = ["Blog-Comments"]
107115

108116
def get_permissions(self):
109117
if self.request.method == 'GET':

core/settings/common.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
'django.contrib.messages',
3232
'django.contrib.staticfiles',
3333
# 3rd party apps
34+
# 'django.contrib.staticfiles', # required for serving swagger ui's css/js files
35+
'drf_yasg',
3436
# "debug_toolbar",
3537
'rest_framework',
3638
'djoser',
@@ -60,6 +62,16 @@
6062

6163
ROOT_URLCONF = 'core.urls'
6264

65+
66+
SWAGGER_SETTINGS = {
67+
"DEFAULT_AUTO_SCHEMA_CLASS": "blogapp.swagger_schema.CustomAutoSchema",
68+
"LOGIN_URL": 'admin/',
69+
"LOGOUT_URL":'admin/logout',
70+
"OPERATIONS_SORTER":'method',
71+
"TAGS_SORTER":'alpha',
72+
"DOC_EXPANSION":"none",
73+
}
74+
6375
TEMPLATES = [
6476
{
6577
'BACKEND': 'django.template.backends.django.DjangoTemplates',
@@ -130,7 +142,9 @@
130142
),
131143
'DEFAULT_PERMISSION_CLASSES': [
132144
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
133-
]
145+
],
146+
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
147+
'PAGE_SIZE': 5,
134148
}
135149

136150
SIMPLE_JWT = {

core/urls.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,49 @@
1313
1. Import the include() function: from django.urls import include, path
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
16-
from django.conf import settings ,urls
16+
from django.conf import settings, urls
1717
from django.contrib import admin
1818
from django.urls import path, include
1919
from django.conf.urls.static import static
2020

21+
# DRF_YASG
22+
from rest_framework import permissions
23+
from drf_yasg.views import get_schema_view
24+
from drf_yasg import openapi
25+
26+
27+
schema_view = get_schema_view(
28+
openapi.Info(
29+
title="Blogapp API",
30+
default_version='v1',
31+
description="""
32+
# This is the `Blogapp API` documentation
33+
34+
> ### Here all the api routes are grouped by tags
35+
36+
""",
37+
# terms_of_service="https://www.google.com/policies/terms/",
38+
# contact=openapi.Contact(email="pritam.chk98@gmail.com"),
39+
# license=openapi.License(name="BSD License"),
40+
),
41+
public=True,
42+
permission_classes=[permissions.AllowAny],
43+
)
44+
# DRF_YASG
45+
46+
2147
urlpatterns = [
2248
path('admin/', admin.site.urls),
2349
path('auth/', include('djoser.urls')),
2450
path('auth/', include('djoser.urls.jwt')),
2551
# path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
2652
# own app routes
2753
path('blogapp/', include("blogapp.urls")),
54+
#FIXME: drf_yasg url
55+
path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
2856
]
2957
# urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
3058

3159
if settings.DEBUG:
3260
import debug_toolbar
33-
urlpatterns+=path("__debug__/", include(debug_toolbar.urls)),
61+
urlpatterns += path("__debug__/", include(debug_toolbar.urls)),

0 commit comments

Comments
 (0)