Skip to content

Commit

Permalink
Allow inherited resources to set a custom model instance before insta…
Browse files Browse the repository at this point in the history
…ntiating (fixes #1472)
  • Loading branch information
leplatrem committed Jan 31, 2018
1 parent 71ad7ad commit 8eacc08
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ This document describes changes between each past release.
8.1.4 (unreleased)
------------------

- Nothing changed yet.
**Bug fixes**

- Allow inherited resources to set a custom model instance before instantiating (fixes #1472)

8.1.3 (2018-01-26)
------------------
Expand Down
13 changes: 7 additions & 6 deletions kinto/core/resource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ def __init__(self, request, context=None):
# Authentication to storage is transmitted as is (cf. cloud_storage).
auth = request.headers.get('Authorization')

self.model = self.default_model(
storage=request.registry.storage,
id_generator=self.id_generator,
collection_id=classname(self),
parent_id=parent_id,
auth=auth)
if not hasattr(self, 'model'):
self.model = self.default_model(
storage=request.registry.storage,
id_generator=self.id_generator,
collection_id=classname(self),
parent_id=parent_id,
auth=auth)

# Initialize timestamp as soon as possible.
self.timestamp
Expand Down
14 changes: 14 additions & 0 deletions tests/core/resource/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock
import unittest
from pyramid import httpexceptions

from kinto.core.resource import UserResource, ShareableResource
Expand Down Expand Up @@ -66,3 +67,16 @@ def test_get_parent_can_be_overridded(self):
parent_id = self.resource.get_parent_id(request)
self.assertEquals(parent_id, 'overrided')
self.assertEquals(self.resource.model.parent_id, 'overrided')


class CustomModelResource(UserResource):
def __init__(self, *args, **kwargs):
self.model = mock.MagicMock()
self.model.name = mock.sentinel.model
super().__init__(*args, **kwargs)


class CustomModelResourceTets(unittest.TestCase):
def test_custom_model_is_not_overriden(self):
c = CustomModelResource(request=mock.MagicMock())
self.assertEquals(c.model.name, mock.sentinel.model)

0 comments on commit 8eacc08

Please sign in to comment.