-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Introduced app registration system #6421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4f4139e
7db2fc9
0394871
0ee728e
f79f575
f329f22
b5f1825
5b88aa1
c9f064d
e53eac3
f2299db
2416687
ec35c49
5968fc9
c1b606e
367e49f
31d767d
d908a66
093408c
263c1c5
741a803
bcc02f4
e64a652
6a55666
9f3f6f2
6e683d9
0850fd1
2055642
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
import inspect | ||
from importlib import import_module | ||
|
||
from django.apps import apps | ||
from django.utils.module_loading import module_has_submodule | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from cms.app_base import CMSAppConfig | ||
|
||
|
||
CMS_CONFIG_NAME = 'cms_config' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can go into the |
||
|
||
|
@@ -49,28 +52,19 @@ def autodiscover_cms_configs(): | |
"class which inherits from CMSAppConfig") | ||
|
||
|
||
def get_cms_apps_with_features(): | ||
def get_cms_apps(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. decorate with |
||
""" | ||
Returns app configs of apps with cms features | ||
Returns django app configs of apps with a cms config | ||
""" | ||
apps_with_features = [] | ||
cms_apps = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be a list comprehensio 8000 n |
||
|
||
for app_config in apps.get_app_configs(): | ||
# The cms_app attr is added by the autodiscover_cms_configs | ||
# function if a cms_config.py file with a suitable class is found. | ||
is_cms_app = hasattr(app_config, 'cms_app') | ||
if is_cms_app: | ||
# The configure_app method is only present on the cms | ||
# app class if the app provides a cms feature. For classes | ||
# that only have config this method will not be present. | ||
has_cms_extension = hasattr( | ||
app_config.cms_app, 'configure_app') | ||
else: | ||
has_cms_extension = False | ||
if is_cms_app and has_cms_extension: | ||
apps_with_features.append(app_config) | ||
if hasattr(app_config, 'cms_app'): | ||
cms_apps.append(app_config) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove newline |
||
return apps_with_features | ||
return cms_apps | ||
|
||
|
||
def configure_cms_apps(apps_with_features): | ||
|
@@ -79,8 +73,7 @@ def configure_cms_apps(apps_with_features): | |
and run code to register them with their config | ||
""" | ||
for app_with_feature in apps_with_features: | ||
enabled_property = "{app_label}_enabled".format( | ||
app_label=app_with_feature.label) | ||
enabled_property = "{}_enabled".format(app_with_feature.label) | ||
configure_app = app_with_feature.cms_app.configure_app | ||
|
||
for app_config in apps.get_app_configs(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there should be a function that returns all apps with cms configs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the spirit of YAGNI, wouldn't it be better to add that when we actually need it rather than before? It's a simple thing to add later if it is needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would use it here, and will be good because then we can compute it once at runtime There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You want one with all cms apps (both extensions and configs) or one with just the cms apps that define a config? Cause actually I think in this case it makes more sense if it's just the apps that have a cms_config attribute. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, just the ones with cms config |
||
|
@@ -90,7 +83,3 @@ def configure_cms_apps(apps_with_features): | |
if getattr(app_config.cms_app, enabled_property, False): | ||
# Feature enabled for this app so configure | ||
configure_app(app_config) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should pass the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, that will mean that I'll also need to override the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I've just done this and looked at how the code is now, it has occurred to me that it would probably be neater to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't like it because it implies that they're different while they're not, most cases will not need to use app_config. |
||
|
||
|
||
class CMSAppConfig(): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,34 +56,30 @@ def test_raises_exception_when_more_than_one_cms_app_class_found_in_cms_file(sel | |
app_registration.autodiscover_cms_configs() | ||
|
||
|
||
class GetCmsAppsWithFeaturesTestCase(CMSTestCase): | ||
class GetCmsAppsTestCase(CMSTestCase): | ||
|
||
@patch.object(apps, 'get_app_configs') | ||
def test_returns_only_apps_with_features(self, mocked_apps): | ||
def test_returns_only_cms_apps(self, mocked_apps): | ||
# apps with cms_app attr and a configure method | ||
app_with_cms_ext1 = Mock(cms_app=Mock(spec=['configure_app'])) | ||
app_with_cms_ext2 = Mock(cms_app=Mock(spec=['configure_app'])) | ||
# app with cms_app attr without a configure method | ||
# this throws an AttributeError if configure_app is accessed | ||
app_with_cms_config = Mock(cms_app=Mock(spec=[])) | ||
cms_app1 = Mock(cms_app=Mock(spec=['configure_app'])) | ||
cms_app2 = Mock(cms_app=Mock(spec=['configure_app'])) | ||
# app without cms_app attr | ||
# this throws an AttributeError if cms_app is accessed | ||
non_cms_app = Mock(spec=[]) | ||
# mock what apps have been installed | ||
mocked_apps.return_value = [ | ||
app_with_cms_ext1, | ||
app_with_cms_config, | ||
app_with_cms_ext2, | ||
cms_app1, | ||
cms_app2, | ||
non_cms_app, | ||
] | ||
|
||
apps_with_features = app_registration.get_cms_apps_with_features() | ||
cms_apps = app_registration.get_cms_apps() | ||
|
||
# Of the 4 installed apps only 2 have features (1 is a | ||
# non-cms app and 1 is a cms app without a feature) | ||
# Of the 3 installed apps only 2 have features (1 is a | ||
# non-cms app) | ||
self.assertListEqual( | ||
apps_with_features, | ||
[app_with_cms_ext1, app_with_cms_ext2]) | ||
cms_apps, | ||
[cms_app1, cms_app2]) | ||
|
||
|
||
class ConfigureCmsAppsTestCase(CMSTestCase): | ||
|
@@ -255,3 +251,11 @@ def test_cms_apps_setup_after_setup_function_run(self): | |
# whole app registration code to run through. | ||
self.assertEqual(feature_app.cms_app.num_configured_apps, 1) | ||
self.assertTrue(config_app.cms_app.configured) | ||
|
||
@override_settings(INSTALLED_APPS=[ | ||
'cms.tests.test_app_registry.app_with_cms_config', | ||
'cms.tests.test_app_registry.app_using_non_feature' | ||
]) | ||
def test_raises_not_implemented_exception_when_feature_app_doesnt_implement_configure_method(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be shortened to test_raises_not_implemented_exception and the rest explained in comment |
||
with self.assertRaises(NotImplementedError): | ||
setup.setup_cms_apps() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
default_app_config = 'cms.tests.test_app_registry.app_using_non_feature.apps.NonFeatureCMSConfig' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class NonFeatureCMSConfig(AppConfig): | ||
name = 'cms.tests.test_app_registry.app_using_non_feature' | ||
label = 'app_using_non_feature' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from cms.app_base import CMSAppConfig | ||
|
||
|
||
class NonFeatureCMSConfig(CMSAppConfig): | ||
# Attempting to use features from a cms app that doesnt define any | ||
# features | ||
app_with_cms_config_enabled = True |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
from cms.utils.compat.dj import is_installed as app_is_installed | ||
from cms.app_registration import ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import order. |
||
autodiscover_cms_configs, get_cms_apps_with_features, | ||
autodiscover_cms_configs, get_cms_apps, | ||
configure_cms_apps) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please change to: from cms.app_registration import (
autodiscover_cms_configs,
configure_cms_apps,
get_cms_apps,
) |
||
|
||
|
||
|
@@ -50,5 +50,5 @@ def setup_cms_apps(): | |
any of this functionality. | ||
""" | ||
autodiscover_cms_configs() | ||
apps_with_cms_features = get_cms_apps_with_features() | ||
configure_cms_apps(apps_with_cms_features) | ||
cms_apps = get_cms_apps() | ||
configure_cms_apps(cms_apps) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when not passing arguments, raise
NotImplementedError
, vsNotImplementedError()