Пример #1
0
    def get(self, request, *args, **kwargs):
        user = request.user

        cached_suggestions = cache_client.get(user.id)

        if not cached_suggestions:
            # Do not run task if user not following anything
            if not user.moviefollow_set.exists(
            ) or user.artistfollow_set.exists() or user.genrefollow_set.exists(
            ):
                return Response(
                    {'message': 'You are not following any movie/star/genre'},
                    status=status.HTTP_200_OK)

            task_id = process_suggest_for_user.apply_async(
                kwargs={'user_id': user.id})
            CeleryResult.objects.create(user_id=user.id,
                                        task_id=task_id,
                                        status=CeleryResult.PENDING)
            return Response({'message': 'Your request is still in progress'},
                            status=status.HTTP_200_OK)

        qs = Movie.objects.filter(dataset_id__in=cached_suggestions)
        qs = sorted(qs, key=lambda x: cached_suggestions.index(x.dataset_id))
        return Response(UserSuggestionSerializer(qs, many=True).data,
                        status=status.HTTP_200_OK)
Пример #2
0
 def unfollow(self, request, pk):
     instance = GenreFollow.objects.filter(user=request.user,
                                           genre_id=pk).first()
     if not instance:
         raise ValidationError('Object not found')
     task_id = process_suggest_for_user.apply_async(
         kwargs={'user_id': request.user.id})
     CeleryResult.objects.create(user_id=request.user.id,
                                 task_id=task_id,
                                 status=CeleryResult.PENDING)
     return Response(status=status.HTTP_204_NO_CONTENT)
Пример #3
0
 def follow(self, request, pk):
     instance = Movie.objects.filter(id=pk).first()
     if instance:
         MovieFollow.objects.get_or_create(user=request.user,
                                           movie=instance)
         task_id = process_suggest_for_user.apply_async(
             kwargs={'user_id': request.user.id})
         CeleryResult.objects.create(user_id=request.user.id,
                                     task_id=task_id,
                                     status=CeleryResult.PENDING)
         return Response(status=status.HTTP_204_NO_CONTENT)
     else:
         raise ValidationError('Movie not found')
Пример #4
0
 def save(self, *args, **kwargs):
     super().save(*args, **kwargs)
     from movie.tasks import process_suggest_for_user
     process_suggest_for_user.apply_async(kwargs={'user_id': self.user.id})