Files
@ 4dbe69574c2a
Branch filter:
Location: symposion_app/registrasion/controllers/conditions.py - annotation
4dbe69574c2a
9.2 KiB
text/x-python
Adds report that tracks the free money in the system
Fixes #52
Fixes #52
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | 3f1be0e14e06 3f1be0e14e06 d9e433659d01 3f1be0e14e06 3f1be0e14e06 d9e433659d01 d9e433659d01 875f736d67c5 875f736d67c5 d9e433659d01 3f1be0e14e06 145fd057aca0 3f1be0e14e06 3f1be0e14e06 d9e433659d01 4fedc733047a d9e433659d01 d9e433659d01 3f1be0e14e06 3f1be0e14e06 d9e433659d01 d9e433659d01 3f1be0e14e06 3f1be0e14e06 875f736d67c5 875f736d67c5 875f736d67c5 875f736d67c5 4d134e95d70b 875f736d67c5 4fedc733047a 875f736d67c5 875f736d67c5 d9e433659d01 d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 d9e433659d01 3f1be0e14e06 d9e433659d01 d9e433659d01 d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 c24b9ee213b9 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 3f1be0e14e06 1e7a2abc7f92 3f1be0e14e06 c24b9ee213b9 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 1e7a2abc7f92 3f1be0e14e06 d9e433659d01 d9e433659d01 3f1be0e14e06 d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 d9e433659d01 3f1be0e14e06 d9e433659d01 d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 3f1be0e14e06 6d52a4c18ff8 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 4fedc733047a d9e433659d01 d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 d9e433659d01 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 6d52a4c18ff8 d9e433659d01 6d52a4c18ff8 d9e433659d01 d9e433659d01 3f1be0e14e06 b40505117f4c b40505117f4c b40505117f4c 4fedc733047a d9e433659d01 d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 d9e433659d01 d9e433659d01 d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 d9e433659d01 3f1be0e14e06 3f1be0e14e06 d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 d9e433659d01 3f1be0e14e06 3f1be0e14e06 1e7a2abc7f92 3f1be0e14e06 d9e433659d01 3f1be0e14e06 3f1be0e14e06 875f736d67c5 1e7a2abc7f92 1e7a2abc7f92 63dfd353c16e 1e7a2abc7f92 3f1be0e14e06 4d134e95d70b 760996588372 4fedc733047a 4d134e95d70b 4d134e95d70b 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 162db2481781 3f1be0e14e06 162db2481781 3f1be0e14e06 4d134e95d70b 4d134e95d70b 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 d9e433659d01 d9e433659d01 4d134e95d70b 4d134e95d70b 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 4d134e95d70b 3f1be0e14e06 3f1be0e14e06 d9e433659d01 d9e433659d01 3f1be0e14e06 4fedc733047a d9e433659d01 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 3f1be0e14e06 d9e433659d01 6d52a4c18ff8 | from django.db.models import Case
from django.db.models import F, Q
from django.db.models import Sum
from django.db.models import Value
from django.db.models import When
from django.utils import timezone
from registrasion.models import commerce
from registrasion.models import conditions
_BIG_QUANTITY = 99999999 # A big quantity
class ConditionController(object):
''' Base class for testing conditions that activate Flag
or Discount objects. '''
def __init__(self, condition):
self.condition = condition
@staticmethod
def _controllers():
return {
conditions.CategoryFlag: CategoryConditionController,
conditions.IncludedProductDiscount: ProductConditionController,
conditions.ProductFlag: ProductConditionController,
conditions.TimeOrStockLimitDiscount:
TimeOrStockLimitDiscountController,
conditions.TimeOrStockLimitFlag:
TimeOrStockLimitFlagController,
conditions.VoucherDiscount: VoucherConditionController,
conditions.VoucherFlag: VoucherConditionController,
}
@staticmethod
def for_type(cls):
return ConditionController._controllers()[cls]
@staticmethod
def for_condition(condition):
try:
return ConditionController.for_type(type(condition))(condition)
except KeyError:
return ConditionController()
@classmethod
def pre_filter(cls, queryset, user):
''' Returns only the flag conditions that might be available for this
user. It should hopefully reduce the number of queries that need to be
executed to determine if a flag is met.
If this filtration implements the same query as is_met, then you should
be able to implement ``is_met()`` in terms of this.
Arguments:
queryset (Queryset[c]): The canditate conditions.
user (User): The user for whom we're testing these conditions.
Returns:
Queryset[c]: A subset of the conditions that pass the pre-filter
test for this user.
'''
# Default implementation does NOTHING.
return queryset
def passes_filter(self, user):
''' Returns true if the condition passes the filter '''
cls = type(self.condition)
qs = cls.objects.filter(pk=self.condition.id)
return self.condition in self.pre_filter(qs, user)
def user_quantity_remaining(self, user, filtered=False):
''' Returns the number of items covered by this flag condition the
user can add to the current cart. This default implementation returns
a big number if is_met() is true, otherwise 0.
Either this method, or is_met() must be overridden in subclasses.
'''
return _BIG_QUANTITY if self.is_met(user, filtered) else 0
def is_met(self, user, filtered=False):
''' Returns True if this flag condition is met, otherwise returns
False.
Either this method, or user_quantity_remaining() must be overridden
in subclasses.
Arguments:
user (User): The user for whom this test must be met.
filter (bool): If true, this condition was part of a queryset
returned by pre_filter() for this user.
'''
return self.user_quantity_remaining(user, filtered) > 0
class IsMetByFilter(object):
def is_met(self, user, filtered=False):
''' Returns True if this flag condition is met, otherwise returns
False. It determines if the condition is met by calling pre_filter
with a queryset containing only self.condition. '''
if filtered:
return True # Why query again?
return self.passes_filter(user)
class RemainderSetByFilter(object):
def user_quantity_remaining(self, user, filtered=True):
''' returns 0 if the date range is violated, otherwise, it will return
the quantity remaining under the stock limit.
The filter for this condition must add an annotation called "remainder"
in order for this to work.
'''
if filtered:
if hasattr(self.condition, "remainder"):
return self.condition.remainder
# Mark self.condition with a remainder
qs = type(self.condition).objects.filter(pk=self.condition.id)
qs = self.pre_filter(qs, user)
if len(qs) > 0:
return qs[0].remainder
else:
return 0
class CategoryConditionController(IsMetByFilter, ConditionController):
@classmethod
def pre_filter(self, queryset, user):
''' Returns all of the items from queryset where the user has a
product from a category invoking that item's condition in one of their
carts. '''
in_user_carts = Q(
enabling_category__product__productitem__cart__user=user
)
released = commerce.Cart.STATUS_RELEASED
in_released_carts = Q(
enabling_category__product__productitem__cart__status=released
)
queryset = queryset.filter(in_user_carts)
queryset = queryset.exclude(in_released_carts)
return queryset
class ProductConditionController(IsMetByFilter, ConditionController):
''' Condition tests for ProductFlag and
IncludedProductDiscount. '''
@classmethod
def pre_filter(self, queryset, user):
''' Returns all of the items from queryset where the user has a
product invoking that item's condition in one of their carts. '''
in_user_carts = Q(enabling_products__productitem__cart__user=user)
released = commerce.Cart.STATUS_RELEASED
in_released_carts = Q(
enabling_products__productitem__cart__status=released
)
queryset = queryset.filter(in_user_carts)
queryset = queryset.exclude(in_released_carts)
return queryset
class TimeOrStockLimitConditionController(
RemainderSetByFilter,
ConditionController,
):
''' Common condition tests for TimeOrStockLimit Flag and
Discount.'''
@classmethod
def pre_filter(self, queryset, user):
''' Returns all of the items from queryset where the date falls into
any specified range, but not yet where the stock limit is not yet
reached.'''
now = timezone.now()
# Keep items with no start time, or start time not yet met.
queryset = queryset.filter(Q(start_time=None) | Q(start_time__lte=now))
queryset = queryset.filter(Q(end_time=None) | Q(end_time__gte=now))
# Filter out items that have been reserved beyond the limits
quantity_or_zero = self._calculate_quantities(user)
remainder = Case(
When(limit=None, then=Value(_BIG_QUANTITY)),
default=F("limit") - Sum(quantity_or_zero),
)
queryset = queryset.annotate(remainder=remainder)
queryset = queryset.filter(remainder__gt=0)
return queryset
@classmethod
def _relevant_carts(cls, user):
reserved_carts = commerce.Cart.reserved_carts()
reserved_carts = reserved_carts.exclude(
user=user,
status=commerce.Cart.STATUS_ACTIVE,
)
return reserved_carts
class TimeOrStockLimitFlagController(
TimeOrStockLimitConditionController):
@classmethod
def _calculate_quantities(cls, user):
reserved_carts = cls._relevant_carts(user)
# Calculate category lines
item_cats = F('categories__product__productitem__product__category')
reserved_category_products = (
Q(categories=item_cats) &
Q(categories__product__productitem__cart__in=reserved_carts)
)
# Calculate product lines
reserved_products = (
Q(products=F('products__productitem__product')) &
Q(products__productitem__cart__in=reserved_carts)
)
category_quantity_in_reserved_carts = When(
reserved_category_products,
then="categories__product__productitem__quantity",
)
product_quantity_in_reserved_carts = When(
reserved_products,
then="products__productitem__quantity",
)
quantity_or_zero = Case(
category_quantity_in_reserved_carts,
product_quantity_in_reserved_carts,
default=Value(0),
)
return quantity_or_zero
class TimeOrStockLimitDiscountController(TimeOrStockLimitConditionController):
@classmethod
def _calculate_quantities(cls, user):
reserved_carts = cls._relevant_carts(user)
quantity_in_reserved_carts = When(
discountitem__cart__in=reserved_carts,
then="discountitem__quantity"
)
quantity_or_zero = Case(
quantity_in_reserved_carts,
default=Value(0)
)
return quantity_or_zero
class VoucherConditionController(IsMetByFilter, ConditionController):
''' Condition test for VoucherFlag and VoucherDiscount.'''
@classmethod
def pre_filter(self, queryset, user):
''' Returns all of the items from queryset where the user has entered
a voucher that invokes that item's condition in one of their carts. '''
return queryset.filter(voucher__cart__user=user)
|