Example #1
0
def send_new_codes_notification_email(site, email_address, enterprise_id,
                                      coupon_id):
    """
    Send new codes email notification to an enterprise customer.

    Arguments:
        site (str): enterprise customer site
        email_address (str): recipient email address of the enterprise customer
        enterprise_id (str): enterprise customer uuid
        coupon_id (str): id of the newly created coupon
    """
    enterprise_customer_object = get_enterprise_customer(site, enterprise_id)
    enterprise_slug = enterprise_customer_object.get('slug')

    try:
        send_mail(subject=settings.NEW_CODES_EMAIL_CONFIG['email_subject'],
                  message=settings.NEW_CODES_EMAIL_CONFIG['email_body'].format(
                      enterprise_slug=enterprise_slug),
                  from_email=settings.NEW_CODES_EMAIL_CONFIG['from_email'],
                  recipient_list=[email_address],
                  fail_silently=False)
    except SMTPException:
        logger.exception(
            'New codes email failed for enterprise customer [%s] for coupon [%s]',
            enterprise_id, coupon_id)

    logger.info(
        'New codes email sent to enterprise customer [%s] for coupon [%s]',
        enterprise_id, coupon_id)
    def _get_enterprise_customer(self, enterprise_customer_uuid, site):
        if enterprise_customer_uuid not in self.enterprise_customer_map:
            enterprise_customer = get_enterprise_customer(
                site, enterprise_customer_uuid)
            self.enterprise_customer_map[
                enterprise_customer_uuid] = enterprise_customer

        return self.enterprise_customer_map[enterprise_customer_uuid]
Example #3
0
    def test_get_enterprise_customer(self):
        """
        Verify that "get_enterprise_customer" returns an appropriate response from the
        "enterprise-customer" Enterprise service API endpoint.
        """
        self.mock_specific_enterprise_customer_api(TEST_ENTERPRISE_CUSTOMER_UUID)
        response = get_enterprise_customer(self.site, self.learner.access_token, TEST_ENTERPRISE_CUSTOMER_UUID)

        self.assertEqual(TEST_ENTERPRISE_CUSTOMER_UUID, response.get('id'))
Example #4
0
    def test_get_enterprise_customer(self):
        """
        Verify that "get_enterprise_customer" returns an appropriate response from the
        "enterprise-customer" Enterprise service API endpoint.
        """
        self.mock_access_token_response()
        self.mock_specific_enterprise_customer_api(TEST_ENTERPRISE_CUSTOMER_UUID)

        # verify the caching
        with patch.object(TieredCache, 'set_all_tiers', wraps=TieredCache.set_all_tiers) as mocked_set_all_tiers:
            mocked_set_all_tiers.assert_not_called()

            response = get_enterprise_customer(self.site, TEST_ENTERPRISE_CUSTOMER_UUID)
            self.assertEqual(TEST_ENTERPRISE_CUSTOMER_UUID, response.get('id'))
            self.assertEqual(mocked_set_all_tiers.call_count, 2)

            cached_response = get_enterprise_customer(self.site, TEST_ENTERPRISE_CUSTOMER_UUID)
            self.assertEqual(mocked_set_all_tiers.call_count, 2)
            self.assertEqual(response, cached_response)
Example #5
0
 def get_context_data(self, **kwargs):
     context = super(EnterpriseOfferUpdateView, self).get_context_data(**kwargs)
     context.update({
         'editing': True,
         'enterprise_customer': get_enterprise_customer(
             self.request.site,
             self.object.condition.enterprise_customer_uuid
         )
     })
     return context
Example #6
0
def get_or_create_enterprise_offer(benefit_type,
                                   benefit_value,
                                   enterprise_customer,
                                   enterprise_customer_catalog,
                                   offer_name,
                                   site,
                                   max_uses=None,
                                   email_domains=None):

    enterprise_customer_object = get_enterprise_customer(
        site, enterprise_customer) if site else {}
    enterprise_customer_name = enterprise_customer_object.get('name', '')

    condition_kwargs = {
        'proxy_class': class_path(AssignableEnterpriseCustomerCondition),
        'enterprise_customer_uuid': enterprise_customer,
        'enterprise_customer_name': enterprise_customer_name,
        'enterprise_customer_catalog_uuid': enterprise_customer_catalog,
        'type': Condition.COUNT,
        'value': 1,
    }

    # Because there is a chance of duplicate Conditions, we are checking here for this case.
    # If duplicates are encountered, then grab the first matching Condition.
    try:
        condition, __ = Condition.objects.get_or_create(**condition_kwargs)
    except Condition.MultipleObjectsReturned:
        condition = Condition.objects.filter(**condition_kwargs).first()

    benefit, _ = Benefit.objects.get_or_create(
        proxy_class=class_path(ENTERPRISE_BENEFIT_MAP[benefit_type]),
        value=benefit_value,
        max_affected_items=1,
    )

    offer_kwargs = {
        'offer_type': ConditionalOffer.VOUCHER,
        'condition': condition,
        'benefit': benefit,
        'max_global_applications':
        int(max_uses) if max_uses is not None else None,
        'email_domains': email_domains,
        'site': site,
        'partner': site.siteconfiguration.partner if site else None,
        # For initial creation, we are setting the priority lower so that we don't want to use these
        # until we've done some other implementation work. We will update this to a higher value later.
        'priority': 5,
    }
    offer, __ = ConditionalOffer.objects.update_or_create(
        name=offer_name, defaults=offer_kwargs)

    return offer
