symposion_app/registrasion/controllers/product.py

82 lines
2.5 KiB
Python
Raw Normal View History

import itertools
from django.db.models import Sum
2016-01-22 05:01:30 +00:00
from registrasion import models as rego
from category import CategoryController
2016-01-22 05:01:30 +00:00
from conditions import ConditionController
class ProductController(object):
def __init__(self, product):
self.product = product
@classmethod
def available_products(cls, user, category=None, products=None):
''' Returns a list of all of the products that are available per
flag conditions from the given categories.
TODO: refactor so that all conditions are tested here and
can_add_with_flags calls this method. '''
if category is None and products is None:
raise ValueError("You must provide products or a category")
if category is not None:
all_products = rego.Product.objects.filter(category=category)
all_products = all_products.select_related("category")
else:
all_products = []
if products is not None:
all_products = set(itertools.chain(all_products, products))
cat_quants = dict(
(
category,
CategoryController(category).user_quantity_remaining(user),
)
for category in set(product.category for product in all_products)
)
passed_limits = set(
product
for product in all_products
if cat_quants[product.category] > 0
if cls(product).user_quantity_remaining(user) > 0
)
failed_and_messages = ConditionController.test_flags(
user, products=passed_limits
2016-04-06 06:09:57 +00:00
)
failed_conditions = set(i[0] for i in failed_and_messages)
out = list(passed_limits - failed_conditions)
out.sort(key=lambda product: product.order)
return out
def user_quantity_remaining(self, user):
''' Returns the quantity of this product that the user add in the
current cart. '''
prod_limit = self.product.limit_per_user
if prod_limit is None:
# Don't need to run the remaining queries
return 999999 # We can do better
2016-01-22 05:01:30 +00:00
carts = rego.Cart.objects.filter(
user=user,
active=False,
released=False,
)
2016-01-22 06:02:07 +00:00
items = rego.ProductItem.objects.filter(
2016-04-02 00:02:01 +00:00
cart__in=carts,
product=self.product,
)
2016-01-22 05:01:30 +00:00
prod_count = items.aggregate(Sum("quantity"))["quantity__sum"] or 0
2016-04-02 00:02:01 +00:00
return prod_limit - prod_count