def get_product(sku): """Retrieve the product corresponding to the provided SKU.""" try: return Product.objects.get(stockrecords__partner_sku=sku) except Product.DoesNotExist: raise exceptions.ProductNotFoundError( exceptions.PRODUCT_NOT_FOUND_DEVELOPER_MESSAGE.format(sku=sku))
def get_voucher_and_products_from_code(code): """ Returns a voucher and product for a given code. Arguments: code (str): The code of a coupon voucher. Returns: voucher (Voucher): The Voucher for the passed code. products (list): List of Products associated with the Voucher. Raises: Voucher.DoesNotExist: When no vouchers with provided code exist. ProductNotFoundError: When no products are associated with the voucher. """ voucher = get_cached_voucher(code) voucher_range = voucher.best_offer.benefit.range has_catalog_configuration = voucher_range and ( voucher_range.catalog_query or voucher_range.course_catalog) # pylint: disable=consider-using-ternary is_enterprise = ((voucher_range and voucher_range.enterprise_customer) or voucher.best_offer.condition.enterprise_customer_uuid) products = voucher_range.all_products() if voucher_range else [] if products or has_catalog_configuration or is_enterprise: # List of products is empty in case of Multi-course coupon return voucher, products raise exceptions.ProductNotFoundError()
def get_voucher_and_products_from_code(code): """ Returns a voucher and product for a given code. Arguments: code (str): The code of a coupon voucher. Returns: voucher (Voucher): The Voucher for the passed code. products (list): List of Products associated with the Voucher. Raises: Voucher.DoesNotExist: When no vouchers with provided code exist. ProductNotFoundError: When no products are associated with the voucher. """ voucher = Voucher.objects.get(code=code) products = voucher.offers.all()[0].benefit.range.all_products() if products: return voucher, products else: raise exceptions.ProductNotFoundError()
def get_voucher_and_products_from_code(code): """ Returns a voucher and product for a given code. Arguments: code (str): The code of a coupon voucher. Returns: voucher (Voucher): The Voucher for the passed code. products (list): List of Products associated with the Voucher. Raises: Voucher.DoesNotExist: When no vouchers with provided code exist. ProductNotFoundError: When no products are associated with the voucher. """ voucher = get_cached_voucher(code) voucher_range = voucher.offers.first().benefit.range products = voucher_range.all_products() if products or voucher_range.catalog_query or voucher_range.course_catalog: # List of products is empty in case of Multi-course coupon return voucher, products else: raise exceptions.ProductNotFoundError()