Esempio n. 1
0
    def revoke(self, request, pk):  # pylint: disable=unused-argument
        """
        Revoke users by email from codes within the Coupon.
        """
        coupon = self.get_object()
        self._validate_coupon_availablity(
            coupon, 'Coupon is not available for code revoke')
        subject = request.data.pop('template_subject', '')
        greeting = request.data.pop('template_greeting', '')
        closing = request.data.pop('template_closing', '')
        self._validate_email_fields(subject, greeting, closing)
        serializer = CouponCodeRevokeSerializer(
            data=request.data.get('assignments'),
            many=True,
            context={
                'coupon': coupon,
                'subject': subject,
                'greeting': greeting,
                'closing': closing
            })
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
    def revoke(self, request, pk):  # pylint: disable=unused-argument
        """
        Revoke users by email from codes within the Coupon.
        """
        coupon = self.get_object()
        email_template = request.data.pop('template', None)
        serializer = CouponCodeRevokeSerializer(
            data=request.data.get('assignments'),
            many=True,
            context={'coupon': coupon, 'template': email_template}
        )
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 3
0
    def revoke(self, request, pk):  # pylint: disable=unused-argument
        """
        Revoke users by email from codes within the Coupon.
        """
        coupon = self.get_object()
        self._validate_coupon_availablity(
            coupon, 'Coupon is not available for code revoke')
        subject = request.data.pop('template_subject', '')
        greeting = request.data.pop('template_greeting', '')
        closing = request.data.pop('template_closing', '')
        template_id = request.data.pop('template_id', None)
        template = OfferAssignmentEmailTemplates.get_template(template_id)
        enterprise_customer = coupon.attr.enterprise_customer_uuid
        self._validate_email_fields(subject, greeting, closing)
        self._validate_assignments_data(request.data.get('assignments'))

        do_not_email = request.data.get('do_not_email')
        assignments = [
            dict(**assignment, do_not_email=do_not_email)
            for assignment in request.data.get('assignments')
        ]

        serializer = CouponCodeRevokeSerializer(data=assignments,
                                                many=True,
                                                context={
                                                    'coupon': coupon,
                                                    'subject': subject,
                                                    'greeting': greeting,
                                                    'closing': closing,
                                                })
        if serializer.is_valid():
            serializer.save()
            if not do_not_email:
                # Create a record of the email sent
                self._create_offer_assignment_email_sent_record(
                    enterprise_customer, REVOKE, template)

            # unsubscribe user from receiving nudge emails
            CodeAssignmentNudgeEmails.unsubscribe_from_nudging(
                map(lambda assignment: assignment['code'], assignments),
                map(lambda assignment: assignment['email'], assignments))

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

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)