Ejemplo n.º 1
0
    def create_data(self, voucher_type, max_uses, assignments):
        """
        Create vouchers, offers and offer assignments.
        """
        codes = {assignment['code'] for assignment in assignments}
        emails = sorted({assignment['user_email'] for assignment in assignments})
        quantity = len(codes)

        voucher_types = (Voucher.MULTI_USE, Voucher.ONCE_PER_CUSTOMER, Voucher.MULTI_USE_PER_CUSTOMER)
        num_of_offers = quantity if voucher_type in voucher_types else 1

        offers = []
        for __ in range(num_of_offers):
            offers.append(factories.EnterpriseOfferFactory(max_global_applications=max_uses))

        coupon = self.create_coupon(quantity=quantity, voucher_type=voucher_type)
        for index, info in enumerate(zip(coupon.attr.coupon_vouchers.vouchers.all(), codes)):
            voucher, code = info
            voucher.code = code
            voucher.offers.add(offers[index] if len(offers) > 1 else offers[0])
            voucher.save()

        data = {'codes': codes, 'emails': emails}
        serializer = CouponCodeAssignmentSerializer(data=data, context={'coupon': coupon})
        if serializer.is_valid():
            serializer.save()
            assignments = serializer.data
Ejemplo n.º 2
0
    def assign(self, request, pk):  # pylint: disable=unused-argument
        """
        Assign users by email to codes within the Coupon.
        """
        coupon = self.get_object()
        self._validate_coupon_availablity(
            coupon, 'Coupon is not available for code assignment')
        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)
        sender_id = request.user.lms_user_id

        self._validate_email_fields(subject, greeting, closing)

        serializer = CouponCodeAssignmentSerializer(data=request.data,
                                                    context={
                                                        'coupon': coupon,
                                                        'subject': subject,
                                                        'greeting': greeting,
                                                        'closing': closing,
                                                        'template_id':
                                                        template_id,
                                                        'sender_id': sender_id,
                                                        'site': request.site
                                                    })
        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)
Ejemplo n.º 3
0
    def assign(self, request, pk):  # pylint: disable=unused-argument
        """
        Assign users by email to codes within the Coupon.
        """
        coupon = self.get_object()
        self._validate_coupon_availablity(
            coupon, 'Coupon is not available for code assignment')
        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)

        serializer = CouponCodeAssignmentSerializer(data=request.data,
                                                    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, ASSIGN, template)
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 4
0
    def test_enterprise_customer_coupon_redemption_for_no_offer_assignment(self):
        """
        Verify the warning message appears on redemption if coupon does not have any
        offer assignments for the user.
        """
        expected_message = (
            'This code is not valid with your email. '
            'Please login with the correct email assigned '
            'to the code or contact your Learning Manager '
            'for additional questions.'
        )

        code, coupon = self.prepare_enterprise_data(
            benefit_value=5, consent_enabled=False, catalog=self.catalog,
            enterprise_customer_catalog=ENTERPRISE_CUSTOMER_CATALOG
        )
        self.mock_assignable_enterprise_condition_calls(ENTERPRISE_CUSTOMER_CATALOG)
        self.mock_enterprise_learner_api_for_learner_with_no_enterprise()
        self.mock_enterprise_learner_post_api()
        self.mock_course_run_detail_endpoint(self.course, discovery_api_url=self.site_configuration.discovery_api_url)

        data = {'codes': [code], 'emails': ['*****@*****.**']}
        serializer = CouponCodeAssignmentSerializer(data=data, context={'coupon': coupon})
        if serializer.is_valid():
            serializer.save()

        response = self.client.get(self.redeem_url_with_params(code=code), follow=True)

        messages = []
        messages += response.context['messages']
        self.assertEqual(messages[0].tags, 'warning')
        self.assertEqual(messages[0].message, expected_message)
Ejemplo n.º 5
0
    def assign(self, request, pk):  # pylint: disable=unused-argument
        """
        Assign users by email to codes within the Coupon.
        """
        coupon = self.get_object()
        serializer = CouponCodeAssignmentSerializer(data=request.data,
                                                    context={'coupon': coupon})
        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)