views:

149

answers:

2

I have a django project which is installed by customers on their servers. I've got a few more apps which are optional plugins of functionality that can be installed/uninstalled.

I'd like a simple way to package these plugin apps to make the install/uninstall painless. I dont want them to copy the template files to one directory, app to another one, media to a third one and so on. I would prefer that they need not edit settings.py, though its okay if it can't be helped.

The ideal situation would be if they could simply unzip to a location on the python path (maybe a special plugin directory?), and delete it to uninstall. Is there an easy way to package the apps so that they can be installed this way?

+4  A: 

I'll skip over discussion of Python packaging (distutils, setuptools, pip, etc), since it sounds like you'd prefer using simple zip files or tarballs. I'll address the "pain points" you mentioned one at a time:

Template files: As long as you have 'django.template.loaders.app_directories.load_template_source' included in the TEMPLATE_LOADERS setting of your projects, you shouldn't have to worry about this one. Each of your apps can have a "templates/" subdirectory, and templates in there will be loaded just as if they were in your project-wide templates directory.

Media files: App media is a pain. For development, you can use a custom serve_media view that operates similarly to the app_directories template loader (looks for media in each app). In production, you have to either copy the files, use symbolic links, or use webserver-level aliases. There are several utility apps out there that try to smooth over this problem; I now use django-staticfiles.

Editing settings.py: No simple way around this one. For its models, template tags, management commands, etc to work, an app has to be listed in INSTALLED_APPS. What you could do is write some custom code in your settings.py that lists the contents of a certain directory and dynamically adds the packages it finds there to INSTALLED_APPS. A little bit dangerous (think carefully about who has permissions to place files in that directory, because they have the keys to your kingdom), and new files there will only be detected on a server reload, but it should work.

I think if you put together those solutions, it's possible to achieve your ideal situation: unzip to install, delete to uninstall.

Carl Meyer
Thanks for the link to djano-media-utils. Thats the biggest pain in the mechanism. Wish we could specify multiple media dirs too. I guess dynamically adding to INSTALLED_APPS is an option. Since you need to access the server to place the plugin, they person installing has access to all the files anyway. Have you tried this before?
Siddhi
Nope, I've never tried a dynamic INSTALLED_APPS setting. In concept there's nothing too complicated about it. In practice one trouble might be the need to restart the production web server anytime apps are added/removed from the plugins directory. Also if plugin apps can include models, you'd need a plan for making the necessary syncdb happen.
Carl Meyer
A: 

Editing settings.py: Your plugin can read its settings from its own settings file in its own directory. They'd only need to edit the root settings.py to add/remove the plug-in path from "INSTALLED_APPS".

John Mee