Пример #1
0
def send_create_article_notification_to_followers(sender, instance, created,
                                                  **kwargs):
    followers = instance.author.profile.followers()

    for follower in followers:
        if follower.user.is_subscribed:
            data = {
                'username':
                follower.user.username,
                'article_title':
                instance.title,
                'author':
                instance.author.username,
                'unsubscribe_url':
                'http://localhost:8000' + reverse('notifications:subscribe')
            }
            send_email(
                template='article_created.html',
                data=data,
                to_email=follower.user.email,
                subject='You have a new notification',
            )
        notify.send(
            instance,
            verb=Verbs.ARTICLE_CREATION,
            recipient=follower.user,
            description="An article by an author you follow has been created")
Пример #2
0
    def post(self, request, slug):
        try:
            article = Article.objects.get(slug=slug, published=True)
        except Article.DoesNotExist:
            return Response({"errors": "The article does not exist."},
                            status=status.HTTP_404_NOT_FOUND)
        self.check_object_permissions(self.request, article)

        data = request.data
        data['article'] = article
        data['reporter'] = request.user

        serializer = self.serializer_class(data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        email_data = {
            'username': request.user.username,
            'article_slug': article.slug,
            'author': article.author.username,
            'report_category': serializer.data['type']
        }

        send_email(
            template='acknowledgement_email.html',
            data=email_data,
            to_email=request.user.email,
            subject='Report received',
        )

        message = 'Your report has been received. You will receive a confirmation email shortly.'

        return Response({'message': message}, status=status.HTTP_200_OK)
Пример #3
0
    def put(self, request, slug):
        decision = request.data.get('decision')

        if decision not in Violation.DECISION_TYPES.keys():
            return self.get_error_response(
                "This violation decision '%s' is not valid." % decision)

        if not Article.objects.filter(slug=slug).exists():
            return Response({'error': 'The article does not exist.'},
                            status=status.HTTP_404_NOT_FOUND)

        violations = Violation.objects.filter(article__slug=slug)
        # if the specified article has no violations we take no action
        if violations.count() < 1:
            return self.get_error_response(
                'This article does not have any pending violation reports.')

        if decision == 'approve':
            # approve all violation reports
            decision_status = Violation.approved
            article = Article.objects.get(slug=slug)
            email_data = {
                'username': request.user.username,
                'article': article.title
            }
            to_email = article.author.email
            template = 'confirmation_email.html'
            # send email to email owner
            send_email(data=email_data,
                       template=template,
                       subject='Violation attention',
                       to_email=to_email)
            # soft-delete the article
            article.delete()

        elif decision == 'reject':
            # reject all violation reports
            decision_status = Violation.rejected
        for violation in violations:
            # update violation status accordingly
            violation.status = decision_status
            violation.save()
        return Response(
            {'message': 'You have %s this violation.' % decision_status},
            status=status.HTTP_200_OK)
Пример #4
0
 def post(self, request):
     user = request.user
     user.is_subscribed = True
     user.save()
     data = {'username': request.user.username}
     send_email(
         template='email_subscribe.html',
         data=data,
         to_email=request.user.email,
         subject='Email subscription activated',
     )
     res_data = {
         "message": {
             "subscription_status":
             request.user.is_subscribed,
             "message":
             "You have successfully subscribed to our notifications.",
         }
     }
     return Response(res_data, status=status.HTTP_200_OK)
Пример #5
0
 def delete(self, request):
     user = request.user
     user.is_subscribed = False
     user.save()
     data = {
         'username': request.user.username,
     }
     send_email(
         template='unsubscribe_email.html',
         data=data,
         to_email=request.user.email,
         subject='Email subscription deactivated',
     )
     res_data = {
         "message": {
             "subscription_status":
             request.user.is_subscribed,
             "message":
             "You have successfully unsubscribed from our notifications.",
         }
     }
     return Response(res_data, status=status.HTTP_200_OK)