handling dependencies of django

There are lots of ways to use plugins without installing them to main system. Python virtualenv is easy and common to use for this purposes. Soon I'll write an article about it too. But the main problem of Python Virtualenv is it's complexity. You basically need to install something, make an environment and so on... What if you just need to implement nice and easy "Django way".
Here direct copy comes in mind. It's the easiest way, as for me.
Basic concept is:
   - download plugin (be it in egg, zip, tar.gz or any other format)
   - unpack it 
   - copy to django app dir (simple drag and copy files to eclipse in my case)
   - settings.py modification (and/or any App post setup procedures)
Thats ALL!

Lets go into this way more deeply. Lets install django-pagination into my app.
First wee need to check plugin manual. Let's download it from google code HERE.
Unpack It. If you've done it in a period near to writing this article you'll probably see something like this.

We see a list of folders, containing one, like "docs" and a bunch of setup scripts like "setup.py", "setup.cfg" and so on. "Docs/install.txt" says about different install methods.
But you don't have to install it in order things to work properly!
The simplest way is to copy this main app "pagination" (it is usually a first dir in a tree containing "__init__.py" file) into your project or app dir, containing "settings.py".  This way you can see apps or plugins, like they where installed on a python path. 
Setup script usually does this for you, but installing this directory to your system (or virtualenv) python path. And what if you'd like any other version of this django app installed? 
There some other thoughts here also. You may create dir, like "plugins" and copy installed plugins there. But i would not recommend this. You usually need up to 10 plugins per project. Why bother separating them? Bad side here is that we need to do some inside plugin modifications because you're renaming main plugin directory path. So it's possible, but unlikely simpler...

Now we must do plugin setup inside our App. You unfortunately can not escape this. :)
So let's make steps 1 and 2 from "docs/usage.txt" in our plugin dir. Let's add 'pagination', app to INSTALLED_APPS in our settings.py and add a middleware 'pagination.middleware.PaginationMiddleware', string to our MIDDLEWARE_CLASSES.

Let's add request to our TEMPLATE_CONTEXT_PROCESSORS too (if it isn't already there).
It will look something like so:

And thats pretty much all. Now we can add {% load pagination_tags %} somewhere in our template and use  {% autopaginate object_list %} and then {% paginate %} father. (see "docs/usage.txt" of this app for more info)

Now your app is really installed. You can use common from my_app import my_function like in "standard" installation way. Nothing more to do. 

We worked out most common situation with django plugins or redistributable apps. Maybe to some complex python apps, like lets say PIL, this method would be unsuitable, BUT for most common "django app installation" cases it will work like a charm.
Main plus of this way, that you can have all code related to you App/project etc. stored in one place.
This code would not affect main system also. So if you decide to use older version of plugin (for older ver of django for e.g.), maybe you'll remember yourself reading this article :)...

Bad thing here, that if your app has some more dependencies, you have to install them somehow too. Maybe you have to change your new app code, if you choose to copy them to your main dir way. But it's in not so common "complex" cases.

No comments:

Post a Comment