Changeset - de8301577617
[Not reviewed]
0 2 0
Christopher Neugebauer - 8 years ago 2016-05-02 00:55:29
chrisjrn@gmail.com
Fixes ordering error in error display
2 files changed with 32 insertions and 27 deletions:
0 comments (0 inline, 0 general)
registrasion/controllers/flag.py
Show inline comments
 
import itertools
 
import operator
 

	
 
from collections import defaultdict
 
from collections import namedtuple
 
from django.db.models import Count
 
from django.db.models import Q
 

	
 
from .batch import BatchController
 
from .conditions import ConditionController
 

	
 
from registrasion.models import conditions
 
from registrasion.models import inventory
 

	
 

	
 
class FlagController(object):
 

	
 
    SINGLE = True
 
    PLURAL = False
 
    NONE = True
 
    SOME = False
 
    MESSAGE = {
 
        NONE: {
 
            SINGLE:
 
                "%(items)s is no longer available to you",
 
            PLURAL:
 
                "%(items)s are no longer available to you",
 
        },
 
        SOME: {
 
            SINGLE:
 
                "Only %(remainder)d of the following item remains: %(items)s",
 
            PLURAL:
 
                "Only %(remainder)d of the following items remain: %(items)s"
 
        },
 
    }
 

	
 
    @classmethod
 
    def test_flags(
 
            cls, user, products=None, product_quantities=None):
 
        ''' Evaluates all of the flag conditions on the given products.
 

	
 
        If `product_quantities` is supplied, the condition is only met if it
 
        will permit the sum of the product quantities for all of the products
 
        it covers. Otherwise, it will be met if at least one item can be
 
        accepted.
 

	
 
        If all flag conditions pass, an empty list is returned, otherwise
 
        a list is returned containing all of the products that are *not
 
        enabled*. '''
 

	
 
        if products is not None and product_quantities is not None:
 
            raise ValueError("Please specify only products or "
 
                             "product_quantities")
 
        elif products is None:
 
            products = set(i[0] for i in product_quantities)
 
            quantities = dict((product, quantity)
 
                              for product, quantity in product_quantities)
 
        elif product_quantities is None:
 
            products = set(products)
 
            quantities = {}
...
 
