Avoiding duplicate actions in the activity stream

Sometimes, your users might perform an action multiple times. They might click several times on the LIKE or UNLIKE buttons or perform the same action multiple times in a short period of time. This will easily lead to storing and displaying duplicate actions. To avoid this, we will improve the create_action() function to skip obvious duplicated actions.

Edit the utils.py file of the actions application, as follows:

import datetimefrom django.utils import timezonefrom django.contrib.contenttypes.models import ContentTypefrom .models import Actiondef create_action(user, verb, target=None):    # check for any similar action made in the last minute    now = timezone.now() last_minute = now - datetime.timedelta(seconds=60) ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.