How to package a Django application as an addon¶

See also

Register the addon¶

Before your addon can be uploaded, the Divio Cloud must be ready to receive it.

Select Add custom addon from Personal Addons in the Divio Control Panel, or simply go straight to Add custom addon.

  • Package Name: must be unique on the system. We recommend prefixing it with your own name, forexample susan-example-application.
  • Name: e.g. Susan's Django Debug Toolbar
  • License: select a predefined license for your addon (or leave it blank and add your own later.)
  • Organisation: select an organisation if appropriate.
    When you hit Create addon, the addon will be registered on the system.

Important

The package name must not contain underscores. See the note in the addon packagingtutorial for more information.

Add the packaging files¶

We need to work in the project’s addons-dev directory. Create a newdirectory there with the same name as the Package Name.

Select Package Information from your addon’s menu. Download the packagingfiles, and add them to the addon. It should look something like this:

  1. addons-dev/
  2. susan-example-application/
  3. addon.json
  4. LICENSE
  5. MANIFEST.in
  6. README.rst
  7. setup.py
  8. susan_example_application/
  9. __init__.py

Now let’s go through the files one by one.

The setup.py file¶

All the lines you need in the setup.py will be provided automatically inthe downloaded version, with the exception of the install_requiresargument:

If your addon installs an application¶

In this case, you will need to add the package to be installed to theinstall_requires argument, for exampleinstall_requires=["example_application==1.8.3"].

If your addon contains an application¶

If on the other hand, for example if the application is not available on PyPI,simply add it as the inner application directory.

The addon will then contain some additional files:

  1. addons-dev/
  2. susan-example-application/
  3. [...]
  4. susan_example_application/
  5. __init__.py
  6. admin.py
  7. apps.py
  8. migrations/
  9. __init__.py
  10. models.py
  11. tests.py
  12. views.py

Add any dependencies of the application to install_requires of setup.py.

The init.py file¶

setup.py expects to find a version number in the addon, attutorialdjangodebug_toolbar.__version:

For an addon that installs a package¶

We recommend providing a version number that tracks the package’s versionnumber - for example, if the addon installs version 1.8.3, the addon’sversion numbers should be 1.8.3.1, 1.8.3.2 and so on.

For an addon that includes a package¶

We recommend some form of semantic versioning.

The other packaging files¶

The other packaging files are simpler:

  • README.rst: If you haven’t already provided a description via the Control Panel, this will beempty. If you plan to share your addon with other users, it’s important to provide a usefulREADME.
  • MANIFEST.in: The default MANIFEST.in takes care of most non-Python files that an addon islikely to need the setup tools to take care of: LICENSE, plus directories for LICENSE, plusdirectories for boilerplates, templates, static and locale files.
  • LICENSE: Make sure the license terms are appropriate.
  • addon.json: We recommend leaving this as it is. Although you can use it to add multiplepackages to INSTALLED_APPS, it’s better to do this in aldryn_config.py (see below).

Add configuration¶

Create aldryn_config.py¶

If your application requires any settings of its own, you will need to managethem in aldryn_config.py, placed at the root of your application. The general form is:

  1. from aldryn_client import forms
  2.  
  3. class Form(forms.BaseForm):
  4. def to_settings(self, data, settings):
  5.  
  6. settings['INSTALLED_APPS'].extend([SOME_APPLICATION])
  7. settings['ENABLE_FLIDGETS'] = True
  8.  
  9. return settings

See how to configure settings inaldryn_config.py for more details and examples.

Provide form-based configuration¶

You can use the Form class to allow configuration via the Control Panel.

See adding form fields for user configuraionfor more information.

Provide URL configuration¶

Not all addons will have their own URL configurations that need to be includedin a project, but if they do, you can add them. See how to include an addon’s URL configuration for more details.

Check the addon¶

Test it¶

Your addon is now ready to be tested.

Add it the package name to the INSTALLED_ADDONS in settings.py. Thisadds it to the list of addons that the project will “watch”.

Run:

  1. divio project develop <package name>

You can test that the project now works as expected.

Validate it¶

Now make sure you’re in the addons-dev/<package name> directory.

Now, running divio addon validate should now confirm that the addon isvalid:

  1. divio addon validate
  2. Addon is valid!

Upload the addon¶

Upload with divio addon upload.

This version of the addon will be placed into the Alpha release channel. Ifyou visit the its Versions page, you’ll be able to change the release channel.

Your addon is now available for installation into projects via the controlpanel. If you make it public, other users will be able to install it too.

You can continue uploading new versions of it, as long as each has its ownunique version number.

原文: http://docs.divio.com/en/latest/how-to/create-addon.html