6. Package an addon (configuration)¶

There are a few more steps required. One feature of the Divio Cloud ControlPanel is the option to configure the settings associated with addons via a form.

The form is a fairly standard Django Form class. It needs to be built intothe addon, so that it will be exposed on the Control Panel.

This form also has a special method called to_settings(), which is used tomanipulate the project’s settings for the addon.

The form needs to be in a file called aldryn_config.py, placed alongsidethe other addon packaging files.

6.1. Add the addon to settings¶

Note

This step will be performed automatically in a forthcoming update to theDivio CLI application when divio project develop is invoked.

Add the addon to the INSTALLEDADDONS, _inside the <INSTALLED_ADDONS>tags:

  1. INSTALLED_ADDONS = [
  2. # <INSTALLED_ADDONS> # Warning: text inside the INSTALLED_ADDONS tags is auto-generated. Manual changes will be overwritten.
  3. 'aldryn-addons',
  4. 'aldryn-django',
  5. 'aldryn-sso',
  6. 'aldryn-devsync',
  7. 'tutorial-django-debug-toolbar',
  8. # </INSTALLED_ADDONS>
  9. ]

This adds the addon to the list of addons that the project will “watch”.

Note

The presence of the addon in this list is temporary, while you aredeveloping locally. Remove it once you’re done, and don’t push it to theCloud. It won’t cause any harm, but it will be removed and could producea Git conflict next time you pull.

6.2. Create an aldryn_config.py file¶

This file will do the “heavy lifting” of configuring the project for youraddon.

When a project is built, it checks each addon for aldrynconfig.py, andcalls its Form.to_settings() method to configure settings. _Every addon onthe Cloud will therefore need, at minimum, an aldryn_config.py thatcontains:

  1. from aldryn_client import forms
  2.  
  3.  
  4. class Form(forms.BaseForm):
  5.  
  6. def to_settings(self, data, settings):
  7. return settings

Create that now, in your tutorial-django-debug-toolbar directory.

So far, it doesn’t actually do anything, but we can see that it receives asettings argument, which will contain the project’s settings that can wethen can manipulate and pass on.

6.3. Configure settings.py¶

In settings.py we currently have:

  1. if DEBUG:
  2.  
  3. INSTALLED_APPS.extend(["debug_toolbar"])
  4.  
  5. def _show_toolbar(request):
  6. return True
  7.  
  8. DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": _show_toolbar}
  9.  
  10. MIDDLEWARE_CLASSES.insert(
  11. MIDDLEWARE_CLASSES.index("django.middleware.gzip.GZipMiddleware") + 1,
  12. "debug_toolbar.middleware.DebugToolbarMiddleware"
  13. )

Remove all that from settings.py. If you refresh your project’sadmin page, you’ll see the Debug Toolbar is no longer there.

We’re going to implement those settings in the aldryn_config.py instead(note that they’re in a method now, so will look slightly different):

  1. from aldryn_client import forms
  2.  
  3.  
  4. class Form(forms.BaseForm):
  5.  
  6. def _show_toolbar(self, request):
  7. return True
  8.  
  9. def to_settings(self, data, settings):
  10.  
  11. if settings["DEBUG"]:
  12. settings["INSTALLED_APPS"].extend(["debug_toolbar"])
  13. settings["DEBUG_TOOLBAR_CONFIG"] = {"SHOW_TOOLBAR_CALLBACK": self._show_toolbar}
  14. settings["MIDDLEWARE_CLASSES"].insert(
  15. settings["MIDDLEWARE_CLASSES"].index("django.middleware.gzip.GZipMiddleware") + 1,
  16. "debug_toolbar.middleware.DebugToolbarMiddleware"
  17. )
  18.  
  19. return settings

And if you refresh the admin, the Toolbar should be back.

6.4. Configure urls.py¶

The next step is to move the URLs configuration to the addon too.

Remove all the configuration related to Django Debug Toolbar from the project’surls.py.

Check the admin - it should now raise a NoReverseMatch error, because it’slooking for djdt URLs that don’t exist.

In tutorialdjangodebug_toolbar (alongside the __init.py) add aurls.py:

  1. from django.conf.urls import url, include
  2.  
  3. import debug_toolbar
  4.  
  5. urlpatterns = [url(r'^__debug__/', include(debug_toolbar.urls))]

Notice that this time we don’t check for settings["DEBUG"] in theurls.py. We can do it in the existing check, in the to_settings() method of aldryn_config.py:

  1. def to_settings(self, data, settings):
  2.  
  3. if settings["DEBUG"]:
  4.  
  5. [...]
  6.  
  7. settings['ADDON_URLS'].append('tutorial_django_debug_toolbar.urls')
  8.  
  9. return settings

In the admin, the Toolbar should be working once more.

We now have a self-configuring addon, with only the most minimal traces of itleft in the project configuration itself.

The remaining steps are concerned with completing the configuration andpackaging of the addon for the Divio Cloud.

Note

This configuration example in aldrynconfig.py is somewhat simplified. For example,although GZipMiddleware is _probably installed, that’s not guaranteed. Our version willraise a ValueError if it’s not. For a fuller, more robust version, see thealdryn_config.py in the official Divio version of the addon.

原文: http://docs.divio.com/en/latest/introduction/06-package-addon-configuration.html