Skip to content

Commit c106169

Browse files
authored
Merge pull request #181 from TMiguelT/resource_init_args
Allow custom resource constructor args
2 parents 8ded23d + 3de105d commit c106169

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

flask_rest_jsonapi/api.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ def route(self, resource, view, *urls, **kwargs):
6969
resource.view = view
7070
url_rule_options = kwargs.get('url_rule_options') or dict()
7171

72-
view_func = resource.as_view(view)
72+
# Allow the customization of the resource class instance
73+
resource_args = kwargs.get('resource_args', [])
74+
resource_kwargs = kwargs.get('resource_kwargs', {})
75+
76+
view_func = resource.as_view(view, *resource_args, **resource_kwargs)
7377

7478
if 'blueprint' in kwargs:
7579
resource.view = '.'.join([kwargs['blueprint'].name, resource.view])

flask_rest_jsonapi/resource.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __new__(cls, name, bases, d):
5353
class Resource(MethodView):
5454
"""Base resource class"""
5555

56-
def __new__(cls):
56+
def __new__(cls, *args, **kwargs):
5757
"""Constructor of a resource instance"""
5858
if hasattr(cls, '_data_layer'):
5959
cls._data_layer.resource = cls

tests/test_sqlalchemy_data_layer.py

+41
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from flask_rest_jsonapi.data_layers.alchemy import SqlalchemyDataLayer
2020
from flask_rest_jsonapi.data_layers.base import BaseDataLayer
2121
from flask_rest_jsonapi.data_layers.filtering.alchemy import Node
22+
from flask_rest_jsonapi.decorators import check_headers, check_method_requirements, jsonapi_exception_formatter
2223
import flask_rest_jsonapi.decorators
2324
import flask_rest_jsonapi.resource
2425
import flask_rest_jsonapi.schema
@@ -1025,6 +1026,46 @@ def test_get_list_response(client, register_routes):
10251026
assert response.status_code == 200, response.json['errors']
10261027

10271028

1029+
class TestResourceArgs:
1030+
def test_resource_args(self, app):
1031+
class TestResource(ResourceDetail):
1032+
"""
1033+
This fake resource always renders a constructor parameter
1034+
"""
1035+
def __init__(self, *args, **kwargs):
1036+
super(TestResource, self).__init__()
1037+
self.constant = args[0]
1038+
1039+
def get(self):
1040+
return self.constant
1041+
api = Api(app=app)
1042+
api.route(TestResource, 'resource_args', '/resource_args', resource_args=['hello!'])
1043+
api.init_app()
1044+
with app.test_client() as client:
1045+
rv = client.get('/resource_args')
1046+
assert rv.json == 'hello!'
1047+
1048+
def test_resource_kwargs(self, app):
1049+
class TestResource(ResourceDetail):
1050+
"""
1051+
This fake resource always renders a constructor parameter
1052+
"""
1053+
def __init__(self, *args, **kwargs):
1054+
super(TestResource, self).__init__()
1055+
self.constant = kwargs.get('constant')
1056+
1057+
def get(self):
1058+
return self.constant
1059+
api = Api(app=app)
1060+
api.route(TestResource, 'resource_kwargs', '/resource_kwargs', resource_kwargs={
1061+
'constant': 'hello!'
1062+
})
1063+
api.init_app()
1064+
with app.test_client() as client:
1065+
rv = client.get('/resource_kwargs')
1066+
assert rv.json == 'hello!'
1067+
1068+
10281069
# test various Accept headers
10291070
def test_single_accept_header(client, register_routes):
10301071
with client:

0 commit comments

Comments
 (0)