Login

Custom management command to list recent admin actions

Author:
pbx
Posted:
July 16, 2009
Language:
Python
Version:
1.0
Tags:
admin commands
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. Send information mails to related staff members. by sleytr 7 years, 8 months ago
  2. Command Line Script Launcher by dakrauth 6 years, 7 months ago
  3. Management command which helps to find temlate files by skyjur 4 years, 1 month ago
  4. Using manager methods by ubernostrum 8 years ago
  5. SQLite database vacuum script by pbx 7 years, 9 months ago

Comments

Please login first before commenting.