Skip to content

richardARPANET/django-simple-activity

Repository files navigation

NOTICE: If you're reading this on GitHub.com please be aware this is a mirror of the primary remote located at https://code.richard.do/explore/projects. Please direct issues and pull requests there.

django-simple-activity

PyPI Build Status

Simple, generic, activity streams from the actions on your site.

Supports Django versions 1.10.x, 1.11.x, 2.0 and above.

Installation

  1. Install using pip...

    pip install django-simple-activity

  2. Add 'activity' to your INSTALLED_APPS setting.

    INSTALLED_APPS = (
        ...
        'activity',
    )
  3. Ensure django.contrib.contenttypes is above django.contrib.auth in your installed apps (why? read here).

Example Usage

Creating Activity Actions

from activity.logic import add_action, get_action_text
user = User.objects.get(username='bob')

# actor and verb example
action = add_action(actor=user, verb='joined the website!')
print(get_action_text(action)) -> 'bob joined the website!'

# actor, verb and action
another_user = User.objects.get(username='jessica')
action = add_action(
    actor=user,
    verb='made friends with',
    action=another_user,
    timestamp=timezone.now() - timedelta(days=2)
)
print(get_action_text(action)) -> 'bob made friends with jessica 2 days ago'

Further examples are in the tests.

Retrieving Activity Actions

Get all Activity Action records:

Action.objects.all()

Get all public Activity Action records:

Action.objects.public()

Get all private Activity Action records, where the actor is a given user

Action.objects.private(actor=user)  # user being a Django User model instance

Here's an example use of django-simple-activity which uses django signals to trigger off the creation of an activity action when a new user registers on the site.

from django.db.models.signals import pre_save
from django.dispatch import receiver
from activity.logic import add_action
from . models import UserProfile


@receiver(pre_save, sender=UserProfile)
def new_user_activity(sender, **kwargs):
    profile = kwargs.get('instance')
    # if no profile.id yet, then this is a new user registering
    if not profile.id:
        add_action(actor=profile.user, verb='joined the website!')

Running the unit tests

To test dependency installation and run the tests:

pip install tox
tox