-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
abstract_models.py
1328 lines (1132 loc) · 44.5 KB
/
abstract_models.py
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pylint: disable=unused-argument, W0621
import csv
import operator
from decimal import ROUND_DOWN
from decimal import Decimal as D
from django.conf import settings
from django.core import exceptions
from django.db import models
from django.db.models.query import Q
from django.template.defaultfilters import date as date_filter
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.timezone import get_current_timezone, now
from django.utils.translation import gettext_lazy as _
from oscar.core.compat import AUTH_USER_MODEL
from oscar.core.loading import cached_import_string, get_class, get_classes, get_model
from oscar.models import fields
from oscar.templatetags.currency_filters import currency
ExpandDownwardsCategoryQueryset = get_class(
"catalogue.expressions", "ExpandDownwardsCategoryQueryset"
)
ActiveOfferManager, RangeManager, BrowsableRangeManager = get_classes(
"offer.managers", ["ActiveOfferManager", "RangeManager", "BrowsableRangeManager"]
)
ZERO_DISCOUNT = get_class("offer.results", "ZERO_DISCOUNT")
load_proxy, unit_price = get_classes("offer.utils", ["load_proxy", "unit_price"])
class BaseOfferMixin(models.Model):
class Meta:
abstract = True
def proxy(self):
"""
Return the proxy model
"""
klassmap = self.proxy_map
# Short-circuit logic if current class is already a proxy class.
if self.__class__ in klassmap.values():
return self
field_dict = dict(self.__dict__)
for field in list(field_dict.keys()):
if field.startswith("_"):
del field_dict[field]
if self.proxy_class:
klass = load_proxy(self.proxy_class)
# Short-circuit again.
if self.__class__ == klass:
return self
return klass(**field_dict)
if self.type in klassmap:
return klassmap[self.type](**field_dict)
raise RuntimeError(
"Unrecognised %s type (%s)" % (self.__class__.__name__.lower(), self.type)
)
def __str__(self):
return self.name
@property
def name(self):
"""
A text description of the benefit/condition. Every proxy class
has to implement it.
This is used in the dropdowns within the offer dashboard.
"""
proxy_instance = self.proxy()
if self.proxy_class and self.__class__ == proxy_instance.__class__:
raise AssertionError("Name property is not defined on proxy class.")
return proxy_instance.name
@property
def description(self):
"""
A description of the benefit/condition.
Defaults to the name. May contain HTML.
"""
return self.name
class AbstractConditionalOffer(models.Model):
"""
A conditional offer (e.g. buy 1, get 10% off)
"""
name = models.CharField(
_("Name"),
max_length=128,
unique=True,
help_text=_("This is displayed within the customer's basket"),
)
slug = fields.AutoSlugField(
_("Slug"), max_length=128, unique=True, populate_from="name"
)
description = models.TextField(
_("Description"),
blank=True,
help_text=_("This is displayed on the offer browsing page"),
)
# Offers come in a few different types:
# (a) Offers that are available to all customers on the site. e.g. a
# 3-for-2 offer.
# (b) Offers that are linked to a voucher, and only become available once
# that voucher has been applied to the basket
# (c) Offers that are linked to a user. e.g. all students get 10% off. The
# code to apply this offer needs to be coded
# (d) Session offers - these are temporarily available to a user after some
# trigger event. e.g. users coming from some affiliate site get 10%
# off.
SITE, VOUCHER, USER, SESSION = ("Site", "Voucher", "User", "Session")
TYPE_CHOICES = (
(SITE, _("Site offer - available to all users")),
(
VOUCHER,
_(
"Voucher offer - only available after entering "
"the appropriate voucher code"
),
),
(USER, _("User offer - available to certain types of user")),
(
SESSION,
_(
"Session offer - temporary offer, available for "
"a user for the duration of their session"
),
),
)
offer_type = models.CharField(
_("Type"), choices=TYPE_CHOICES, default=SITE, max_length=128
)
exclusive = models.BooleanField(
_("Exclusive offer"),
help_text=_("Exclusive offers cannot be combined on the same items"),
default=True,
)
combinations = models.ManyToManyField(
"offer.ConditionalOffer",
help_text=_(
"Select other non-exclusive offers that this offer can be combined with on the same items"
),
related_name="in_combination",
limit_choices_to={"exclusive": False},
blank=True,
)
# We track a status variable so it's easier to load offers that are
# 'available' in some sense.
OPEN, SUSPENDED, CONSUMED = "Open", "Suspended", "Consumed"
status = models.CharField(_("Status"), max_length=64, default=OPEN)
condition = models.ForeignKey(
"offer.Condition",
on_delete=models.CASCADE,
related_name="offers",
verbose_name=_("Condition"),
)
benefit = models.ForeignKey(
"offer.Benefit",
on_delete=models.CASCADE,
related_name="offers",
verbose_name=_("Benefit"),
)
# Some complicated situations require offers to be applied in a set order.
priority = models.IntegerField(
_("Priority"),
default=0,
db_index=True,
help_text=_("The highest priority offers are applied first"),
)
# AVAILABILITY
# Range of availability. Note that if this is a voucher offer, then these
# dates are ignored and only the dates from the voucher are used to
# determine availability.
start_datetime = models.DateTimeField(
_("Start date"),
blank=True,
null=True,
help_text=_(
"Offers are active from the start date. "
"Leave this empty if the offer has no start date."
),
)
end_datetime = models.DateTimeField(
_("End date"),
blank=True,
null=True,
help_text=_(
"Offers are active until the end date. "
"Leave this empty if the offer has no expiry date."
),
)
# Use this field to limit the number of times this offer can be applied in
# total. Note that a single order can apply an offer multiple times so
# this is not necessarily the same as the number of orders that can use it.
# Also see max_basket_applications.
max_global_applications = models.PositiveIntegerField(
_("Max global applications"),
help_text=_(
"The number of times this offer can be used before it is unavailable"
),
blank=True,
null=True,
)
# Use this field to limit the number of times this offer can be used by a
# single user. This only works for signed-in users - it doesn't really
# make sense for sites that allow anonymous checkout.
max_user_applications = models.PositiveIntegerField(
_("Max user applications"),
help_text=_("The number of times a single user can use this offer"),
blank=True,
null=True,
)
# Use this field to limit the number of times this offer can be applied to
# a basket (and hence a single order). Often, an offer should only be
# usable once per basket/order, so this field will commonly be set to 1.
max_basket_applications = models.PositiveIntegerField(
_("Max basket applications"),
blank=True,
null=True,
help_text=_(
"The number of times this offer can be applied to a basket (and order)"
),
)
# Use this field to limit the amount of discount an offer can lead to.
# This can be helpful with budgeting.
max_discount = models.DecimalField(
_("Max discount"),
decimal_places=2,
max_digits=12,
null=True,
blank=True,
help_text=_(
"When an offer has given more discount to orders "
"than this threshold, then the offer becomes "
"unavailable"
),
)
# TRACKING
# These fields are used to enforce the limits set by the
# max_* fields above.
total_discount = models.DecimalField(
_("Total Discount"), decimal_places=2, max_digits=12, default=D("0.00")
)
num_applications = models.PositiveIntegerField(
_("Number of applications"), default=0
)
num_orders = models.PositiveIntegerField(_("Number of Orders"), default=0)
redirect_url = fields.ExtendedURLField(_("URL redirect (optional)"), blank=True)
date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
objects = models.Manager()
active = ActiveOfferManager()
# We need to track the voucher that this offer came from (if it is a
# voucher offer)
_voucher = None
class Meta:
abstract = True
app_label = "offer"
ordering = ["-priority", "pk"]
verbose_name = _("Conditional offer")
verbose_name_plural = _("Conditional offers")
def save(self, *args, **kwargs):
# Check to see if consumption thresholds have been broken
if not self.is_suspended:
if self.get_max_applications() == 0:
self.status = self.CONSUMED
else:
self.status = self.OPEN
return super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse("offer:detail", kwargs={"slug": self.slug})
def __str__(self):
return self.name
def clean(self):
if (
self.start_datetime
and self.end_datetime
and self.start_datetime > self.end_datetime
):
raise exceptions.ValidationError(
_("End date should be later than start date")
)
@property
def is_voucher_offer_type(self):
return self.offer_type == self.VOUCHER
@property
def is_open(self):
return self.status == self.OPEN
@property
def is_suspended(self):
return self.status == self.SUSPENDED
def suspend(self):
self.status = self.SUSPENDED
self.save()
suspend.alters_data = True
def unsuspend(self):
self.status = self.OPEN
self.save()
unsuspend.alters_data = True
def is_available(self, user=None, test_date=None):
"""
Test whether this offer is available to be used
"""
if self.is_suspended:
return False
if test_date is None:
test_date = now()
predicates = []
if self.start_datetime:
predicates.append(self.start_datetime > test_date)
if self.end_datetime:
predicates.append(test_date > self.end_datetime)
if any(predicates):
return False
return self.get_max_applications(user) > 0
def is_condition_satisfied(self, basket):
return self.condition.proxy().is_satisfied(self, basket)
def is_condition_partially_satisfied(self, basket):
return self.condition.proxy().is_partially_satisfied(self, basket)
def get_upsell_message(self, basket):
return self.condition.proxy().get_upsell_message(self, basket)
def apply_benefit(self, basket):
"""
Applies the benefit to the given basket and returns the discount.
"""
if not self.is_condition_satisfied(basket):
return ZERO_DISCOUNT
return self.benefit.proxy().apply(basket, self.condition.proxy(), self)
def apply_deferred_benefit(self, basket, order, application):
"""
Applies any deferred benefits. These are things like adding loyalty
points to someone's account.
"""
return self.benefit.proxy().apply_deferred(basket, order, application)
def set_voucher(self, voucher):
self._voucher = voucher
def get_voucher(self):
return self._voucher
def get_max_applications(self, user=None):
"""
Return the number of times this offer can be applied to a basket for a
given user.
"""
if self.max_discount and self.total_discount >= self.max_discount:
return 0
# Hard-code a maximum value as we need some sensible upper limit for
# when there are not other caps.
limits = [10000]
if self.max_user_applications and user:
limits.append(
max(
0, self.max_user_applications - self.get_num_user_applications(user)
)
)
if self.max_basket_applications:
limits.append(self.max_basket_applications)
if self.max_global_applications:
limits.append(max(0, self.max_global_applications - self.num_applications))
return min(limits)
def get_num_user_applications(self, user):
OrderDiscount = get_model("order", "OrderDiscount")
aggregates = OrderDiscount.objects.filter(
offer_id=self.id, order__user=user
).aggregate(total=models.Sum("frequency"))
return aggregates["total"] if aggregates["total"] is not None else 0
def shipping_discount(self, charge, currency=None):
return self.benefit.proxy().shipping_discount(charge, currency)
def record_usage(self, discount):
self.num_applications += discount["freq"]
self.total_discount += discount["discount"]
self.num_orders += 1
self.save()
record_usage.alters_data = True
def availability_description(self):
"""
Return a description of when this offer is available
"""
restrictions = self.availability_restrictions()
descriptions = [r["description"] for r in restrictions]
return "<br/>".join(descriptions)
def availability_restrictions(self):
restrictions = []
if self.is_suspended:
restrictions.append(
{"description": _("Offer is suspended"), "is_satisfied": False}
)
if self.max_global_applications:
remaining = self.max_global_applications - self.num_applications
desc = _("Limited to %(total)d uses (%(remainder)d remaining)") % {
"total": self.max_global_applications,
"remainder": remaining,
}
restrictions.append({"description": desc, "is_satisfied": remaining > 0})
if self.max_user_applications:
if self.max_user_applications == 1:
desc = _("Limited to 1 use per user")
else:
desc = _("Limited to %(total)d uses per user") % {
"total": self.max_user_applications
}
restrictions.append({"description": desc, "is_satisfied": True})
if self.max_basket_applications:
if self.max_user_applications == 1:
desc = _("Limited to 1 use per basket")
else:
desc = _("Limited to %(total)d uses per basket") % {
"total": self.max_basket_applications
}
restrictions.append({"description": desc, "is_satisfied": True})
def hide_time_if_zero(dt):
# Only show hours/minutes if they have been specified
if dt.tzinfo:
localtime = dt.astimezone(get_current_timezone())
else:
localtime = dt
if localtime.hour == 0 and localtime.minute == 0:
return date_filter(localtime, settings.DATE_FORMAT)
return date_filter(localtime, settings.DATETIME_FORMAT)
if self.start_datetime or self.end_datetime:
today = now()
if self.start_datetime and self.end_datetime:
desc = _("Available between %(start)s and %(end)s") % {
"start": hide_time_if_zero(self.start_datetime),
"end": hide_time_if_zero(self.end_datetime),
}
is_satisfied = self.start_datetime <= today <= self.end_datetime
elif self.start_datetime:
desc = _("Available from %(start)s") % {
"start": hide_time_if_zero(self.start_datetime)
}
is_satisfied = today >= self.start_datetime
elif self.end_datetime:
desc = _("Available until %(end)s") % {
"end": hide_time_if_zero(self.end_datetime)
}
is_satisfied = today <= self.end_datetime
restrictions.append({"description": desc, "is_satisfied": is_satisfied})
if self.max_discount:
desc = _("Limited to a cost of %(max)s") % {
"max": currency(self.max_discount)
}
restrictions.append(
{
"description": desc,
"is_satisfied": self.total_discount < self.max_discount,
}
)
return restrictions
@property
def has_products(self):
return self.condition.range is not None
def products(self):
"""
Return a queryset of products in this offer
"""
Product = get_model("catalogue", "Product")
if not self.has_products:
return Product.objects.none()
queryset = self.condition.range.all_products()
return queryset.filter(is_discountable=True).browsable()
@cached_property
def combined_offers(self):
return self.__class__.objects.filter(
models.Q(pk=self.pk)
| models.Q(pk__in=self.combinations.values_list("pk", flat=True))
| models.Q(pk__in=self.in_combination.values_list("pk", flat=True))
).distinct()
class AbstractBenefit(BaseOfferMixin, models.Model):
range = models.ForeignKey(
"offer.Range",
blank=True,
null=True,
on_delete=models.CASCADE,
verbose_name=_("Range"),
)
# Benefit types
PERCENTAGE, FIXED, FIXED_UNIT, MULTIBUY, FIXED_PRICE = (
"Percentage",
"Absolute",
"Fixed",
"Multibuy",
"Fixed price",
)
SHIPPING_PERCENTAGE, SHIPPING_ABSOLUTE, SHIPPING_FIXED_PRICE = (
"Shipping percentage",
"Shipping absolute",
"Shipping fixed price",
)
TYPE_CHOICES = (
(PERCENTAGE, _("Discount is a percentage off of the product's value")),
(FIXED, _("Discount is a fixed amount off of the basket's total")),
(FIXED_UNIT, _("Discount is a fixed amount off of the product's value")),
(MULTIBUY, _("Discount is to give the cheapest product for free")),
(FIXED_PRICE, _("Get the products that meet the condition for a fixed price")),
(SHIPPING_ABSOLUTE, _("Discount is a fixed amount of the shipping cost")),
(SHIPPING_FIXED_PRICE, _("Get shipping for a fixed price")),
(
SHIPPING_PERCENTAGE,
_("Discount is a percentage off of the shipping cost"),
),
)
type = models.CharField(_("Type"), max_length=128, choices=TYPE_CHOICES, blank=True)
# The value to use with the designated type. This can be either an integer
# (eg for multibuy) or a decimal (eg an amount) which is slightly
# confusing.
value = fields.PositiveDecimalField(
_("Value"), decimal_places=2, max_digits=12, null=True, blank=True
)
# If this is not set, then there is no upper limit on how many products
# can be discounted by this benefit.
max_affected_items = models.PositiveIntegerField(
_("Max Affected Items"),
blank=True,
null=True,
help_text=_(
"Set this to prevent the discount consuming all items "
"within the range that are in the basket."
),
)
# A custom benefit class can be used instead. This means the
# type/value/max_affected_items fields should all be None.
proxy_class = fields.NullCharField(_("Custom class"), max_length=255, default=None)
class Meta:
abstract = True
app_label = "offer"
verbose_name = _("Benefit")
verbose_name_plural = _("Benefits")
@property
def proxy_map(self):
return {
self.PERCENTAGE: get_class("offer.benefits", "PercentageDiscountBenefit"),
self.FIXED: get_class("offer.benefits", "AbsoluteDiscountBenefit"),
self.FIXED_UNIT: get_class("offer.benefits", "FixedUnitDiscountBenefit"),
self.MULTIBUY: get_class("offer.benefits", "MultibuyDiscountBenefit"),
self.FIXED_PRICE: get_class("offer.benefits", "FixedPriceBenefit"),
self.SHIPPING_ABSOLUTE: get_class(
"offer.benefits", "ShippingAbsoluteDiscountBenefit"
),
self.SHIPPING_FIXED_PRICE: get_class(
"offer.benefits", "ShippingFixedPriceBenefit"
),
self.SHIPPING_PERCENTAGE: get_class(
"offer.benefits", "ShippingPercentageDiscountBenefit"
),
}
def apply(self, basket, condition, offer):
return ZERO_DISCOUNT
def apply_deferred(self, basket, order, application):
return None
def clean(self):
if not self.type:
return
method_name = "clean_%s" % self.type.lower().replace(" ", "_")
if hasattr(self, method_name):
getattr(self, method_name)()
def clean_multibuy(self):
errors = []
if not self.range:
errors.append(_("Multibuy benefits require a product range"))
if self.value:
errors.append(_("Multibuy benefits don't require a value"))
if self.max_affected_items:
errors.append(
_("Multibuy benefits don't require a 'max affected items' attribute")
)
if errors:
raise exceptions.ValidationError(errors)
def clean_percentage(self):
errors = []
if not self.range:
errors.append(_("Percentage benefits require a product range"))
if not self.value:
errors.append(_("Percentage discount benefits require a value"))
elif self.value > 100:
errors.append(_("Percentage discount cannot be greater than 100"))
if errors:
raise exceptions.ValidationError(errors)
def clean_shipping_absolute(self):
errors = []
if not self.value:
errors.append(_("A discount value is required"))
if self.range:
errors.append(
_(
"No range should be selected as this benefit does "
"not apply to products"
)
)
if self.max_affected_items:
errors.append(
_(
"Shipping discounts don't require a "
"'max affected items' attribute"
)
)
if errors:
raise exceptions.ValidationError(errors)
def clean_shipping_percentage(self):
errors = []
if not self.value:
errors.append(_("Percentage discount benefits require a value"))
elif self.value > 100:
errors.append(_("Percentage discount cannot be greater than 100"))
if self.range:
errors.append(
_(
"No range should be selected as this benefit does "
"not apply to products"
)
)
if self.max_affected_items:
errors.append(
_(
"Shipping discounts don't require a "
"'max affected items' attribute"
)
)
if errors:
raise exceptions.ValidationError(errors)
def clean_shipping_fixed_price(self):
errors = []
if self.range:
errors.append(
_(
"No range should be selected as this benefit does "
"not apply to products"
)
)
if self.max_affected_items:
errors.append(
_(
"Shipping discounts don't require a "
"'max affected items' attribute"
)
)
if errors:
raise exceptions.ValidationError(errors)
def clean_fixed_price(self):
if self.range:
raise exceptions.ValidationError(
_(
"No range should be selected as the condition range will "
"be used instead."
)
)
def clean_absolute(self):
errors = []
if not self.range:
errors.append(_("Fixed discount benefits require a product range"))
if not self.value:
errors.append(_("Fixed discount benefits require a value"))
if errors:
raise exceptions.ValidationError(errors)
def clean_fixed(self):
errors = []
if not self.range:
errors.append(
_("Fixed product level discount benefits require a product range")
)
if not self.value:
errors.append(_("Fixed product level discount benefits require a value"))
if errors:
raise exceptions.ValidationError(errors)
def round(self, amount, currency=None):
"""
Apply rounding to discount amount
"""
rounding_function_path = getattr(
settings, "OSCAR_OFFER_ROUNDING_FUNCTION", None
)
if rounding_function_path:
rounding_function = cached_import_string(rounding_function_path)
return rounding_function(amount, currency)
return amount.quantize(D(".01"), ROUND_DOWN)
def _effective_max_affected_items(self):
"""
Return the maximum number of items that can have a discount applied
during the application of this benefit
"""
return self.max_affected_items if self.max_affected_items else 10000
def can_apply_benefit(self, line):
"""
Determines whether the benefit can be applied to a given basket line
"""
return line.stockrecord and line.product.is_discountable
# pylint: disable=W0622
def get_applicable_lines(self, offer, basket, range=None):
"""
Return the basket lines that are available to be discounted
:basket: The basket
:range: The range of products to use for filtering. The fixed-price
benefit ignores its range and uses the condition range
"""
if range is None:
range = self.range
line_tuples = []
for line in basket.all_lines():
product = line.product
if not range.contains_product(product) or not self.can_apply_benefit(line):
continue
price = unit_price(offer, line)
if not price:
# Avoid zero price products
continue
line_tuples.append((price, line))
# We sort lines to be cheapest first to ensure consistent applications
return sorted(line_tuples, key=operator.itemgetter(0))
def shipping_discount(self, charge, currency=None):
return D("0.00")
class AbstractCondition(BaseOfferMixin, models.Model):
"""
A condition for an offer to be applied. You can either specify a custom
proxy class, or need to specify a type, range and value.
"""
COUNT, VALUE, COVERAGE = ("Count", "Value", "Coverage")
TYPE_CHOICES = (
(
COUNT,
_("Depends on number of items in basket that are in condition range"),
),
(
VALUE,
_("Depends on value of items in basket that are in condition range"),
),
(
COVERAGE,
_(
"Needs to contain a set number of DISTINCT items "
"from the condition range"
),
),
)
range = models.ForeignKey(
"offer.Range",
blank=True,
null=True,
on_delete=models.CASCADE,
verbose_name=_("Range"),
)
type = models.CharField(_("Type"), max_length=128, choices=TYPE_CHOICES, blank=True)
value = fields.PositiveDecimalField(
_("Value"), decimal_places=2, max_digits=12, null=True, blank=True
)
proxy_class = fields.NullCharField(_("Custom class"), max_length=255, default=None)
class Meta:
abstract = True
app_label = "offer"
verbose_name = _("Condition")
verbose_name_plural = _("Conditions")
@property
def proxy_map(self):
return {
self.COUNT: get_class("offer.conditions", "CountCondition"),
self.VALUE: get_class("offer.conditions", "ValueCondition"),
self.COVERAGE: get_class("offer.conditions", "CoverageCondition"),
}
def clean(self):
# The form will validate whether this is ok or not.
if not self.type:
return
method_name = "clean_%s" % self.type.lower()
if hasattr(self, method_name):
getattr(self, method_name)()
def clean_count(self):
errors = []
if not self.range:
errors.append(_("Count conditions require a product range"))
if not self.value:
errors.append(_("Count conditions require a value"))
if errors:
raise exceptions.ValidationError(errors)
def clean_value(self):
errors = []
if not self.range:
errors.append(_("Value conditions require a product range"))
if not self.value:
errors.append(_("Value conditions require a value"))
if errors:
raise exceptions.ValidationError(errors)
def clean_coverage(self):
errors = []
if not self.range:
errors.append(_("Coverage conditions require a product range"))
if not self.value:
errors.append(_("Coverage conditions require a value"))
if errors:
raise exceptions.ValidationError(errors)
def consume_items(self, offer, basket, affected_lines):
pass
def is_satisfied(self, offer, basket):
"""
Determines whether a given basket meets this condition. This is
stubbed in this top-class object. The subclassing proxies are
responsible for implementing it correctly.
"""
return False
def is_partially_satisfied(self, offer, basket):
"""
Determine if the basket partially meets the condition. This is useful
for up-selling messages to entice customers to buy something more in
order to qualify for an offer.
"""
return False
def get_upsell_message(self, offer, basket):
return None
def can_apply_condition(self, line):
"""
Determines whether the condition can be applied to a given basket line
"""
if not line.stockrecord_id:
return False
product = line.product
return self.range.contains_product(product) and product.is_discountable
def get_applicable_lines(self, offer, basket, most_expensive_first=True):
"""
Return line data for the lines that can be consumed by this condition
"""
line_tuples = []
for line in basket.all_lines():
if not self.can_apply_condition(line):
continue
price = unit_price(offer, line)
if not price:
continue
line_tuples.append((price, line))
key = operator.itemgetter(0)
if most_expensive_first:
return sorted(line_tuples, reverse=True, key=key)
return sorted(line_tuples, key=key)
class AbstractRange(models.Model):
"""
Represents a range of products that can be used within an offer.
"""
name = models.CharField(_("Name"), max_length=128, unique=True)
slug = fields.AutoSlugField(
_("Slug"), max_length=128, unique=True, populate_from="name"
)
description = models.TextField(_("Description"), blank=True)
# Whether this range is public
is_public = models.BooleanField(
_("Is public?"),
default=False,
help_text=_("Public ranges have a customer-facing page"),
)
includes_all_products = models.BooleanField(
_("Includes all products?"), default=False
)
included_products = models.ManyToManyField(
"catalogue.Product",
related_name="includes",
blank=True,
verbose_name=_("Included Products"),
through="offer.RangeProduct",
)
excluded_products = models.ManyToManyField(
"catalogue.Product",
related_name="excludes",
blank=True,
verbose_name=_("Excluded Products"),
)
classes = models.ManyToManyField(
"catalogue.ProductClass",
related_name="classes",
blank=True,
verbose_name=_("Product Types"),
)
included_categories = models.ManyToManyField(