Skip to content

Commit

Permalink
contrib: Add rest-auth support
Browse files Browse the repository at this point in the history
Closes #113
  • Loading branch information
jayvdb committed Jul 14, 2020
1 parent 7600cbe commit d1c6141
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Features
- Schema serving with ``SpectacularAPIView`` (Redoc and Swagger-UI views are also available)
- Optional input/output serializer component split
- Included support for:
- `django-rest-auth <https://github.com/Tivix/django-rest-auth>`_
- `django-polymorphic <https://github.com/django-polymorphic/django-polymorphic>`_ / `django-rest-polymorphic <https://github.com/apirobot/django-rest-polymorphic>`_
- `SimpleJWT <https://github.com/SimpleJWT/django-rest-framework-simplejwt>`_
- `DjangoOAuthToolkit <https://github.com/jazzband/django-oauth-toolkit>`_
Expand Down
1 change: 1 addition & 0 deletions drf_spectacular/contrib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__all__ = [
'django_oauth_toolkit',
'djangorestframework_camel_case',
'rest_auth',
'rest_polymorphic',
'rest_framework_jwt',
'rest_framework_simplejwt'
Expand Down
121 changes: 121 additions & 0 deletions drf_spectacular/contrib/rest_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from django.conf import settings as global_settings
from rest_framework import serializers

try:
from allauth.account.app_settings import EMAIL_VERIFICATION, EmailVerificationMethod
except ImportError:
EMAIL_VERIFICATION, EmailVerificationMethod = None, None

try:
from rest_auth.app_settings import JWTSerializer, UserDetailsSerializer, TokenSerializer
from rest_auth.registration.app_settings import RegisterSerializer
except ImportError:
JWTSerializer, UserDetailsSerializer, TokenSerializer, RegisterSerializer = (None * 4)

from drf_spectacular.extensions import OpenApiViewExtension
from drf_spectacular.utils import extend_schema

if getattr(global_settings, 'REST_USE_JWT', False):
AuthTokenSerializer = JWTSerializer
else:
AuthTokenSerializer = TokenSerializer


class DRFDefaultDetailResponseSerializer(serializers.Serializer):
detail = serializers.CharField(read_only=True, required=False)


if EmailVerificationMethod and EMAIL_VERIFICATION == EmailVerificationMethod.MANDATORY:
class RegisterResponseSerializer(DRFDefaultDetailResponseSerializer):
pass

elif AuthTokenSerializer:
class RegisterResponseSerializer(serializers.Serializer):
user = UserDetailsSerializer(read_only=True)
token = AuthTokenSerializer(read_only=True)
else:
class RegisterResponseSerializer:
pass


class RestAuthDefaultResponseView(OpenApiViewExtension):

def view_replacement(self):

class Fixed(self.target_class):

@extend_schema(responses=DRFDefaultDetailResponseSerializer)
def post(self, request, *args, **kwargs):
pass

return Fixed


class RestAuthLoginView(OpenApiViewExtension):
target_class = 'rest_auth.views.LoginView'

def view_replacement(self):

class Fixed(self.target_class):

@extend_schema(responses=AuthTokenSerializer)
def post(self, request, *args, **kwargs):
pass

return Fixed


class RestAuthLogoutView(OpenApiViewExtension):
target_class = 'rest_auth.views.LogoutView'

def view_replacement(self):

class Fixed(self.target_class):

if getattr(global_settings, 'ACCOUNT_LOGOUT_ON_GET', None):
@extend_schema(request=None, responses=DRFDefaultDetailResponseSerializer)
def get(self, request, *args, **kwargs):
pass
else:
@extend_schema(exclude=True)
def get(self, request, *args, **kwargs):
pass

@extend_schema(request=None, responses=DRFDefaultDetailResponseSerializer)
def post(self, request, *args, **kwargs):
pass

return Fixed


class RestAuthPasswordChangeView(RestAuthDefaultResponseView):
target_class = 'rest_auth.views.PasswordChangeView'


class RestAuthPasswordResetView(RestAuthDefaultResponseView):
target_class = 'rest_auth.views.PasswordResetView'


class RestAuthPasswordResetConfirmView(RestAuthDefaultResponseView):
target_class = 'rest_auth.views.PasswordResetConfirmView'


class RestAuthRegisterView(OpenApiViewExtension):
target_class = 'rest_auth.registration.views.RegisterView'

def view_replacement(self):

class Fixed(self.target_class):

@extend_schema(
request=RegisterSerializer,
responses=RegisterResponseSerializer,
)
def post(self, request, *args, **kwargs):
pass

return Fixed


class RestAuthVerifyEmailView(RestAuthDefaultResponseView):
target_class = 'rest_auth.registration.views.VerifyEmailView'
2 changes: 2 additions & 0 deletions requirements/optionals.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
django-allauth
drf-jwt>=0.13.0
django-rest-auth
djangorestframework-simplejwt>=4.4.0
django-polymorphic>=2.1
django-rest-polymorphic>=0.1.8
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def pytest_configure(config):
)

contrib_apps = [
'rest_auth',
'rest_auth.registration',
'rest_framework_jwt',
'oauth2_provider',
# this is not strictly required and when added django-polymorphic
Expand Down Expand Up @@ -69,6 +71,8 @@ def pytest_configure(config):
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'allauth',
'allauth.account',
*[app for app in contrib_apps if module_available(app)],
'drf_spectacular',
'tests',
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ known_third_party =
yaml
jsonschema
inflection
allauth
rest_auth
rest_framework_simplejwt
rest_polymorphic
rest_framework_jwt
Expand Down

0 comments on commit d1c6141

Please sign in to comment.