Skip to content

Commit 3642eb5

Browse files
authored
Wagtail 8: Make API v3 opt-in (#289)
* Make API v3 opt-in via the new `ENABLE_API_V3` setting
1 parent 5cd6d7d commit 3642eb5

15 files changed

Lines changed: 117 additions & 100 deletions

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ WAGTAILMEDIA = {
7070
"ogv",
7171
"webm",
7272
], # list of extensions
73+
"ENABLE_API_V3": False, # Wagtail 8.0+ - enable the v3 API endpoints
7374
}
7475
```
7576

@@ -285,6 +286,8 @@ class BlogPage(Page):
285286

286287
### API
287288

289+
#### v2
290+
288291
To expose media items in the API, you can follow the [Wagtail documentation guide](https://docs.wagtail.org/en/stable/advanced_topics/api/v2/configuration.html#api-v2-configuration)
289292
for API configuration with wagtailmedia specifics:
290293

@@ -300,9 +303,16 @@ api_router = WagtailAPIRouter("wagtailapi")
300303
api_router.register_endpoint("media", MediaAPIViewSet)
301304
```
302305

306+
#### v3 (preview)
307+
308+
Starting with version 8.0, Wagtail provides an [experimental API v3](https://docs.wagtail.org/en/stable/advanced_topics/api/v3/index.html), powered by [django-ninja](https://github.com/vitalik/django-ninja).
309+
To opt-in, set `ENABLE_API_V3` to `True` in the `WAGTAILMEDIA` setting. The wagtailmedia endpoint will be available under the `<v3 API root>/media/`.
310+
311+
For further details, explore the [Wagtail API v3 documentation](https://docs.wagtail.org/en/stable/advanced_topics/api/v3/index.html).
312+
303313
## Translations
304314

305-
wagtailmedia has translations in French and Chinese. More translations welcome!
315+
wagtailmedia has translations in Chinese, French, German, Romanian, Ukrainian. More translations welcome!
306316

307317
## Contributing
308318

src/wagtailmedia/apps.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ def ready(self):
3232
)
3333

3434
if WAGTAIL_VERSION >= (8, 0):
35-
# v3 API. Note: the import order matters
36-
from .api.v3.registry import register_content_types
35+
from .settings import wagtailmedia_settings
3736

38-
register_content_types()
37+
if wagtailmedia_settings.ENABLE_API_V3:
38+
# v3 API. Note: the import order matters
39+
from .api.v3.registry import register_content_types
3940

40-
from wagtail.api.v3.api import api
41+
register_content_types()
4142

42-
from .api.v3.router import router
43+
from wagtail.api.v3.api import api
4344

44-
api.add_router("/media/", router)
45+
from .api.v3.router import router
46+
47+
api.add_router("/media/", router)

src/wagtailmedia/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"ogv",
3232
"webm",
3333
],
34+
"ENABLE_API_V3": False,
3435
}
3536

3637
# List of settings that have been deprecated
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from wagtail import VERSION as WAGTAIL_VERSION
2+
3+
4+
if WAGTAIL_VERSION >= (8, 0):
5+
from unittest import skipIf
6+
7+
from django.conf import settings
8+
from django.urls import NoReverseMatch, reverse
9+
from wagtail.api.v3.registry import registry
10+
11+
from wagtailmedia import get_media_model
12+
13+
from .base import TestV3MediaBase
14+
15+
Media = get_media_model()
16+
17+
@skipIf(settings.ENABLE_API_V3, "Skipped as testing with ENABLE_API_V3=True")
18+
class TestAPIv3IsOptIn(TestV3MediaBase):
19+
def test_schema_not_registered(self):
20+
registration = registry.get(Media._meta.label)
21+
self.assertIsNone(registration)
22+
23+
def test_schema_discovery_endpoint_doesnt_list_media(self):
24+
self.login()
25+
response = self.client.get(reverse("wagtailapi_v3:list_schemas"))
26+
names = [entry["name"] for entry in response.json()["types"]]
27+
self.assertNotIn(Media._meta.label, names)
28+
29+
def test_route_not_registered(self):
30+
with self.assertRaises(NoReverseMatch):
31+
reverse("wagtailapi_v3:list_media")

