Beispiel #1
0
 def subscribe_get(self, request):
     """
         Return the newsletters that can subscribe
     """
     newsletters = Newsletter.objects.filter(subscribed=F('target'))
     serialized = NewsletterSerializer(newsletters, many=True)
     return Response(status=status.HTTP_200_OK, data=serialized.data)
Beispiel #2
0
 def vote_get(self, request):
     """
         Return a list the newsletters that can vote
     """
     newsletters = self.get_queryset().filter(subscribed__lt=F('target'))
     serialized = NewsletterSerializer(newsletters, many=True)
     return Response(status=status.HTTP_200_OK, data=serialized.data)
Beispiel #3
0
 def author(self, request, pk=None):
     """
         Return the newsletter of a author.
     """
     user = CustomUser.objects.get(id=pk)
     author = user.author_newsletter.all()
     serialized = NewsletterSerializer(author, many=True)
     return Response(status=status.HTTP_200_OK, data=serialized.data)
Beispiel #4
0
 def subscribed(self, request, pk=None):
     """
         Return the newsletter that subscribed a user
     """
     user = CustomUser.objects.get(id=pk)
     newsletter = Newsletter.objects.filter(
         Q(users=user) & Q(subscribed=F('target')))
     serialized = NewsletterSerializer(newsletter, many=True)
     return Response(status=status.HTTP_200_OK, data=serialized.data)
 def newsletters_subscribe(self, request, slug=None):
     """
         Return the newsletters that can subscribe of a tag for the slug.
     """
     tags = Tag.objects.get(
         slug=slug)  # Regresa un tag en particular por el slug
     newsletters = Newsletter.objects.filter(
         Q(tag__slug=tags.slug) & Q(subscribed=F('target'))
     )  # filtramos los boletines por el tag__slug y los bolotines que el subscribe sea igual al target
     serialized = NewsletterSerializer(newsletters, many=True)
     return Response(status=status.HTTP_200_OK, data=serialized.data)
Beispiel #6
0
 def vote(self, request, pk=None):
     """
         Add the user that vote for a newsletter
     """
     newsletter = Newsletter.objects.get(id=pk)
     user_id = request.data.get('user')
     user = CustomUser.objects.get(id=user_id)
     if not newsletter.users.filter(id=user.id).exists(
     ) and newsletter.subscribed < newsletter.target:
         newsletter.users.add(user)
         if (newsletter.target - newsletter.subscribed) == 1:
             serialized_user = UserSerializer(newsletter.users.all(),
                                              many=True)
             serialized_newsletter = NewsletterSerializer(newsletter)
             send_email_newsletter.apply_async(
                 args=[serialized_user.data, serialized_newsletter.data])
         newsletter.subscribed += 1
     elif newsletter.users.filter(id=user.id).exists():
         newsletter.users.remove(user)
         if not newsletter.subscribed == newsletter.target:
             newsletter.subscribed -= 1
     newsletter.save()
     return Response(status=status.HTTP_200_OK)