Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango Activity Feed (Feedly Integration?)
    primarykey
    data
    text
    <p>I have built a simple Django photo app. Users can upload photos, follow other users and like photos. To handle relationships amongst the users (following &amp; unfollowing) I use a package called <a href="https://github.com/coleifer/django-relationships" rel="noreferrer">django-relationships</a> by coleifer. It's a great package and very simple to use. </p> <p>Everything works as it should. I currently have a working activity feed. </p> <p>I filter the feed into two sections: Following (all the activities from the users that I follow) &amp; You (all the activities that happened to me). I've posted two pictures below from my iOS app that uses my Django photo app as it's back-end:</p> <p><img src="https://i.stack.imgur.com/fTWt6.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/iNRRH.png" alt="enter image description here"></p> <p>What I would like to do is add aggregation to the Following Feed. As you can see, user alexperri has liked 5 shots. I would like to aggregate all these items into one line. I don't need to add aggregation for the "You" feed since I would like to see each individual action happening to me. However for the Following feed, it makes sense to add aggregation. There are several applications that do aggregation very well. Fashionlista, Pinterest &amp; Instagram do this well. Here is an example from Instagram to show what I am trying to achieve:</p> <p><img src="https://i.stack.imgur.com/Hufoo.png" alt="enter image description here"></p> <p>In the example above, you can see the following feed and that lovetoronto liked 5 photos. I started to play around with the Instagram following feed to see how it works. The Instagram following feed shows a maximum of 35 activity entries and each entry can have a maximum of 5 activities of that action type. "lovetoronto liked 5 photos" is one activity entry and it shows the latest 5 pictures that he liked. Since lovetoronto performed the latest action, he is at the top. </p> <p>I would like to achieve the same setup. </p> <p>Here is my current model setup:</p> <p><strong>models.py</strong></p> <pre><code>from django.db import models from django.contrib.auth.models import User class Photographer(models.Model): user = models.OneToOneField(User, primary_key=True likes = models.ManyToManyField('Photo', through = 'Likes', related_name = 'likedby', blank = True) class Photo(models.Model): photographer = models.ForeignKey(Photographer, related_name = 'shot_owner') created = models.DateTimeField(auto_now_add=True) url = models.CharField(max_length=128) class Likes(models.Model): liked_at = models.DateTimeField(auto_now_add=True, blank=True, null=True) photographer = models.ForeignKey(Photographer, related_name = 'liked_by') photo = models.ForeignKey(Photo, null=True) class Activity(models.Model): actor = models.ForeignKey(Photographer, related_name = 'actor') receiver = models.ForeignKey(Photographer, related_name = 'receiver') action = models.CharField(max_length=12) post = models.ForeignKey(Photo, null=True, blank=True) time = models.DateTimeField(auto_now_add=True) </code></pre> <p>Every time a 'Like' object is created, I create an Activity object as well, the actor being the person who did the action, the receiver being the person who the action was done to, the action (in this case a string, 'liked'), post (the photo) and the time being the creation of the activity object.</p> <p>I use django-tastypie to get and create 'Like' &amp; 'Activity' objects.</p> <p><strong>api.py</strong></p> <pre><code>from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS from tastypie.authentication import BasicAuthentication from tastypie.authorization import DjangoAuthorization, Authorization from photoapp.photodb.models import * from tastypie.serializers import Serializer from relationships.utils import positive_filter from relationships.models import Relationship from relationships.models import RelationshipStatus class LikeResource(ModelResource): user = fields.ForeignKey(BasicUserResource, 'user', full=True) class Meta: queryset = Photographer.objects.all() allowed_methods = ['put'] resource_name = 'like' fields = ['user'] default_format = 'application/json' authorization = Authorization() authentication = BasicAuthentication() serializer = Serializer(formats=['json']) always_return_data = True include_resource_uri = False def hydrate(self, bundle): shot = Photo.objects.all().get(id = bundle.data['photo id']) user = Photographer.objects.all().get(user = bundle.request.user) if(bundle.obj.likes.filter(id = bundle.data['photo id']).exists()): Likes.objects.all().filter(photographer=user).filter(photo=shot).delete() Activity.objects.filter(actor__user = bundle.request.user, post = shot, action = 'liked').delete() else: like = Likes(photographer = user, photo=shot) like.save() user_doing_the_liking = User.objects.get( username=bundle.request.user.username) user = Photographer.objects.all().get(user = bundle.request.user) user_getting_liked = shot.photographer.user photographer_getting_liked = shot.photographer newActivity = Activity() newActivity.actor = user newActivity.receiver = photographer_getting_liked newActivity.action = 'liked' newActivity.post = shot newActivity.save() return bundle class FollowingFeed(ModelResource): actor = fields.ForeignKey(BasicPhotographerResource, 'actor', full=True) receiver = fields.ForeignKey(BasicPhotographerResource, 'receiver', full=True) post = fields.ForeignKey(BasicPostResource, attribute = 'post', full=True, null=True) class Meta: queryset = Activity.objects.all() allowed_methods = ['get'] resource_name = 'following-feed' fields = ['actor', 'receiver', 'action', 'post', 'id', 'time'] default_format = "application/json" authorization = Authorization() authentication = BasicAuthentication() serializer = Serializer(formats=['json']) always_return_data = True include_resource_uri = False def get_object_list(self, request): return super(FollowingFeed, self).get_object_list(request)\ .filter(actor__user__in = request.user.relationships.following())\ .exclude(receiver__user = request.user)\ .exclude(actor__user = request.user).order_by('-time') </code></pre> <p>How can I modify the FollowingFeed resource in such a way that it will aggregate the activity objects? I came across the <a href="https://github.com/tschellenbach/Feedly" rel="noreferrer">Feedly</a> project. How can I use it with my current setup?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload