All Projects → FundersClub → django-papertrail

FundersClub / django-papertrail

Licence: Apache-2.0 License
django-papertrail - An elegant solution for keeping a relational log of events in a Django application.

Programming Languages

python
139335 projects - #7 most used programming language
HTML
75241 projects
Makefile
30231 projects

Projects that are alternatives of or similar to django-papertrail

bash
A collection of small bash utils.
Stars: ✭ 15 (-50%)
Mutual labels:  utility
ToGoZip
Android share/sendTo menu implementation "add2Zip"
Stars: ✭ 44 (+46.67%)
Mutual labels:  utility
aspZip
A classic ASP zip and unzip utility class that uses the native zip support from Windows (XP and above) - no components needed
Stars: ✭ 24 (-20%)
Mutual labels:  utility
tiler
N-dimensional NumPy array tiling and merging with overlapping, padding and tapering
Stars: ✭ 26 (-13.33%)
Mutual labels:  utility
CLIp
CLIp is a clipboard manager for a command line interface written in 100% standard C only. Pipe to it to copy, pipe from it to paste.
Stars: ✭ 12 (-60%)
Mutual labels:  utility
dePAC
seamless Proxy Auto-Config (a.k.a. Web Proxy Auto Discovery) for CLI apps
Stars: ✭ 26 (-13.33%)
Mutual labels:  utility
pynotify
A Python package to send emails like humans.
Stars: ✭ 21 (-30%)
Mutual labels:  utility
pegjs-util
Utility Class for PEG.js
Stars: ✭ 29 (-3.33%)
Mutual labels:  utility
Vutils
Vutils or Vic Utilities is an utility library written in Modern C++ and for Modern C++. It helps your programming go easier, faster, and simpler.
Stars: ✭ 16 (-46.67%)
Mutual labels:  utility
arr-flatten
Recursively flatten an array or arrays. This is the fastest implementation of array flatten.
Stars: ✭ 55 (+83.33%)
Mutual labels:  utility
findlargedir
find all "blackhole" directories with a huge amount of filesystem entries in a flat structure
Stars: ✭ 15 (-50%)
Mutual labels:  utility
asynckit
Minimal async jobs utility library, with streams support
Stars: ✭ 21 (-30%)
Mutual labels:  utility
rearmed rails
A collection of helpful methods and monkey patches for Rails
Stars: ✭ 34 (+13.33%)
Mutual labels:  utility
91porn-utility
91porn comprehensive utility
Stars: ✭ 78 (+160%)
Mutual labels:  utility
bncsutil
The Classic Battle.net™ client library
Stars: ✭ 19 (-36.67%)
Mutual labels:  utility
diskusage
FANTASTIC SPEED utility to find out top largest folders/files on the disk.
Stars: ✭ 64 (+113.33%)
Mutual labels:  utility
ctxutil
utils for Go context
Stars: ✭ 18 (-40%)
Mutual labels:  utility
springboard
Springboard jumpstarts your projects for you. Hello, warp speed.
Stars: ✭ 13 (-56.67%)
Mutual labels:  utility
reminders-menubar
Simple macOS menu bar application to view and interact with reminders. Developed with SwiftUI and using Apple Reminders as a source.
Stars: ✭ 250 (+733.33%)
Mutual labels:  utility
regenny
A reverse engineering tool to interactively reconstruct structures and generate header files
Stars: ✭ 58 (+93.33%)
Mutual labels:  utility

django-papertrail

An elegant solution for keeping a relational log of chronological events in a Django application.

Installation

To install django-papertrail:

$ pip install django-papertrail

To enable django-papertrail in your project you need to add it to INSTALLED_APPS in your projects settings.py file:

INSTALLED_APPS = (
    ...
    'papertrail',
    ...
)

After that, you should migrate:

$ python manage.py migrate

Using it

import papertrail

###########################################################################
# Creating entries
###########################################################################

# Basic usage. Timestamp defaults to now
papertrail.log('cache-flushed', 'Cache was flushed!')

# Optional data
papertrail.log(
    'periodic-cleanup-ran',
    'Periodic cleanup task executed.',
    data={
        'success': True,
        'cleaned_objects': 100,
    }
)

# Optional targets
papertrail.log(
    'user-logged-in',
    u'{} logged in'.format(request.user.get_full_name()),
    targets={
        'user': request.user,
    }
)

# Optional timestamp
papertrail.log(
    'user-joined',
    'User joined site',
    targets={
        'user': request.user,
    },
    timestamp=request.user.date_joined,
)

# Multiple targets
user1 = User.objects.get(...)
user2 = User.objects.get(...)
papertrail.log(
    'user-followed',
    'User followed another user',
    targets={
        'follower': user1,
        'following': user2,
    },
)

###########################################################################
# Querying the papertrail
###########################################################################

# Gettying all papertrail entries that points to user1, without taking
# into account the target relationship name
qs = papertrail.related_to(user)
entry = qs.first()
print '[{}] {} ({}) - {}'.format(
    entry.timestamp, entry.type, entry.message, entry.data
)

# Get all entry that points to both users
# (Will only return entries that have both user1 and user2 in their
#  targets)
qs = papertrail.related_to(user1, user2)

# Query specific relationships, such as user1 following user2
qs = papertrail.related_to(follower=user1, following=user2)

# Filtering entry by a specific type (or any Django ORM filter)
qs = papertrail.filter(type='user-followed')

# And chaining
qs = papertrail.filter(type='user-followed').related_to(follower=user1)

# Get all the users that have followed a specific user (user1). This might
# look a bit confusing at first, but can be very useful.
# The objects_represented filter allows filtering a given queryset to contain
# only elements that have a specific papertrail entry pointing at them.
all_users = get_user_model().objects.all()
users_who_followed_user1 = (papertrail
    # Narrow down to only user-followed entries that followed user1
    .filter(type='user-followed')
    .related_to(following=user1)
    # Return a User queryset that only has the users for which we have a
    # user-followed entry that has a followed target pointing at them
    .objects_represented(all_users, 'followed')
)

# objects_not_represented does the same, but returns a queryset that
# excludes any object that has a papertrail entry pointing at it:
# Get all users who never logged in
users_who_never_logged_in = (papertrail
    .filter(type='user-logged-in')
    .objects_not_represented(all_users, 'user')
)

Admin integration

django-papertrail provides a Django admin integration to both view entries (simple Django admin Entry list, usually available under /admin/papertrail/entry/) as well as a more advanced intergration for objects you want to keep track of.

The advanced integration provides two useful functionalities:

  1. Change tracking - whenever an object for which the integration is enabled is added/edited/deleted, a papertrail entry will be created
  2. A convenient link to view all papertrail entries pointing to the object being viewed as well as an integrated papertrail viewer:

To enable the integration, your ModelAdmin class needs to inherit from AdminEventLoggerMixin:

from papertrail.admin import AdminEventLoggerMixin

class MyObjectAdmin(AdminEventLoggerMixin, admin.ModelAdmin):
    pass

    # The admin papertrail viewer can have filters:
    papertrail_type_filters = {
        'Login events': (
            'user-logged-in',
            'user-logged-out',
        ),
        'Social events': (
            'user-followed',
            'user-unfollowed',
        ),
    }

A viewer with filters would look like this:

Maintained by Eran Rundstein @eranrund

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].