Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replace get_entry_base_model by load_model_class which is more generic

  • Loading branch information...
commit c33e96e2b22613dee4e2e1bda6a3e2160b5a3f95 1 parent e2cff23
@Fantomas42 authored
View
2  docs/ref/settings.rst
@@ -24,7 +24,7 @@ Entry
ZINNIA_ENTRY_BASE_MODEL
-----------------------
-**Default value:** ``''`` (Empty string)
+**Default value:** ``'zinnia.models_bases.entry.AbstractEntry'`` (Empty string)
String defining the base model path for the Entry model. See
:doc:`/how-to/extending_entry_model` for more informations.
View
30 zinnia/models/entry.py
@@ -1,35 +1,9 @@
"""Entry model for Zinnia"""
-import warnings
-
-from django.utils.importlib import import_module
-
from zinnia.settings import ENTRY_BASE_MODEL
-from zinnia.models_bases.entry import AbstractEntry
-
-
-def get_entry_base_model():
- """
- Determine the base abstract model to inherit from,
- to build the final Entry model.
- This mecanizm allows extension and customization of
- the Entry model class.
- """
- if not ENTRY_BASE_MODEL:
- return AbstractEntry
-
- dot = ENTRY_BASE_MODEL.rindex('.')
- module_name = ENTRY_BASE_MODEL[:dot]
- class_name = ENTRY_BASE_MODEL[dot + 1:]
- try:
- _class = getattr(import_module(module_name), class_name)
- return _class
- except (ImportError, AttributeError):
- warnings.warn('%s cannot be imported' % ENTRY_BASE_MODEL,
- RuntimeWarning)
- return AbstractEntry
+from zinnia.models_bases import load_model_class
-class Entry(get_entry_base_model()):
+class Entry(load_model_class(ENTRY_BASE_MODEL)):
"""
The final Entry model based on inheritence.
"""
View
19 zinnia/models_bases/__init__.py
@@ -1 +1,20 @@
"""Base models for Zinnia"""
+from django.utils.importlib import import_module
+from django.core.exceptions import ImproperlyConfigured
+
+
+def load_model_class(model_path):
+ """
+ Load by import a class by a string path like:
+ 'module.models.MyModel'.
+ This mecanizm allows extension and customization of
+ the Entry model class.
+ """
+ dot = model_path.rindex('.')
+ module_name = model_path[:dot]
+ class_name = model_path[dot + 1:]
+ try:
+ _class = getattr(import_module(module_name), class_name)
+ return _class
+ except (ImportError, AttributeError):
+ raise ImproperlyConfigured('%s cannot be imported' % model_path)
View
4 zinnia/settings.py
@@ -15,7 +15,9 @@
ALLOW_EMPTY = getattr(settings, 'ZINNIA_ALLOW_EMPTY', True)
ALLOW_FUTURE = getattr(settings, 'ZINNIA_ALLOW_FUTURE', True)
-ENTRY_BASE_MODEL = getattr(settings, 'ZINNIA_ENTRY_BASE_MODEL', '')
+ENTRY_BASE_MODEL = getattr(settings, 'ZINNIA_ENTRY_BASE_MODEL',
+ 'zinnia.models_bases.entry.AbstractEntry')
+
ENTRY_DETAIL_TEMPLATES = getattr(
settings, 'ZINNIA_ENTRY_DETAIL_TEMPLATES', [])
ENTRY_CONTENT_TEMPLATES = getattr(
View
4 zinnia/tests/__init__.py
@@ -6,7 +6,7 @@
from zinnia.tests.entry import EntryTestCase
from zinnia.tests.entry import EntryHtmlContentTestCase
from zinnia.tests.entry import EntryAbsoluteUrlTestCase
-from zinnia.tests.entry import EntryGetBaseModelTestCase
+from zinnia.tests.models_bases import LoadModelClassTestCase
from zinnia.tests.signals import SignalsTestCase
from zinnia.tests.category import CategoryTestCase
from zinnia.tests.admin import EntryAdminTestCase
@@ -43,7 +43,7 @@ def suite():
loader = TestLoader()
test_cases = (ManagersTestCase, EntryTestCase,
- EntryGetBaseModelTestCase, SignalsTestCase,
+ LoadModelClassTestCase, SignalsTestCase,
EntryHtmlContentTestCase, CategoryTestCase,
ZinniaViewsTestCase, ZinniaFeedsTestCase,
ZinniaSitemapsTestCase, ComparisonTestCase,
View
25 zinnia/tests/entry.py
@@ -1,5 +1,4 @@
"""Test cases for Zinnia's Entry"""
-import warnings
from datetime import timedelta
from django.test import TestCase
@@ -19,9 +18,6 @@
from zinnia.models.author import Author
from zinnia.flags import PINGBACK, TRACKBACK
from zinnia.tests.utils import datetime
-from zinnia.models import entry as entry_models
-from zinnia.models_bases.entry import AbstractEntry
-from zinnia.models.entry import get_entry_base_model
from zinnia import url_shortener as shortener_settings
@@ -390,24 +386,3 @@ def test_get_absolute_url_with_timezone(self):
'/2013/01/01/my-entry/')
self.check_get_absolute_url(datetime(2013, 1, 1, 23, 0),
'/2013/01/02/my-entry/')
-
-
-class EntryGetBaseModelTestCase(TestCase):
-
- def setUp(self):
- self.original_entry_base_model = entry_models.ENTRY_BASE_MODEL
-
- def tearDown(self):
- entry_models.ENTRY_BASE_MODEL = self.original_entry_base_model
-
- def test_get_entry_base_model(self):
- entry_models.ENTRY_BASE_MODEL = ''
- self.assertEquals(get_entry_base_model(), AbstractEntry)
-
- entry_models.ENTRY_BASE_MODEL = 'mymodule.myclass'
- with warnings.catch_warnings(record=True) as w:
- self.assertEquals(get_entry_base_model(), AbstractEntry)
- self.assertTrue(issubclass(w[-1].category, RuntimeWarning))
-
- entry_models.ENTRY_BASE_MODEL = 'zinnia.models.entry.AbstractEntry'
- self.assertEquals(get_entry_base_model(), AbstractEntry)
View
16 zinnia/tests/models_bases.py
@@ -0,0 +1,16 @@
+"""Test cases for zinnia.models_bases"""
+from django.test import TestCase
+from django.core.exceptions import ImproperlyConfigured
+
+from zinnia.models_bases import load_model_class
+from zinnia.models_bases.entry import AbstractEntry
+
+
+class LoadModelClassTestCase(TestCase):
+
+ def test_load_model_class(self):
+ self.assertEquals(
+ load_model_class('zinnia.models_bases.entry.AbstractEntry'),
+ AbstractEntry)
+ self.assertRaises(ImproperlyConfigured,
+ load_model_class, 'invalid.path.models.Toto')
Please sign in to comment.
Something went wrong with that request. Please try again.