tests/test_api_v3/test_create.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33

44
if WAGTAIL_VERSION >= (8, 0):
5-
from unittest import mock
5+
from unittest import mock, skipUnless
66

7+
from django.conf import settings
78
from django.core.files.uploadedfile import SimpleUploadedFile
89
from django.db.models.signals import post_save
910
from django.test import override_settings
@@ -16,6 +17,7 @@
1617
Media = get_media_model()
1718
FILE_CONTENTS = b"Test media contents"
1819

20+
@skipUnless(settings.ENABLE_API_V3, "Skipped as testing with ENABLE_API_V3=False")
1921
class TestV3MediaCreate(TestV3MediaBase):
2022
def post_media(self, **kwargs):
2123
data = {

tests/test_api_v3/test_custom_media_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33

44
if WAGTAIL_VERSION >= (8, 0):
5+
from unittest import skipUnless
56
from unittest.mock import patch
67

8+
from django.conf import settings
79
from django.contrib.auth.models import Group, Permission
810
from django.core.files.uploadedfile import SimpleUploadedFile
911
from django.test import TestCase, override_settings
@@ -19,6 +21,7 @@
1921
from wagtailmedia.api.v3.form_data import build_media_form
2022
from wagtailmedia.forms import get_media_form
2123

24+
@skipUnless(settings.ENABLE_API_V3, "Skipped as testing with ENABLE_API_V3=False")
2225
@override_settings(WAGTAILMEDIA={"MEDIA_MODEL": "wagtailmedia_tests.CustomMedia"})
2326
class TestV3CustomMediaModel(WagtailTestUtils, TestCase):
2427
@classmethod

tests/test_api_v3/test_delete.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33

44
if WAGTAIL_VERSION >= (8, 0):
5-
from unittest import mock
5+
from unittest import mock, skipUnless
66

7+
from django.conf import settings
78
from django.db.models.signals import post_delete
89
from django.urls import reverse
910

@@ -13,6 +14,7 @@
1314

1415
Media = get_media_model()
1516

17+
@skipUnless(settings.ENABLE_API_V3, "Skipped as testing with ENABLE_API_V3=False")
1618
class TestV3MediaDelete(TestV3MediaBase):
1719
def delete(self, media_id):
1820
return self.client.delete(

tests/test_api_v3/test_detail.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33

44
if WAGTAIL_VERSION >= (8, 0):
5+
from unittest import skipUnless
6+
7+
from django.conf import settings
58
from django.urls import reverse
69
from wagtail.models import CollectionViewRestriction
710

811
from .base import TestV3MediaBase
912

13+
@skipUnless(settings.ENABLE_API_V3, "Skipped as testing with ENABLE_API_V3=False")
1014
class TestV3MediaDetail(TestV3MediaBase):
1115
def get_response(self, media_id):
1216
return self.client.get(

tests/test_api_v3/test_listing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33

44
if WAGTAIL_VERSION >= (8, 0):
5+
from unittest import skipUnless
6+
7+
from django.conf import settings
58
from django.contrib.auth.models import Group
69
from django.db import connection
710
from django.test.utils import CaptureQueriesContext
@@ -10,6 +13,7 @@
1013

1114
from .base import TestV3MediaBase
1215

16+
@skipUnless(settings.ENABLE_API_V3, "Skipped as testing with ENABLE_API_V3=False")
1317
class TestV3MediaListing(TestV3MediaBase):
1418
def get_response(self, **params):
1519
return self.client.get(reverse("wagtailapi_v3:list_media"), params)

tests/test_api_v3/test_schemas.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33

44
if WAGTAIL_VERSION >= (8, 0):
5+
from unittest import skipUnless
6+
7+
from django.conf import settings
58
from django.urls import reverse
69
from wagtail.api.v3.registry import registry
710

@@ -12,6 +15,7 @@
1215

1316
Media = get_media_model()
1417

18+
@skipUnless(settings.ENABLE_API_V3, "Skipped as testing with ENABLE_API_V3=False")
1519
class TestV3MediaSchemas(TestV3MediaBase):
1620
def test_content_type_registered_for_schema_discovery(self):
1721
registration = registry.get(Media._meta.label)

0 commit comments

Comments
 (0)