Example #7
0
    def save(self, commit=True):
        enterprise_customer_uuid = self.cleaned_data[
            'enterprise_customer_uuid']
        enterprise_customer_catalog_uuid = self.cleaned_data[
            'enterprise_customer_catalog_uuid']
        site = self.request.site

        enterprise_customer = get_enterprise_customer(
            site, enterprise_customer_uuid)
        enterprise_customer_name = enterprise_customer['name']

        # Note: the actual name is not displayed like this in the template, so it's safe to use the UUID here.
        # And in fact we have to, because otherwise we face integrity errors since Oscar forces this name to be unique.
        # Truncate 'enterprise_customer_name' to 48 characters so that our complete name with
        # format 'Discount of type {site} provided by {enterprise_name} for {catalog_uuid}. does
        # not exceed the limit of 128 characters for Oscar's 'AbstractConditionalOffer' name.
        offer_name = _(u'Discount of type {} provided by {} for {}.'.format(
            ConditionalOffer.SITE,
            enterprise_customer_name[:48],  # pylint: disable=unsubscriptable-object,
            enterprise_customer_catalog_uuid))

        self.instance.name = offer_name
        self.instance.status = ConditionalOffer.OPEN
        self.instance.offer_type = ConditionalOffer.SITE
        self.instance.max_basket_applications = 1
        self.instance.partner = site.siteconfiguration.partner
        self.instance.priority = OFFER_PRIORITY_ENTERPRISE

        if commit:
            benefit = getattr(self.instance, 'benefit', Benefit())
            benefit.proxy_class = class_path(
                BENEFIT_MAP[self.cleaned_data['benefit_type']])
            benefit.value = self.cleaned_data['benefit_value']
            benefit.save()
            self.instance.benefit = benefit

            if hasattr(self.instance, 'condition'):
                self.instance.condition.enterprise_customer_uuid = enterprise_customer_uuid
                self.instance.condition.enterprise_customer_name = enterprise_customer_name
                self.instance.condition.enterprise_customer_catalog_uuid = enterprise_customer_catalog_uuid
                self.instance.condition.save()
            else:
                self.instance.condition = create_condition(
                    EnterpriseCustomerCondition,
                    enterprise_customer_uuid=enterprise_customer_uuid,
                    enterprise_customer_name=enterprise_customer_name,
                    enterprise_customer_catalog_uuid=
                    enterprise_customer_catalog_uuid,
                )

        return super(EnterpriseOfferForm, self).save(commit)
Example #8
0
def _get_or_create_enterprise_offer(benefit_type,
                                    benefit_value,
                                    enterprise_customer,
                                    enterprise_customer_catalog,
                                    coupon_id=None,
                                    max_uses=None,
                                    offer_number=None,
                                    email_domains=None,
                                    site=None):

    enterprise_customer_object = get_enterprise_customer(
        site, enterprise_customer) if site else {}
    enterprise_customer_name = enterprise_customer_object.get('name', '')

    condition, __ = Condition.objects.get_or_create(
        proxy_class=class_path(EnterpriseCustomerCondition),
        enterprise_customer_uuid=enterprise_customer,
        enterprise_customer_name=enterprise_customer_name,
        enterprise_customer_catalog_uuid=enterprise_customer_catalog,
        type=Condition.COUNT,
        value=1,
    )

    benefit, _ = Benefit.objects.get_or_create(
        proxy_class=class_path(ENTERPRISE_BENEFIT_MAP[benefit_type]),
        value=benefit_value,
        max_affected_items=1,
    )

    offer_name = "Coupon [{}]-{}-{} ENT Offer".format(coupon_id, benefit_type,
                                                      benefit_value)
    if offer_number:
        offer_name = "{} [{}] ENT Offer".format(offer_name, offer_number)

    offer_kwargs = {
        'offer_type': ConditionalOffer.VOUCHER,
        'condition': condition,
        'benefit': benefit,
        'max_global_applications': max_uses,
        'email_domains': email_domains,
        'site': site,
        'partner': site.siteconfiguration.partner if site else None,
        # For initial creation, we are setting the priority lower so that we don't want to use these
        # until we've done some other implementation work. We will update this to a higher value later.
        'priority': 5,
    }
    offer, __ = ConditionalOffer.objects.update_or_create(
        name=offer_name, defaults=offer_kwargs)

    return offer
