0

While building my API, I didn't think about the logic behind notifications. Please, How do I solve a problem like this on a potentially big project? let us assume I have the code below:

model.py

class Showcase(models.Model):

    title = models.CharField(max_length=50)
    description = models.TextField(null=True)
    skill_type = models.ForeignKey(Skill, on_delete=models.CASCADE)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name="Showcases")
    content = models.TextField(null=True)
    created_on = models.DateTimeField(auto_now=True)
    updated_on = models.DateTimeField(auto_now_add=True)
    voters = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="upvotes")
    slug = models.SlugField(max_length=255, unique=True)

serializer.py

class ShowcaseSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    created_on = serializers.SerializerMethodField(read_only=True)
    likes_count = serializers.SerializerMethodField(read_only=True)
    user_has_voted = serializers.SerializerMethodField(read_only=True)
    slug = serializers.SlugField(read_only=True)
    comment_count = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Showcase
        exclude = ['voters', 'updated_on']

    def get_created_on(self, instance):
        return instance.created_on.strftime("%d %B %Y")

    def get_likes_count(self, instance):
        return instance.voters.count()

    def get_user_has_voted(self, instance):
        request = self.context.get("request")
        return instance.voters.filter(pk=request.user.pk).exists()

    def get_comment_count(self, instance):
        return instance.comments.count()

view.py

# SHOWCASE APIView
class showcaseCreateViewSet(generics.ListCreateAPIView):
    '''
    Create showcases view. user must be logged in to do this
    '''
    queryset = Showcase.objects.all()
    serializer_class = ShowcaseSerializer
    permission_classes = [IsAuthenticatedOrReadOnly]

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

class ShowcaseLikeAPIView(APIView):
    '''
    Can like(post) and unlike(delete) the showcases, must be 
    authenticated to do this
    '''
    serializer_class = ShowcaseDetaiedSerializer
    permission_classes = [IsAuthenticated]

    def delete(self, request, slug):
        showcase = get_object_or_404(Showcase, slug=slug)
        user = self.request.user

        showcase.voters.remove(user)
        showcase.save()

        serializer_context = {"request": request}
        serializer = self.serializer_class(showcase, context=serializer_context)

        return Response(serializer.data, status=status.HTTP_200_OK)

    def post(self, request, slug):
        showcase = get_object_or_404(Showcase, slug=slug)
        user = self.request.user

        showcase.voters.add(user)
        showcase.save()

        serializer_context = {"request": request}
        serializer = self.serializer_class(showcase, context=serializer_context)

        return Response(serializer.data, status=status.HTTP_200_OK)

with the code above that likes a user's post(Showcase), I am trying to figure out how the user can get notified when a user likes his post. and the things I am considering for the notification, the user that gets notified, the user that makes the action, the post(in this case showcase) that is getting the action, the message that gets passed, a boolean field for if it has been read. The method that comes to my head is creating an app for that but after nothing else. please, how do I solve a problem like this, or what is the best/recommended way to solve this?

Opeyemi Odedeyi
  • 766
  • 1
  • 10
  • 38
  • this [question](https://stackoverflow.com/questions/8603469/how-to-use-django-notification-to-inform-a-user-when-somebody-comments-on-their) seems to fit for your implementation – Linh Nguyen Nov 26 '19 at 08:24
  • I suggest you use the model signals of django https://docs.djangoproject.com/en/2.2/ref/signals/. It has a post_save where you can trigger notifications after the user likes a certain post. – elomat Nov 26 '19 at 09:00
  • Do you want to make it Real-time ? – Rebai Ahmed Nov 26 '19 at 09:07
  • @AhmedRebai yeah I want to make it real-time. I am guessing i would have to use django channels. – Opeyemi Odedeyi Nov 28 '19 at 16:25

0 Answers0