Django: user collection, object activity stream

I am using the django-activity-stream module to collect user activity. But when one user (user1) follows another (user2).

I need to get the activity stream from the next user (user2) and combine all sort operations by date and time (see code below).

And as the activity list grows, I think I will run into performance and optimization issues. I believe that someone has already solved such problems.

Any ideas and tips on how to improve work efficiency?

def build_activity(raw_activity):
    activity = []
    for item in raw_activity:
        action_object = get_action_object(item)
        activity.append({
            'user': User.objects.get(pk=int(item.actor_object_id)),
            'verb': item.verb,
            'action_object': action_object[1],
            'type': action_object[0],
            'timestamp': timesince(item.timestamp),
            'datetime': item.timestamp,
        })
    return activity


def activity_stream(user):
    from actstream.models import actor_stream
    raw_activity = actor_stream(user)
    activity = build_activity(raw_activity)
    for following in Follow.objects.filter(user=user):
        stream = actor_stream(following.target_user)
        activity += build_activity(stream)
    return sorted(activity, key=lambda item:item['datetime'], reverse=True)

Thank,

The sultan

+3
source share
3 answers

, , . , .

+2

Premature optimization is the root of all evil.

But if I was going to optimize this, I could create another thread, and the timestamps for the actions are set by the timestamp action_object ... :)

+1
source

All Articles