Example #9
0
    def save(self, commit=True):
        enterprise_customer_uuid = self.cleaned_data[
            'enterprise_customer_uuid']
        enterprise_customer_catalog_uuid = self.cleaned_data[
            'enterprise_customer_catalog_uuid']
        site = self.request.site

        enterprise_customer = get_enterprise_customer(
            site, enterprise_customer_uuid)
        enterprise_customer_name = enterprise_customer['name']

        # Note: the actual name is not displayed like this in the template, so it's safe to use the UUID here.
        # And in fact we have to, because otherwise we face integrity errors since Oscar forces this name to be unique.
        self.instance.name = _(
            u'Discount of type {} provided by {} for {}.'.format(
                ConditionalOffer.SITE,
                enterprise_customer_name,
                enterprise_customer_catalog_uuid,
            ))
        self.instance.status = ConditionalOffer.OPEN
        self.instance.offer_type = ConditionalOffer.SITE
        self.instance.max_basket_applications = 1
        self.instance.site = site
        self.instance.priority = OFFER_PRIORITY_ENTERPRISE

        if commit:
            benefit = getattr(self.instance, 'benefit', Benefit())
            benefit.proxy_class = class_path(
                BENEFIT_MAP[self.cleaned_data['benefit_type']])
            benefit.value = self.cleaned_data['benefit_value']
            benefit.save()
            self.instance.benefit = benefit

            if hasattr(self.instance, 'condition'):
                self.instance.condition.enterprise_customer_uuid = enterprise_customer_uuid
                self.instance.condition.enterprise_customer_name = enterprise_customer_name
                self.instance.condition.enterprise_customer_catalog_uuid = enterprise_customer_catalog_uuid
                self.instance.condition.save()
            else:
                self.instance.condition = create_condition(
                    EnterpriseCustomerCondition,
                    enterprise_customer_uuid=enterprise_customer_uuid,
                    enterprise_customer_name=enterprise_customer_name,
                    enterprise_customer_catalog_uuid=
                    enterprise_customer_catalog_uuid,
                )

        return super(EnterpriseOfferForm, self).save(commit)
Example #10
0
    def save(self, commit=True):
        enterprise_customer_uuid = self.cleaned_data[
            'enterprise_customer_uuid']
        enterprise_customer_catalog_uuid = self.cleaned_data[
            'enterprise_customer_catalog_uuid']
        site = self.request.site

        enterprise_customer = get_enterprise_customer(
            site, enterprise_customer_uuid)
        enterprise_customer_name = enterprise_customer['name']

        self.instance.name = _(
            u'Discount provided by {enterprise_customer_name}.'.format(
                enterprise_customer_name=enterprise_customer_name))
        self.instance.status = ConditionalOffer.OPEN
        self.instance.offer_type = ConditionalOffer.SITE
        self.instance.max_basket_applications = 1
        self.instance.site = site
        self.instance.priority = OFFER_PRIORITY_ENTERPRISE

        if commit:
            benefit = getattr(self.instance, 'benefit', Benefit())
            benefit.proxy_class = class_path(
                BENEFIT_MAP[self.cleaned_data['benefit_type']])
            benefit.value = self.cleaned_data['benefit_value']
            benefit.save()
            self.instance.benefit = benefit

            if hasattr(self.instance, 'condition'):
                self.instance.condition.enterprise_customer_uuid = enterprise_customer_uuid
                self.instance.condition.enterprise_customer_name = enterprise_customer_name
                self.instance.condition.enterprise_customer_catalog_uuid = enterprise_customer_catalog_uuid
                self.instance.condition.save()
            else:
                self.instance.condition = create_condition(
                    EnterpriseCustomerCondition,
                    enterprise_customer_uuid=enterprise_customer_uuid,
                    enterprise_customer_name=enterprise_customer_name,
                    enterprise_customer_catalog_uuid=
                    enterprise_customer_catalog_uuid,
                )

        return super(EnterpriseOfferForm, self).save(commit)