@@ -82,105 +63,128 @@ class FlagController(object):
 
            remainder = cond.user_quantity_remaining(user, filtered=True)
 

	
 
            # Get all products covered by this condition, and the products
 
            # from the categories covered by this condition
 

	
 
            ids = [product.id for product in products]
 

	
 
            # TODO: This is re-evaluated a lot.
 
            all_products = inventory.Product.objects.filter(id__in=ids)
 
            cond = (
 
                Q(flagbase_set=condition) |
 
                Q(category__in=condition.categories.all())
 
            )
 

	
 
            all_products = all_products.filter(cond)
 
            all_products = all_products.select_related("category")
 

	
 
            if quantities:
 
                consumed = sum(quantities[i] for i in all_products)
 
            else:
 
                consumed = 1
 
            met = consumed <= remainder
 

	
 
            if not met:
 
                items = ", ".join(str(product) for product in all_products)
 
                base = cls.MESSAGE[remainder == 0][len(all_products) == 1]
 
                message = base % {"items": items, "remainder": remainder}
 
                message = cls._error_message(all_products, remainder)
 

	
 
            for product in all_products:
 
                if condition.is_disable_if_false:
 
                    do_not_disable[product] &= met
 
                    dif_count[product] += 1
 
                else:
 
                    do_enable[product] |= met
 
                    eit_count[product] += 1
 

	
 
                if not met and product not in messages:
 
                    messages[product] = message
 

	
 
        total_flags = FlagCounter.count(user)
 

	
 
        valid = {}
 

	
 
        # the problem is that now, not every condition falls into
 
        # do_not_disable or do_enable '''
 
        # You should look into this, chris :)
 

	
 
        for product in products:
 
            if quantities:
 
                if quantities[product] == 0:
 
                    continue
 

	
 
            f = total_flags.get(product)
 
            if f.dif > 0 and f.dif != dif_count[product]:
 
                do_not_disable[product] = False
 
                if product not in messages:
 
                    messages[product] = "Some disable-if-false " \
 
                                        "conditions were not met"
 
                    messages[product] = cls._error_message([product], 0)
 
            if f.eit > 0 and product not in do_enable:
 
                do_enable[product] = False
 
                if product not in messages:
 
                    messages[product] = "Some enable-if-true " \
 
                                        "conditions were not met"
 
                    messages[product] = cls._error_message([product], 0)
 

	
 
        for product in itertools.chain(do_not_disable, do_enable):
 
            f = total_flags.get(product)
 
            if product in do_enable:
 
                # If there's an enable-if-true, we need need of those met too.
 
                # (do_not_disable will default to true otherwise)
 
                valid[product] = do_not_disable[product] and do_enable[product]
 
            elif product in do_not_disable:
 
                # If there's a disable-if-false condition, all must be met
 
                valid[product] = do_not_disable[product]
 

	
 
        error_fields = [
 
            (product, messages[product])
 
            for product in valid if not valid[product]
 
        ]
 

	
 
        return error_fields
 

	
 
    SINGLE = True
 
    PLURAL = False
 
    NONE = True
 
    SOME = False
 
    MESSAGE = {
 
        NONE: {
 
            SINGLE:
 
                "%(items)s is no longer available to you",
 
            PLURAL:
 
                "%(items)s are no longer available to you",
 
        },
 
        SOME: {
 
            SINGLE:
 
                "Only %(remainder)d of the following item remains: %(items)s",
 
            PLURAL:
 
                "Only %(remainder)d of the following items remain: %(items)s"
 
        },
 
    }
 

	
 
    @classmethod
 
    def _error_message(cls, affected, remainder):
 
        product_strings = (str(product) for product in affected)
 
        items = ", ".join(product_strings)
 
        base = cls.MESSAGE[remainder == 0][len(affected) == 1]
 
        message = base % {"items": items, "remainder": remainder}
 
        return message
 

	
 
    @classmethod
 
    @BatchController.memoise
 
    def _filtered_flags(cls, user):
 
        '''
 

	
 
        Returns:
 
            Sequence[flagbase]: All flags that passed the filter function.
 

	
 
        '''
 

	
 
        types = list(ConditionController._controllers())
 
        flagtypes = [i for i in types if issubclass(i, conditions.FlagBase)]
 

	
 
        all_subsets = []
 

	
 
        for flagtype in flagtypes:
 
            flags = flagtype.objects.all()
 
            ctrl = ConditionController.for_type(flagtype)
 
            flags = ctrl.pre_filter(flags, user)
 
            all_subsets.append(flags)
 

	
 
        return list(itertools.chain(*all_subsets))
 

	
 

	
registrasion/views.py
Show inline comments
...
 
@@ -430,49 +430,50 @@ def _handle_products(request, category, products, prefix):
 
                    "You must have at least one item from this category",
 
                )
 
    handled = False if products_form.errors else True
 

	
 
    # Making this a function to lazily evaluate when it's displayed
 
    # in templates.
 

	
 
    discounts = util.lazy(
 
        DiscountController.available_discounts,
 
        request.user,
 
        [],
 
        products,
 
    )
 

	
 
    return products_form, discounts, handled
 

	
 

	
 
def _set_quantities_from_products_form(products_form, current_cart):
 

	
 
    quantities = list(products_form.product_quantities())
 
    id_to_quantity = dict(i[:2] for i in quantities)
 
    pks = [i[0] for i in quantities]
 
    products = inventory.Product.objects.filter(
 
        id__in=pks,
 
    ).select_related("category")
 
    ).select_related("category").order_by("id")
 
    quantities.sort(key = lambda i: i[0])
 

	
 
    product_quantities = [
 
        (product, id_to_quantity[product.id]) for product in products
 
    ]
 
    field_names = dict(
 
        (i[0][0], i[1][2]) for i in zip(product_quantities, quantities)
 
    )
 

	
 
    try:
 
        current_cart.set_quantities(product_quantities)
 
    except CartValidationError as ve:
 
        for ve_field in ve.error_list:
 
            product, message = ve_field.message
 
            if product in field_names:
 
                field = field_names[product]
 
            elif isinstance(product, inventory.Product):
 
                continue
 
            else:
 
                field = None
 
            products_form.add_error(field, message)
 

	
 

	
 
def _handle_voucher(request, prefix):
 
    ''' Handles a voucher form in the given request. Returns the voucher
0 comments (0 inline, 0 general)