[Fixed]-Django Activity Feed (Feedly Integration?)

2👍

✅

I don’t think you want to do any database level aggregation, because you presumably want to show the individual details as well as the counts e.g. “X like 5 photos” and show the 5 photos. Aggregation, by definition, will exclude the individual data.

Instead, you should do the grouping and sorting in Python code (or Javascript since I think you’re using an HTTP API, but I would prefer a server side API that had things already organised).

itertools.groupby might help. I think you need to group by (user and action), and then sort by the timestamp of the first item in each group, so that you might see “Joe liked 5 photos”, “Anne posted 2 photos”, “Joe posted a photo”, “Claire liked 3 photos” etc.

đŸ‘€spookylukey

1👍

In your feed resource, you are already overriding get_object_list, I would suggest to change the logic to perform a raw query for aggregation logic.

def get_object_list(self, request):
        query = "Your aggregation query logic here"
        feed_model = self._meta.object_class
        return feed_model.objects.raw(query)

This should do what is required. However, you need to think of your query logic. Let me know if you face any other problem.

Thanks!

đŸ‘€Rahul Tanwani

0👍

I think the best way to do this would be to modify the Activity table to store grouped activities. When a new action happens, check for existing ones of the same type and either edit the record to make it ‘grouped’, or add a new record. You could add ManyToMany relationships to all potential table that contain related records, or just store the data in a json field that contains enough information to render the activity in the feed without making queries to other table objects.

If it’s too resource intensive, you can queue the adding/editing of new activities. You’re probably better off keeping the activity table just a straight feed that doesn’t require any processing when rendered. It’s not the easiest solution to implement, but I think that in the long run it makes sense.

0👍

Set the aggregation time, for example do you want to aggregate all “likes” within a 10 minute period or for the past 24 hours?

Then you can filter your object by this time frame.

Then you can apply grouping using the .values(‘model__field’) method. Django generates sql that contains ‘GROUP BY’

And then finally add an aggregation limit so that when the number of likes is over this limit you display the aggregated view rather than the single activity view.

Example below (pseudo, not actual):

if (activity.object.filter(time__gt=yourlowertime, time__lt=youruppertime).values(‘activity__action').count() > aggregation_limit) :
     # show aggregated view
else:
     # show normal view
đŸ‘€HungryArthur

Leave a comment