registrasion/controllers/discount.py
def available_discounts(cls, user, categories, products):
        ''' Returns all discounts available to this user for the given
        categories and products. The discounts also list the available quantity
        for this user, not including products that are pending purchase. '''

        filtered_clauses = cls._filtered_clauses(user)

        # clauses that match provided categories
        categories = set(categories)
        # clauses that match provided products
        products = set(products)
        # clauses that match categories for provided products
        product_categories = set(product.category for product in products)
        # (Not relevant: clauses that match products in provided categories)
        all_categories = categories | product_categories

        filtered_clauses = (
            clause for clause in filtered_clauses
            if hasattr(clause, 'product') and clause.product in products or
            hasattr(clause, 'category') and clause.category in all_categories
        )

        discounts = []

        # Markers so that we don't need to evaluate given conditions
        # more than once
        accepted_discounts = set()
        failed_discounts = set()

        for clause in filtered_clauses:
            discount = clause.discount
            cond = ConditionController.for_condition(discount)

            past_use_count = clause.past_use_count
            if past_use_count >= clause.quantity:
                # This clause has exceeded its use count
                pass
            elif discount not in failed_discounts:
                # This clause is still available
                is_accepted = discount in accepted_discounts
                if is_accepted or cond.is_met(user, filtered=True):
                    # This clause is valid for this user
                    discounts.append(DiscountAndQuantity(
                        discount=discount,
                        clause=clause,
                        quantity=clause.quantity - past_use_count,
                    ))
                    accepted_discounts.add(discount)
                else:
                    # This clause is not valid for this user
                    failed_discounts.add(discount)
        return discounts
def available_discounts(cls, user, categories, products):
        ''' Returns all discounts available to this user for the given
        categories and products. The discounts also list the available quantity
        for this user, not including products that are pending purchase. '''

        filtered_clauses = cls._filtered_clauses(user)

        # clauses that match provided categories
        categories = set(categories)
        # clauses that match provided products
        products = set(products)
        # clauses that match categories for provided products
        product_categories = set(product.category for product in products)
        # (Not relevant: clauses that match products in provided categories)
        all_categories = categories | product_categories

        filtered_clauses = (
            clause for clause in filtered_clauses
            if hasattr(clause, 'product') and clause.product in products or
            hasattr(clause, 'category') and clause.category in all_categories
        )

        discounts = []

        # Markers so that we don't need to evaluate given conditions
        # more than once
        accepted_discounts = set()
        failed_discounts = set()

        for clause in filtered_clauses:
            discount = clause.discount
            cond = ConditionController.for_condition(discount)

            past_use_count = clause.past_use_count
            if past_use_count >= clause.quantity:
                # This clause has exceeded its use count
                pass
            elif discount not in failed_discounts:
                # This clause is still available
                is_accepted = discount in accepted_discounts
                if is_accepted or cond.is_met(user, filtered=True):
                    # This clause is valid for this user
                    discounts.append(DiscountAndQuantity(
                        discount=discount,
                        clause=clause,
                        quantity=clause.quantity - past_use_count,
                    ))
                    accepted_discounts.add(discount)
                else:
                    # This clause is not valid for this user
                    failed_discounts.add(discount)
        return discounts
Returns all discounts available to this user for the given
        categories and products. The discounts also list the available quantity
        for this user, not including products that are pending purchase.