-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Integrate wizards with app registration #6436
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
85edda6
4a59081
156f07d
a54a4ee
2d28a1b
4a47450
56931e8
5d4c924
f70ed48
77d61d0
107ed5b
97c648a
785153c
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from mock import patch, Mock | ||
|
||
from django import forms | ||
from django.apps import apps | ||
from django.core.urlresolvers import reverse | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.forms.models import ModelForm | ||
|
@@ -162,28 +163,56 @@ def test_endpoint_auth_required(self): | |
|
||
class TestWizardPool(WizardTestMixin, CMSTestCase): | ||
|
||
def test_discover(self): | ||
wizard_pool._reset() | ||
self.assertFalse(wizard_pool._discovered) | ||
self.assertEqual(len(wizard_pool._entries), 0) | ||
wizard_pool._discover() | ||
self.assertTrue(wizard_pool._discovered) | ||
|
||
def test_register_unregister_isregistered(self): | ||
wizard_pool._clear() | ||
self.assertEqual(len(wizard_pool._entries), 0) | ||
wizard_pool.register(self.page_wizard) | ||
# Now, try to register the same thing | ||
def tearDown(self): | ||
# Clean up in case anything has been removed or added to the | ||
# registered wizards, so other tests don't have problems | ||
extension = apps.get_app_config('cms').cms_extension | ||
expected_wizards = { | ||
cms_page_wizard.id: cms_page_wizard.title, | ||
sample_wizard.id: sample_wizard.title, | ||
cms_subpage_wizard.id: cms_subpage_wizard.title, | ||
} | ||
if extension.wizards != expected_wizards: | ||
extension.wizards = expected_wizards | ||
|
||
def test_discovered_returns_true(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 removed |
||
# Backwards compatibility | ||
self.assertTrue(wizard_pool.discovered) | ||
|
||
def test_is_registered_for_registered_wizard(self): | ||
# Backwards compatibility | ||
is_registered = wizard_pool.is_registered(cms_page_wizard) | ||
self.assertTrue(is_registered) | ||
|
||
def test_is_registered_for_unregistered_wizard(self): | ||
# Backwards compatibility | ||
is_registered = wizard_pool.is_registered(self.page_wizard) | ||
self.assertFalse(is_registered) | ||
|
||
def test_unregister_registered_wizard(self): | ||
# Test for backwards compatibility only. | ||
was_unregistered = wizard_pool.unregister(cms_page_wizard) | ||
|
||
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 |
||
registered_wizards = apps.get_app_config('cms').cms_extension.wizards | ||
self.assertNotIn(cms_page_wizard.id, registered_wizards) | ||
self.assertTrue(was_unregistered) | ||
|
||
def test_unregister_unregistered_wizard(self): | ||
# Test for backwards compatibility only. | ||
was_unregistered = wizard_pool.unregister(self.page_wizard) | ||
self.assertFalse(was_unregistered) | ||
|
||
def test_register_already_registered_wizard(self): | ||
# Test for backwards compatibility only. | ||
with self.assertRaises(AlreadyRegisteredException): | ||
wizard_pool.register(self.page_wizard) | ||
wizard_pool.register(cms_page_wizard) | ||
|
||
self.assertEqual(len(wizard_pool._entries), 1) | ||
self.assertTrue(wizard_pool.is_registered(self.page_wizard)) | ||
self.assertTrue(wizard_pool.unregister(self.page_wizard)) | ||
self.assertEqual(len(wizard_pool._entries), 0) | ||
def test_register_unregistered_wizard(self): | ||
# Test for backwards compatibility only. | ||
wizard_pool.register(self.page_wizard) | ||
|
||
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 |
||
# Now, try to unregister something that is not registered | ||
self.assertFalse(wizard_pool.unregister(self.user_settings_wizard)) | ||
registered_wizards = apps.get_app_config('cms').cms_extension.wizards | ||
self.assertIn(self.page_wiza 8000 rd.id, registered_wizards) | ||
|
||
@patch('cms.wizards.wizard_pool.get_entry') | ||
def test_get_entry(self, mocked_get_entry): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.apps import apps | ||
from django.utils.module_loading import autodiscover_modules | ||
from django.utils.translation import ugettext as _ | ||
|
||
|
@@ -21,49 +22,28 @@ def entry_choices(user, page): | |
|
||
|
||
class WizardPool(object): | ||
_entries = {} | ||
_discovered = False | ||
|
||
def __init__(self): | ||
self._reset() | ||
|
||
# PRIVATE METHODS ----------------- | ||
|
||
def _discover(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. After thinking about this later, I'm getting the impression I may have messed up backwards compatibility with this. Because although the method 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 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, this is bizarre. Just wrote a test with an app that doesn't have cms_config.py but only cms_wizards.py and it seems to have registered the wizard from cms_wizards.py without me adding this into the init! Will check what's up there Monday morning, but possibly all that is needed is that test? 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. Likely there is an import somewhere of cms_wizards.py which should be removed 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. So, I can't use the |
||
if not self._discovered: | ||
autodiscover_modules('cms_wizards') | ||
self._discovered = True | ||
|
||
def _clear(self): | ||
"""Simply empties the pool but does not clear the discovered flag.""" | ||
self._entries = {} | ||
|
||
def _reset(self): | ||
"""Clears the wizard pool and clears the discovered flag.""" | ||
self._clear() | ||
self._discovered = False | ||
|
||
# PUBLIC METHODS ------------------ | ||
|
||
@property | ||
def discovered(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 removed as it is undocumented |
||
""" | ||
A public getter for the private property _discovered. Note, there is no | ||
public setter. | ||
Returns whether all the wizards have been autodiscovered. | ||
|
||
NOTE: This property is for backwards compatibility only | ||
""" | ||
return self._discovered | ||
# TODO: Add deprecation warning | ||
# Always return True because autodiscovering now happens through | ||
# the app registration system, so we no longer do any | ||
# discovering of registered wizards here | ||
return True | ||
|
||
def is_registered(self, entry, **kwargs): | ||
""" | ||
Returns True if the provided entry is registered. | ||
|
||
NOTE: This method triggers pool discovery unless a «passive» kwarg | ||
is set to True | ||
NOTE: This method is for backwards compatibility only | ||
""" | ||
passive = kwargs.get('passive', False) | ||
if not passive: | ||
self._discover() | ||
return entry.id in self._entries | ||
# TODO: Add deprecation warning | ||
return entry.id in apps.get_app_config('cms').cms_extension.wizards | ||
|
||
def register(self, entry): | ||
""" | ||
|
@@ -80,19 +60,20 @@ def register(self, entry): | |
_(u"A wizard has already been registered for model: %s") % | ||
model.__name__) | ||
else: | ||
self._entries[entry.id] = entry | ||
apps.get_app_config('cms').cms_extension.wizards[entry.id] = entry | ||
|
||
def unregister(self, entry): | ||
""" | ||
If «entry» is registered into the pool, remove it. | ||
|
||
Returns True if the entry was successfully registered, else False. | ||
|
||
NOTE: This method triggers pool discovery. | ||
NOTE: This method is here for backwards compatibility only. | ||
""" | ||
# TODO: Add deprecation warning | ||
assert isinstance(entry, Wizard), u"entry must be an instance of Wizard" | ||
if self.is_registered(entry, passive=True): | ||
del self._entries[entry.id] | ||
if self.is_registered(entry): | ||
del apps.get_app_config('cms').cms_extension.wizards[entry.id] | ||
return True | ||
return False | ||
|
||
|
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.
this means we would have to keep this dict updated for every new wizard in core or tests.
I think we can just trigger a reload of the apps no?
Uh oh!
There was an error while loading. Please reload this page.
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.
There could be side effects to that (such as getting an exception for registering a wizard twice and there could be more stuff like that once more things are integrated with app registration). But I think there are other ways I can be more generic here.