def test_enterprise_customer_for_voucher_when_enterprise_customer_does_not_exist(self, mock_utils): """ Verify that enterprise_customer_for_voucher assignment tag returns None if enterprise customer does not exist. """ mock_utils.side_effect = EnterpriseDoesNotExist() coupon = self.create_coupon(enterprise_customer='') voucher = coupon.attr.coupon_vouchers.vouchers.first() template = Template( "{% load enterprise %}" "{% enterprise_customer_for_voucher voucher as enterprise_customer %}" "{{ enterprise_customer.name }}" ) result = template.render(Context({'voucher': voucher, 'request': self.request})) self.assertEqual(result, '')
def get_enterprise_customer_from_voucher(site, voucher): """ Given a Voucher, find the associated Enterprise Customer and retrieve data about that customer from the Enterprise service. If there is no Enterprise Customer associated with the Voucher, `None` is returned. """ enterprise_customer_uuid = get_enterprise_customer_uuid_from_voucher( voucher) if enterprise_customer_uuid: enterprise_customer = get_enterprise_customer( site, enterprise_customer_uuid) if enterprise_customer is None: raise EnterpriseDoesNotExist( 'Enterprise customer with UUID {uuid} does not exist in the Enterprise service.' .format(uuid=enterprise_customer_uuid)) return enterprise_customer return None
def get_enterprise_customer_from_voucher(site, voucher): """ Given a Voucher, find the associated Enterprise Customer and retrieve data about that customer from the Enterprise service. If there is no Enterprise Customer associated with the Voucher, `None` is returned. """ try: offer = voucher.offers.get(benefit__range__enterprise_customer__isnull=False) except ConditionalOffer.DoesNotExist: # There's no Enterprise Customer associated with this voucher. return None # Get information about the enterprise customer from the Enterprise service. enterprise_customer_uuid = offer.benefit.range.enterprise_customer enterprise_customer = get_enterprise_customer(site, enterprise_customer_uuid) if enterprise_customer is None: raise EnterpriseDoesNotExist( 'Enterprise customer with UUID {uuid} does not exist in the Enterprise service.'.format( uuid=enterprise_customer_uuid ) ) return enterprise_customer