Changeset - 3b5b958b78b6
[Not reviewed]
0 2 0
Christopher Neugebauer - 8 years ago 2016-04-29 01:09:08
chrisjrn@gmail.com
Makes the discounts section from _handle_products evaluate lazily, just in case it’s never displayed in a template (those are some very very expensive queries there).
2 files changed with 35 insertions and 1 deletions:
0 comments (0 inline, 0 general)
registrasion/util.py
Show inline comments
...
 
@@ -25,3 +25,33 @@ def all_arguments_optional(ntcls):
 
    )
 

	
 
    return ntcls
 

	
 

	
 
def lazy(function, *args, **kwargs):
 
    ''' Produces a callable so that functions can be lazily evaluated in
 
    templates.
 

	
 
    Arguments:
 

	
 
        function (callable): The function to call at evaluation time.
 

	
 
        args: Positional arguments, passed directly to ``function``.
 

	
 
        kwargs: Keyword arguments, passed directly to ``function``.
 

	
 
    Return:
 

	
 
        callable: A callable that will evaluate a call to ``function`` with
 
            the specified arguments.
 

	
 
    '''
 

	
 
    NOT_EVALUATED = object()
 
    retval = [NOT_EVALUATED]
 

	
 
    def evaluate():
 
        if retval[0] is NOT_EVALUATED:
 
            retval[0] = function(*args, **kwargs)
 
        return retval[0]
 

	
 
    return evaluate
registrasion/views.py
Show inline comments
...
 
@@ -429,7 +429,11 @@ def _handle_products(request, category, products, prefix):
 
                )
 
    handled = False if products_form.errors else True
 

	
 
    discounts = DiscountController.available_discounts(
 
    # Making this a function to lazily evaluate when it's displayed
 
    # in templates.
 

	
 
    discounts = util.lazy(
 
        DiscountController.available_discounts,
 
        request.user,
 
        [],
 
        products,
0 comments (0 inline, 0 general)