Is there a way for generating all third-party apps endpoints with drf-yasg?
So I have installed social_django app and want to include all URLs it provides to swagger UI
My urlpatterns
urlpatterns = [
re_path('admin/', admin.site.urls),
re_path('', include('user.urls')),
re_path(r'^$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
re_path(r'', include('social_django.urls', namespace='social')),
re_path(r'accounts/', include('django.contrib.auth.urls')),
]
Schema
schema_view = get_schema_view(
openapi.Info(
title="Snippets API",
default_version='v1',
description="Test description",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="contact#snippets.local"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
Those are that defined by me, but I also want to see here /login/facebook/ , /login/google-oauth2/ etc. Or for example simple /admin/ that Django provides.
How can I do this?
Related
Im implementing drf social oauth2 and while accessing the URL - localhost:8000/auth/login/facebook/ I get 'drf' is not a registered namespace, No ReverseMatch error and when I change my namespace to social, I get 'social' is not a registered namespace.
#URLPatterns
urlpatterns = [
path("admin/", admin.site.urls),
path('auth/', include('drf_social_oauth2.urls', namespace='social')),
path('api/', include("users.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
#Installed Apps
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# rest
"rest_framework",
"corsheaders",
"rest_framework_simplejwt",
# Oauth
"oauth2_provider",
"social_django",
"drf_social_oauth2",
# apps
"accounts",
"inventory",
"cart",
"orders",
"users",
]
I have a simple Django app managing user autentication and registration with dj-rest-auth and JWT.
Accorging to the docs, I have set the the app to use JWT with the plugin simple-jwt with settings.py:
JWT_AUTH_COOKIE = 'my-app-auth'
JWT_AUTH_REFRESH_COOKIE = 'my-refresh-token'
Expected behaviour: Cookies should be set on /dj-rest-auth/login/ and /token/refresh
Issue: Cookies are set correctly ONLY on /dj-rest-auth/login/, but not on /token/refresh.
This issue is not something new, since it is supposed to be solved here and here.
So, I also added the suggested middleware.
Here my settings.py:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'dj_rest_auth.jwt_auth.JWTCookieAuthentication',
]
}
REST_USE_JWT = True
JWT_AUTH_COOKIE = 'my-app-auth'
JWT_AUTH_REFRESH_COOKIE = 'my-refresh-token'
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'auth_app.middleware.MoveJWTRefreshCookieIntoTheBody'
]
and my urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url, re_path
from auth_app import views
# JWT CONF
from rest_framework_simplejwt.views import (
TokenRefreshView,
TokenVerifyView,
)
from dj_rest_auth.registration.views import VerifyEmailView, RegisterView
from dj_rest_auth.views import PasswordResetConfirmView
from allauth.account.views import confirm_email
urlpatterns = [
path('admin/', admin.site.urls),
path('token/verify/', TokenVerifyView.as_view(), name='token_verify'),
path('token/refresh/', TokenRefreshView().as_view(), name='token_refresh'),
path('api/protected/', views.ProtectedView.as_view(), name='protected_view'),
path('api/open/', views.OpenView.as_view(), name='open_view'),
# dj-rest-auth common
path('dj-rest-auth/', include('dj_rest_auth.urls')),
# dj-rest-auth registration
path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
path('dj-rest-auth/account-confirm-email/', VerifyEmailView.as_view(), name='account_email_verification_sent'),
re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(),
name='account_confirm_email'),
path(
'rest-auth/password/reset/confirm/<slug:uidb64>/<slug:token>/',
PasswordResetConfirmView.as_view(), name='password_reset_confirm'
),
]
I have been checking my with the solutions provided for long long time, but still no cookie set on refresh endpoint.
Am I missing something?
I SOLVED this issue!
If anybody in future will struggle with this, here to solution: in urls.py you have to substitute the simplejwt TokenRefreshView with get_refresh_view() which belong to dj-rest-auth.
In other words, the right JWT configuration is:
# JWT CONF
from rest_framework_simplejwt.views import TokenVerifyView
from dj_rest_auth.jwt_auth import get_refresh_view
urlpatterns = [
path('users/', include('users.urls')),
path('users/', include('djoser.urls')),
path('users/', include('djoser.urls.jwt')),
]
it gives me error when I try to include djoser.urls and users.urls in one path. Are there any options to do this?
I am currently using Django REST framework and it works very well for me. The problem is that the API documentation page is public in production and I would like it to only be seen in development (localhost) or by administrators users.
This is my urls.py:
schema_view = get_schema_view(title='My API')
router = DefaultRouter()
# router.register(...)
urlpatterns = [
url(r'', include(router.urls)),
url(r'^docs/', include_docs_urls(title='My API service')),
url(r'^schema/$', schema_view),
]
PS: I'm using the version 3.9.2
You can always check for the DEBUG value and do stuff if it is set (which is set to True in development mode). For example:
from django.conf import settings
urlpatterns = [
url(r'', include(router.urls)),
url(r'^schema/$', schema_view),
]
if settings.DEBUG:
urlpatterns += [
url(r'^docs/', include_docs_urls(title='My API service'))
]
I'm trying to set up Django REST Framework with Django 2.0 project which means url(r'^something/' ... has been replaced with path(something/ ....
I'm trying to work out how to set up my rest_framework patterns.
This is what I have:
router = routers.DefaultRouter()
router.register(r'regulations', api.RegulationViewSet)
router.register(r'languages', api.LanguageViewSet)
urlpatterns = [
...
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
...
]
If I go to http://127.0.0.1:8000/regulations I simply get:
Page not found (404)
How should I set up my urlpatterns?
urlpatterns = [
...
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
...
]
with path('', include(router.urls)), you can get:
http://127.0.0.1:8000/regulations/
http://127.0.0.1:8000/languages/
with
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
you can get:
http://127.0.0.1:8000/api-auth/{other paths}
After registering the router you have to include it in the urlpatterns. The way how #Ykh suggested is technically correct, but with regards to content is missing the point.
urlpatterns = [
# here you include your router
path('', include(router.urls)),
# here you include the authentication paths
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
Now you'll have the following routes:
http://localhost:8000/regulations/
http://localhost:8000/languages/
plus:
http://localhost:8000/api-auth/{other paths}