Skip to content

Commit 0d3ed6d

Browse files
committed
Issue #25: __model_cls__ attribute validation within metaclass
1 parent d2dbac4 commit 0d3ed6d

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

domain_models/views.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
"""Contextual view module."""
22
from domain_models import models
3+
import six
34

45

6+
class ContextViewMetaClass(type):
7+
"""Context view meta class."""
8+
9+
def __new__(mcs, class_name, bases, attributes):
10+
"""Context view class factory."""
11+
if bases[0] is not object:
12+
__model_cls__ = attributes.get('__model_cls__')
13+
if __model_cls__ is None:
14+
raise AttributeError("Attribute __model_cls__ is required.")
15+
if not issubclass(__model_cls__, models.DomainModel):
16+
raise TypeError("Attribute __model_cls__ must be subclass of "
17+
"DomainModel.")
18+
19+
return type.__new__(mcs, class_name, bases, attributes)
20+
21+
22+
@six.add_metaclass(ContextViewMetaClass)
523
class ContextView(object):
624
"""Contextual view class."""
725

@@ -12,10 +30,6 @@ def __init__(self, model):
1230
1331
:param model: DomainModel
1432
"""
15-
if not issubclass(self.__model_cls__, models.DomainModel):
16-
raise TypeError("Attribute __model__ must be subclass of "
17-
"DomainModel")
18-
1933
if not isinstance(model, self.__model_cls__):
2034
raise TypeError("\"{0}\" is not an instance of {1}".format(
2135
model, self.__model_cls__))

tests/test_context_view.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,6 @@ def get_data(self):
111111
}
112112

113113

114-
class ModelUndefinedContext(views.ContextView):
115-
"""Model undefined"""
116-
117-
def get_data(self):
118-
pass
119-
120-
121-
class WrongModelContext(views.ContextView):
122-
"""Wrong model defined."""
123-
__model_cls__ = type
124-
125-
def get_data(self):
126-
pass
127-
128-
129-
class GetterUndefinedContext(views.ContextView):
130-
"""Getter undefined."""
131-
__model_cls__ = Profile
132-
133-
134114
class TestContextView(unittest.TestCase):
135115
main_photo = Photo(id=1, title='main photo', path='path/to/the/main/photo',
136116
public=True)
@@ -151,14 +131,19 @@ def test_wrong_model_passed(self):
151131
PublicProfile("invalid argument")
152132

153133
def test_model_undefined(self):
154-
with self.assertRaises(TypeError):
155-
ModelUndefinedContext("it doesn't matter")
134+
with self.assertRaises(AttributeError):
135+
class ModelUndefinedContext(views.ContextView):
136+
pass
156137

157138
def test_wrong_model_defined(self):
158139
with self.assertRaises(TypeError):
159-
WrongModelContext("it doesn't matter")
140+
class WrongModelContext(views.ContextView):
141+
__model_cls__ = type
160142

161143
def test_getter_undefined(self):
144+
class GetterUndefinedContext(views.ContextView):
145+
__model_cls__ = Profile
146+
162147
contextual_view = GetterUndefinedContext(self.profile)
163148
with self.assertRaises(NotImplementedError):
164149
contextual_view.get_data()

0 commit comments

Comments
 (0)