Adding user actions to the activity stream

It's time to add some actions to our views to build the activity stream for our users. We will store an action for each of the following interactions:

  • A user bookmarks an image
  • A user likes an image
  • A user creates an account
  • A user starts following another user

Edit the views.py file of the images application and add the following import:

from actions.utils import create_action

In the image_create view, add create_action() after saving the image, like this:

new_item.save()
create_action(request.user, 'bookmarked image', new_item)

In the image_like view, add create_action() after adding the user to the users_like relationship, as follows:

image.users_like.add(request.user)
create_action(request.user, 'likes', image)

Now, edit the views.py file of the account application and add the following import:

from actions.utils import create_action

In the register view, add create_action() after creating the Profile object, as follows:

Profile.objects.create(user=new_user)
create_action(new_user, 'has created an account')

In the user_follow view, add create_action():

Contact.objects.get_or_create(user_from=request.user,
user_to=user)
create_action(request.user, 'is following', user)

As you can see in the preceding code, thanks to our Action model and our helper function, it's very easy to save new actions to the activity stream.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset