Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit da0ef63

Browse files
author
Oliver Sauder
committed
Add json api settings module
This is inspired by api_settings of REST framework. Advantages: * Centralizing all json api settings and defaults in one module * Simplifying refactoring and deprecation of settings in the future * Properly use override_settings in tests instead of monkey patching
1 parent e501b04 commit da0ef63

File tree

8 files changed

+96
-33
lines changed

8 files changed

+96
-33
lines changed

‎example/tests/test_generic_viewset.py‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
from django.conf import settings
1+
from django.test import override_settings
22
from django.urls import reverse
33

44
from example.tests import TestBase
55

66

7+
@override_settings(JSON_API_FORMAT_KEYS='dasherize')
78
class GenericViewSet(TestBase):
89
"""
910
Test expected responses coming from a Generic ViewSet
1011
"""
1112

12-
def setUp(self):
13-
super(GenericViewSet, self).setUp()
14-
15-
setattr(settings, 'JSON_API_FORMAT_KEYS', 'dasherize')
16-
17-
def tearDown(self):
18-
19-
setattr(settings, 'JSON_API_FORMAT_KEYS', 'camelize')
20-
2113
def test_default_rest_framework_behavior(self):
2214
"""
2315
This is more of an example really, showing default behavior

‎example/tests/test_model_viewsets.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import pytest
2-
from django.conf import settings
32
from django.contrib.auth import get_user_model
43
from django.urls import reverse
54
from django.utils import encoding
5+
from django.test import override_settings
66

77
from example.tests import TestBase
88

99

10+
@override_settings(JSON_API_FORMAT_KEYS='dasherize')
1011
class ModelViewSetTests(TestBase):
1112
"""
1213
Test usage with ModelViewSets, also tests pluralization, camelization,
@@ -21,12 +22,6 @@ def setUp(self):
2122
super(ModelViewSetTests, self).setUp()
2223
self.detail_url = reverse('user-detail', kwargs={'pk': self.miles.pk})
2324

24-
setattr(settings, 'JSON_API_FORMAT_KEYS', 'dasherize')
25-
26-
def tearDown(self):
27-
28-
setattr(settings, 'JSON_API_FORMAT_KEYS', 'camelize')
29-
3025
def test_key_in_list_result(self):
3126
"""
3227
Ensure the result has a 'user' key since that is the name of the model

‎example/tests/unit/test_settings.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
from rest_framework_json_api.settings import json_api_settings
3+
4+
5+
def test_settings_invalid():
6+
with pytest.raises(AttributeError):
7+
json_api_settings.INVALID_SETTING
8+
9+
10+
def test_settings_default():
11+
assert json_api_settings.UNIFORM_EXCEPTIONS is False
12+
13+
14+
def test_settings_override(settings):
15+
settings.JSON_API_FORMAT_KEYS = 'dasherize'
16+
assert json_api_settings.FORMAT_KEYS == 'dasherize'

‎example/tests/unit/test_utils.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
2-
from django.conf import settings
32
from django.contrib.auth import get_user_model
3+
from django.test import override_settings
44
from django.utils import six
55
from rest_framework import serializers
66
from rest_framework.generics import GenericAPIView
@@ -29,12 +29,12 @@ class Meta:
2929
def test_get_resource_name():
3030
view = APIView()
3131
context = {'view': view}
32-
setattr(settings, 'JSON_API_FORMAT_TYPES', None)
33-
assert 'APIViews' == utils.get_resource_name(context), 'not formatted'
32+
withoverride_settings(JSON_API_FORMAT_TYPES=None):
33+
assert 'APIViews' == utils.get_resource_name(context), 'not formatted'
3434

3535
context = {'view': view}
36-
setattr(settings, 'JSON_API_FORMAT_TYPES', 'dasherize')
37-
assert 'api-views' == utils.get_resource_name(context), 'derived from view'
36+
withoverride_settings(JSON_API_FORMAT_TYPES='dasherize'):
37+
assert 'api-views' == utils.get_resource_name(context), 'derived from view'
3838

3939
view.model = get_user_model()
4040
assert 'users' == utils.get_resource_name(context), 'derived from view model'

‎rest_framework_json_api/exceptions.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from django.conf import settings
21
from django.utils.translation import ugettext_lazy as _
32
from rest_framework import exceptions, status
43

54
from rest_framework_json_api import utils
65

6+
from .settings import json_api_settings
7+
78

89
def rendered_with_json_api(view):
910
from rest_framework_json_api.renderers import JSONRenderer
@@ -29,7 +30,7 @@ def exception_handler(exc, context):
2930

3031
# Use regular DRF format if not rendered by DRF JSON API and not uniform
3132
is_json_api_view = rendered_with_json_api(context['view'])
32-
is_uniform = getattr(settings, 'JSON_API_UNIFORM_EXCEPTIONS', False)
33+
is_uniform = json_api_settings.UNIFORM_EXCEPTIONS
3334
if not is_json_api_view and not is_uniform:
3435
return response
3536

‎rest_framework_json_api/parsers.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
22
Parsers
33
"""
4-
from django.conf import settings
54
from django.utils import six
65
from rest_framework import parsers
76
from rest_framework.exceptions import ParseError
87

