make generate_swagger work for projects without authentication (#161)

* make generate_swagger work for projects without authentication
* use get_user_model instead of importing User
openapi3
Étienne Noss 2018-07-06 15:13:19 +02:00 committed by Cristi Vîjdea
parent b37ce3227a
commit db86981dc1
2 changed files with 7 additions and 4 deletions

View File

@ -4,7 +4,7 @@ import os
from collections import OrderedDict from collections import OrderedDict
from importlib import import_module from importlib import import_module
from django.contrib.auth.models import User from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from rest_framework.test import APIRequestFactory, force_authenticate from rest_framework.test import APIRequestFactory, force_authenticate
@ -61,7 +61,6 @@ class Command(BaseCommand):
) )
parser.add_argument( parser.add_argument(
'--user', dest='user', '--user', dest='user',
default='',
help='Username of an existing user to use for mocked authentication. This option implies --mock-request.' help='Username of an existing user to use for mocked authentication. This option implies --mock-request.'
) )
parser.add_argument( parser.add_argument(
@ -122,7 +121,11 @@ class Command(BaseCommand):
api_url = api_url or swagger_settings.DEFAULT_API_URL api_url = api_url or swagger_settings.DEFAULT_API_URL
user = User.objects.get(username=user) if user else None if user:
# Only call get_user_model if --user was passed in order to
# avoid crashing if auth is not configured in the project
user = get_user_model().objects.get(username=user)
mock = mock or private or (user is not None) mock = mock or private or (user is not None)
if mock and not api_url: if mock and not api_url:
raise ImproperlyConfigured( raise ImproperlyConfigured(

View File

@ -17,7 +17,7 @@ from drf_yasg.generators import OpenAPISchemaGenerator
def call_generate_swagger(output_file='-', overwrite=False, format='', api_url='', def call_generate_swagger(output_file='-', overwrite=False, format='', api_url='',
mock=False, user='', private=False, generator_class_name='', **kwargs): mock=False, user=None, private=False, generator_class_name='', **kwargs):
out = StringIO() out = StringIO()
call_command( call_command(
'generate_swagger', stdout=out, 'generate_swagger', stdout=out,