-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace drf-yasg with drf_spectacular
- Loading branch information
Showing
16 changed files
with
361 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/olympia/amo/management/commands/generate_js_swagger_files.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from pathlib import Path | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand, CommandError | ||
from django.http import HttpRequest | ||
from django.utils.encoding import force_str | ||
|
||
from drf_spectacular.views import SpectacularSwaggerSplitView | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Generate static swagger files' | ||
requires_system_checks = [] # Can be ran without the database up yet. | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--verbose', | ||
action='store_true', | ||
help='Print verbose output', | ||
) | ||
|
||
def handle(self, *args, **options): | ||
fake_request = HttpRequest() | ||
# Add a script parameter to the request to get the swagger UI JS | ||
fake_request.GET['script'] = '1' | ||
fake_request.method = 'GET' | ||
fake_request.META = { | ||
'SERVER_NAME': '.mozilla.org', | ||
'SERVER_PORT': '80', | ||
} | ||
|
||
self.stdout.write('Generating swagger UI JS...') | ||
|
||
|
||
root = Path(settings.STATIC_BUILD_PATH) / 'js' / 'swagger' | ||
|
||
if not root.exists(): | ||
if options['verbose']: | ||
self.stdout.write(f'Creating directory: {root}') | ||
root.mkdir(parents=True) | ||
|
||
# TODO: this is failing in production build | ||
response = SpectacularSwaggerSplitView.as_view(url_name='v5:schema')(fake_request) | ||
if response.status_code != 200: | ||
self.stderr.write(response.content) | ||
raise CommandError(f'Unexpected status code: {response.status_code}') | ||
|
||
response.render() | ||
filename = root / 'swagger_ui.js' | ||
content = force_str(response.content) | ||
with open(filename, 'w') as f: | ||
f.write(content) | ||
|
||
self.stdout.write('Swagger UI JS generated successfully.') | ||
if options['verbose']: | ||
self.stdout.write(f'Swagger UI JS file: {filename}') | ||
self.stdout.write(content) |
Oops, something went wrong.