Esempio n. 1
0
    def remind(self, request, pk):  # pylint: disable=unused-argument
        """
        Remind users of pending offer assignments by email.
        """
        coupon = self.get_object()
        self._validate_coupon_availablity(coupon, 'Coupon is not available for code remind')
        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)
        if request.data.get('assignments'):
            assignments = request.data.get('assignments')
        else:
            # If no assignment is passed, send reminder to all assignments associated with the coupon.
            vouchers = coupon.attr.coupon_vouchers.vouchers.all()
            code_filter = request.data.get('code_filter')

            if not code_filter:
                raise serializers.ValidationError('code_filter must be specified')

            if code_filter == VOUCHER_NOT_REDEEMED:
                assignment_usages = self._get_not_redeemed_usages(vouchers)
            elif code_filter == VOUCHER_PARTIAL_REDEEMED:
                assignment_usages = self._get_partial_redeemed_usages(vouchers)
            else:
                raise serializers.ValidationError('Invalid code_filter specified: {}'.format(code_filter))

            assignments = [
                {
                    'code': assignment['code'],
                    'email': assignment['user_email']
                }
                for assignment in assignment_usages
            ]

        serializer = CouponCodeRemindSerializer(
            data=assignments,
            many=True,
            context={
                'coupon': coupon,
                'subject': subject,
                'greeting': greeting,
                'closing': closing,
            }
        )
        if serializer.is_valid():
            serializer.save()
            # Create a record of the email sent
            self._create_offer_assignment_email_sent_record(enterprise_customer, REMIND, template)
            return Response(serializer.data, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
    def remind(self, request, pk):  # pylint: disable=unused-argument
        """
        Remind users of pending offer assignments by email.
        """
        coupon = self.get_object()
        email_template = request.data.pop('template', None)
        if not email_template:
            log_message_and_raise_validation_error(
                str('Template is required.'))

        if request.data.get('assignments'):
            assignments = request.data.get('assignments')
        else:
            # If no assignment is passed, send reminder to all assignments associated with the coupon.
            vouchers = coupon.attr.coupon_vouchers.vouchers.all()
            code_filter = request.data.get('code_filter')

            if not code_filter:
                raise serializers.ValidationError(
                    'code_filter must be specified')

            if code_filter == VOUCHER_NOT_REDEEMED:
                assignment_usages = self._get_not_redeemed_usages(vouchers)
            elif code_filter == VOUCHER_PARTIAL_REDEEMED:
                assignment_usages = self._get_partial_redeemed_usages(vouchers)
            else:
                raise serializers.ValidationError(
                    'Invalid code_filter specified: {}'.format(code_filter))

            assignments = [{
                'code': assignment['code'],
                'email': assignment['user_email']
            } for assignment in assignment_usages]

        serializer = CouponCodeRemindSerializer(data=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 remind(self, request, pk):  # pylint: disable=unused-argument
        """
        Remind users of pending offer assignments by email.
        """
        coupon = self.get_object()
        email_template = request.data.pop('template', None)
        if not email_template:
            log_message_and_raise_validation_error(
                str('Template is required.'))
        serializer = CouponCodeRemindSerializer(
            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)