98
from . import exceptions, renderers, serializers, utils
9+
from .settings import json_api_settings
1010

1111

1212
class JSONParser(parsers.JSONParser):
@@ -32,7 +32,7 @@ class JSONParser(parsers.JSONParser):
3232
@staticmethod
3333
def parse_attributes(data):
3434
attributes = data.get('attributes')
35-
uses_format_translation = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
35+
uses_format_translation = json_api_settings.FORMAT_KEYS
3636

3737
if not attributes:
3838
return dict()
@@ -44,7 +44,7 @@ def parse_attributes(data):
4444

4545
@staticmethod
4646
def parse_relationships(data):
47-
uses_format_translation = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
47+
uses_format_translation = json_api_settings.FORMAT_KEYS
4848
relationships = data.get('relationships')
4949

5050
if not relationships:

‎rest_framework_json_api/settings.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
This module provides the `json_api_settings` object that is used to access
3+
JSON API REST framework settings, checking for user settings first, then falling back to
4+
the defaults.
5+
"""
6+
7+
from django.conf import settings
8+
from django.core.signals import setting_changed
9+
10+
JSON_API_SETTINGS_PREFIX = 'JSON_API_'
11+
12+
DEFAULTS = {
13+
'FORMAT_KEYS': False,
14+
'FORMAT_RELATION_KEYS': None,
15+
'FORMAT_TYPES': False,
16+
'PLURALIZE_RELATION_TYPE': None,
17+
'PLURALIZE_TYPES': False,
18+
'UNIFORM_EXCEPTIONS': False,
19+
}
20+
21+
22+
class JSONAPISettings(object):
23+
"""
24+
A settings object that allows json api settings to be access as
25+
properties.
26+
"""
27+
28+
def __init__(self, user_settings=settings, defaults=DEFAULTS):
29+
self.defaults = defaults
30+
self.user_settings = user_settings
31+
32+
def __getattr__(self, attr):
33+
if attr not in self.defaults:
34+
raise AttributeError("Invalid JSON API setting: '%s'" % attr)
35+
36+
value = getattr(self.user_settings, JSON_API_SETTINGS_PREFIX + attr, self.defaults[attr])
37+
38+
# Cache the result
39+
setattr(self, attr, value)
40+
return value
41+
42+
43+
json_api_settings = JSONAPISettings()
44+
45+
46+
def reload_json_api_settings(*args, **kwargs):
47+
django_setting = kwargs['setting']
48+
setting = django_setting.replace(JSON_API_SETTINGS_PREFIX, '')
49+
value = kwargs['value']
50+
if setting in DEFAULTS.keys():
51+
if value is not None:
52+
setattr(json_api_settings, setting, value)
53+
elif hasattr(json_api_settings, setting):
54+
delattr(json_api_settings, setting)
55+
56+
57+
setting_changed.connect(reload_json_api_settings)

‎rest_framework_json_api/utils.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from rest_framework.exceptions import APIException
1919
from rest_framework.serializers import ManyRelatedField # noqa: F401
2020

21+
from .settings import json_api_settings
22+
2123
try:
2224
from rest_framework_nested.relations import HyperlinkedRouterField
2325
except ImportError:
@@ -104,7 +106,7 @@ def format_keys(obj, format_type=None):
104106
:format_type: Either 'dasherize', 'camelize' or 'underscore'
105107
"""
106108
if format_type is None:
107-
format_type = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
109+
format_type = json_api_settings.FORMAT_KEYS
108110

109111
if format_type in ('dasherize', 'camelize', 'underscore', 'capitalize'):
110112

@@ -136,7 +138,7 @@ def format_keys(obj, format_type=None):
136138

137139
def format_value(value, format_type=None):
138140
if format_type is None:
139-
format_type = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
141+
format_type = json_api_settings.FORMAT_KEYS
140142
if format_type == 'dasherize':
141143
# inflection can't dasherize camelCase
142144
value = inflection.underscore(value)
@@ -156,17 +158,17 @@ def format_relation_name(value, format_type=None):
156158
"settings are now 'JSON_API_FORMAT_TYPES' and 'JSON_API_PLURALIZE_TYPES'"
157159
)
158160
if format_type is None:
159-
format_type = getattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', None)
160-
pluralize = getattr(settings, 'JSON_API_PLURALIZE_RELATION_TYPE', None)
161+
format_type = json_api_settings.FORMAT_RELATION_KEYS
162+
pluralize = json_api_settings.PLURALIZE_RELATION_TYPE
161163
return format_resource_type(value, format_type, pluralize)
162164

163165

164166
def format_resource_type(value, format_type=None, pluralize=None):
165167
if format_type is None:
166-
format_type = getattr(settings, 'JSON_API_FORMAT_TYPES', False)
168+
format_type = json_api_settings.FORMAT_TYPES
167169

168170
if pluralize is None:
169-
pluralize = getattr(settings, 'JSON_API_PLURALIZE_TYPES', False)
171+
pluralize = json_api_settings.PLURALIZE_TYPES
170172

171173
if format_type:
172174
# format_type will never be None here so we can use format_value

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /