Login

Custom management command to list recent admin actions

Author:
pbx
Posted:
July 16, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

On a busy site it can be nice to have a summary of admin activity. Running this command (I call it "adminlog") generates output like this:

2009-07-10 18:06:19: pbx changed flat page: "/yay/ -- Let's All Say Yay"

By default it shows the last five actions; pass it a numerical arg to show more or fewer.

Run this as a cron job and you can follow a site's admin-side activity without even logging in!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django.contrib.admin.models import LogEntry
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    help = "Display recent actions performed via the admin app"
    args = "[number of actions to display]"
    requires_model_validation = False

    def handle(self, how_many=5, *args, **options):
        last_actions = LogEntry.objects.all()[:how_many]
        for action in last_actions:
            timestamp = action.action_time.strftime("%Y-%m-%d %H:%M:%S")
            print '%s: %s %s %s: "%s"' % (timestamp, action.user, verb(action), action.content_type, action.object_repr)


def verb(action):
    """Return English label for a given action code"""
    if action.is_deletion():
        return "deleted"
    elif action.is_addition():
        return "added"
    elif action.is_change():
        return "changed"

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.