Code Monkey home page Code Monkey logo

django-object-tools's Introduction

Django Object Tools

Django app enabling painless creation of additional admin object tools.

image

image

image

image

Contents

Requirements

  1. Python 2.7, 3.5-3.7
  2. Django 1.11, 2.0, 2.1

Installation

  1. Install or add django-object-tools to your python path.
  2. 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.
  3. 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 *
    from  django.conf.urls import url
    import object_tools
    
    urlpatterns = [
        path('object-tools/', object_tools.tools.urls)
    ]
  4. Obviously Django Admin itself needs to be installed, as described here.
  5. Remember to run migrate 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
from django.contrib.admin.sites import site
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()
        modeladmin = site._registry.get(self.model)
        response = delete_selected(modeladmin, request, queryset)
        if response:
            return response
        else:
            return modeladmin.changelist_view(request)

object_tools.tools.register(Delete)

Let's go through that line by line:

  1. object_tools behaves similarly to Django's admin allowing you to explicitly register tools, see line 17. It also provides the ObjectTool base class.
  2. import delete_selected method provided by Django. This method will do all the heavy lifting.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.

image

Clicking on the Delete all tool fires off 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.

django-object-tools's People

Contributors

bashu avatar cblignaut avatar dhruvbaldawa avatar djmaze avatar felixxm avatar gdub avatar hedleyroos avatar k8n avatar phala avatar qoda avatar rizziepit avatar ron8mcr avatar slafs avatar trbs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-object-tools's Issues

Single object tool

Hi, it would be cool to have single object tool as well. By single object I mean in admin change view.

Thanks in advance.

autodiscover in Django>=1.7

The autodisover mechanism isn't compatible with the new Django apps. Also default config should probably do autodiscovering automatically (like in admin).

self.modeladmin returns null

When following your tutorial, I keep getting the error when executing this line:

return self.modeladmin.changelist_view(request)

The error I get is:

AttributeError: 'NoneType' object has no attribute 'changelist_view'

But when I used the code below, everything worked fine:

from django.contrib.admin import site
...
return site._registry(self.model).changelist_view(request)

It seems like the self.modeladmin is not getting populated correctly. My workaround appears to suffice for now but I just wanted to make you aware of what I found.

File Uploads Not Working

The form is not instantiated with request.FILES resulting in issues in tools relying on file uploads.

TemplateSyntaxError

'admin_static' is not a valid tag library: Template library admin_static not found, tried django.templatetags.admin_static,object_tools.templatetags.admin_static,django.contrib.admin.templatetags.admin_static,django.contrib.markup.templatetags.admin_static,django.contrib.humanize.templatetags.admin_static,shop.templatetags.admin_static,jsonfield.templatetags.admin_static,bootstrapform.templatetags.admin_static

where can I find 'admin_static'?

Python 3 issues

Hi,

Installing collected packages: django-object-tools
  Running setup.py install for django-object-tools

      File "/home/repos/.virtualenvs/c2e-dashboard/lib/python3.4/site-packages/object_tools/management/__init__.py", line 59
        print "Adding permission '%s'" % p
                                     ^
    SyntaxError: invalid syntax

      File "/home/repos/.virtualenvs/c2e-dashboard/lib/python3.4/site-packages/object_tools/tests/__init__.py", line 43
        except ImproperlyConfigured, e:
                                   ^
    SyntaxError: invalid syntax

syntax in python3 are print() and except MyException as e

Update for Django 1.7

The template is out of date, breadcrumb is different now.

Would be better if the list items could be injected into the template somehow rather than overwriting the entire template??

Only add tools & permission to selected models

Often the case is, one object tools only makes sense to certain models. Permissions gets messy quickly if I have several tools. Is it possible to only add tools & permissions to selected models?

Update for Django 2.0

Looks like a few imports have changed around that affect django-object-tools, here's one example:

…
  File "/path/to/project/.direnv/python-3.6.3/lib/python3.6/site-packages/object_tools/__init__.py", line 4, in <module>
    from object_tools.options import ObjectTool
  File "/path/to/project/.direnv/python-3.6.3/lib/python3.6/site-packages/object_tools/options.py", line 8, in <module>
    from django.core.urlresolvers import reverse
ModuleNotFoundError: No module named 'django.core.urlresolvers'

Django 1.10.8 not working

After ddoing all the instalation steps I run into this problem:

Traceback (most recent call last):
File "/home/bob/Documents/VirtualEnvs/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(args, **kwargs)
File "/home/bob/Documents/VirtualEnvs/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/home/bob/Documents/VirtualEnvs/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(
_exception)
File "/home/bob/Documents/VirtualEnvs/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/bob/Documents/VirtualEnvs/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/init.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/bob/Documents/VirtualEnvs/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/bob/Documents/VirtualEnvs/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
ImportError: No module named export

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.