Django app enabling painless creation of additional admin object tools.
Project description
Django Object Tools
Django app enabling painless creation of additional admin object tools.
This packages is part of the larger Jmbo project.
Installation
Install or add django-object-tools to your python path.
Add object_tools to your INSTALLED_APPS setting. django-object-tools overrides certain admin templates so you have to add it before django.contrib.admin.
Call object tool’s autodiscover method. This works in a similar fashion as Django’s admin; discovering which tools to render in admin. You can do this in any module that is called during initialization but we recommend doing it in urls.py, as illustrated in the next point.
Hook up URLConf. Do this by pointing a given URL at the tools.urls method. In this example, we register the default Tools instance object_tools.tools at the URL /object-tools/:
# urls.py from django.conf.urls.defaults import * import object_tools object_tools.autodiscover() urlpatterns = patterns('', (r'^object-tools/', include(object_tools.tools.urls)), )
Obviously Django Admin itself needs to be installed, as described here.
Remember to run syncdb whenever you install new tools to setup permissions.
Usage
django-object-tools itself doesn’t do much in terms of providing useful tools. Its purpose is to simplify creation and integration of custom tools delivered by other Django applications. To that end it takes care of the messy details like permissions and admin template integration so you can focus on the fun stuff.
As an example lets create a tool allowing you to delete all objects. Yes this is a bit convoluted but it’s a good toy example for illustration purposes. Have a look at django-export and django-order for examples of real world tools leveraging django-object-tools.
Firstly create a Django app folder structure as per usual, with the root directory named delete, including a file called tools.py. It should look as follows:
delete/ __init__.py tools.py
Edit tools.py to look like this:
from django.contrib.admin.actions import delete_selected import object_tools class Delete(object_tools.ObjectTool): name = 'delete' label = 'Delete all' def view(self, request, extra_context=None): queryset = self.model.objects.all() response = delete_selected(self.modeladmin, request, queryset) if response: return response else: return self.modeladmin.changelist_view(request) object_tools.tools.register(Delete)
Let’s go through that line by line:
object_tools behaves similarly to Django’s admin allowing you to explicitly register tools, see line 17. It also provides the ObjectTool base class.
import delete_selected method provided by Django. This method will do all the heavy lifting.
Create a tool class inheriting from object_tools.ObjectTool. All object tools have to inherit from object_tools.ObjectTool. ObjectTool provides various methods to simplify creation of tools. See object_tools.options.py for more details.
Set tool name to delete. This has to be a unique name identifying the tool. This is used to uniquely identify the tool internally and for instance to setup permissions.
Set label to Delete all. The label is displayed within admin and not the name, thus allowing you to specify a more verbose, user friendly label.
Implement view method. This is the brains of your tool. The view method is called when the user executes your tool, so your tool logic would go here. This can be any view like code, as long as it returns an HttpResponse object. In this case we wrap Django’s built-in delete_selected to provide the form, logic and template code to perform the actual delete.
Register the tool with object_tools, thus enabling its display in admin.
To enable the tool add delete to your INSTALLED_APPS setting.
Now when you navigate to the change list view of any model you’ll find the delete all object tool in the upper right hand corner.
Clicking on the Delete all tool fires of the view and proceeds with deleting objects as per usual.
Note: django-object-tools adds per tool permissions to the built-in set of default Django permissions. So in this example only superusers or users who have the the Can delete <model> permission will be able to see and use the tool. If you can’t see or use a particular tool make sure the authenticated user has the required permissions to do so.
Changelog
0.0.3 (2011-09-15)
Correctly resolve title.
0.0.1 (2011-07-22)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file django-object-tools-0.0.3.tar.gz
.
File metadata
- Download URL: django-object-tools-0.0.3.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30f87155ce0c35f5f3e69e6f5e1757af6c9e82c9b10ce8d11da4bb4eff297b86 |
|
MD5 | 504280fcda4b8f61df845d47a133bed0 |
|
BLAKE2b-256 | 36cf30bff726042c45d6f581fb6763f8d689de75c0cb4eda2b2c7f6195eb3163 |
File details
Details for the file django_object_tools-0.0.3-py2.7.egg
.
File metadata
- Download URL: django_object_tools-0.0.3-py2.7.egg
- Upload date:
- Size: 26.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67a61a388491a7589f3409aaf15b3a7d1defd71e8d2d50daeabbaafc60950b5c |
|
MD5 | ea6ed34d82bd2fdd3a6bd385063a8b2d |
|
BLAKE2b-256 | e24031a7e9cf3bceec1b288b15811fbe826fb907ce2cd1c90daf0321404b7c27 |
File details
Details for the file django_object_tools-0.0.3-py2.6.egg
.
File metadata
- Download URL: django_object_tools-0.0.3-py2.6.egg
- Upload date:
- Size: 26.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5111f1493080a75255c7bf6ba28b80e00cf002880522b89f5789e085e7cc746a |
|
MD5 | 6102da7e451545f196a70c0569df6a77 |
|
BLAKE2b-256 | 5c69838f6ef2b84a71d76c89c1b7aa350f54a77dd989e41458e263b977723520 |