/
models.py
778 lines (649 loc) · 24.2 KB
/
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
# python imports
import datetime
# django imports
from django.conf import settings
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.encoding import force_unicode
from django.utils.translation import ugettext_lazy as _, ugettext
from django.template import RequestContext
from django.template.loader import render_to_string
# lfs imports
import lfs.cart.utils
import lfs.core.utils
from lfs import shipping
from lfs.core.models import Country
from lfs.payment.models import PaymentMethod
from lfs.shipping.models import ShippingMethod
class Criterion(models.Model):
"""
Base class for all criteria.
**Attributes:**
cart
The current cart of the current customer.
content
The content object the criterion belongs to.
operator
The current selected operator for the criterion.
position
The position of the criterion within a list of criteria of the
content object.
product
The product, if the criterion is called from a product detail view.
Otherwise this is None.
request
The current request.
**Constants:**
EQUAL, LESS_THAN, LESS_THAN_EQUAL, GREATER_THAN, GREATER_THAN_EQUAL, IS_SELECTED, IS_NOT_SELECTED, IS_VALID, IS_NOT_VALID, CONTAINS
Integers which represents certain operators.
INPUT, SELECT, MULTIPLE_SELECT
Constants which represents the types of selectable values. One of
these must be returned from ``get_value_type``.
NUMBER_OPERATORS
A list of operators which can be returned from ``get_operators``.
.. code-block:: python
[
[EQUAL, _(u"Equal to")],
[LESS_THAN, _(u"Less than")],
[LESS_THAN_EQUAL, _(u"Less than equal to")],
[GREATER_THAN, _(u"Greater than")],
[GREATER_THAN_EQUAL, _(u"Greater than equal to")],
]
SELECTION_OPERATORS
A list of operators which can be returned from ``get_operators``.
.. code-block:: python
[
[IS_SELECTED, _(u"Is selected")],
[IS_NOT_SELECTED, _(u"Is not selected")],
]
VALID_OPERATORS
A list of operators which can be returned from ``get_operators``.
.. code-block:: python
[
[IS_VALID, _(u"Is valid")],
[IS_NOT_VALID, _(u"Is not valid")],
]
STRING_OPERATORS
A list of operators which can be return from ``get_operators``.
.. code-block:: python
[
[EQUAL, _(u"Equal to")],
[CONTAINS, _(u"Contains")],
]
"""
content_type = models.ForeignKey(ContentType, verbose_name=_(u"Content type"), related_name="content_type")
content_id = models.PositiveIntegerField(_(u"Content id"))
content = generic.GenericForeignKey(ct_field="content_type", fk_field="content_id")
sub_type = models.CharField(_(u"Sub type"), max_length=100, blank=True)
position = models.PositiveIntegerField(_(u"Position"), default=999)
operator = models.PositiveIntegerField(_(u"Operator"), blank=True, null=True)
class Meta:
ordering = ("position", )
EQUAL = 0
LESS_THAN = 1
LESS_THAN_EQUAL = 2
GREATER_THAN = 3
GREATER_THAN_EQUAL = 4
IS_SELECTED = 10
IS_NOT_SELECTED = 11
IS_VALID = 21
IS_NOT_VALID = 22
CONTAINS = 32
INPUT = 0
SELECT = 1
MULTIPLE_SELECT = 2
NUMBER_OPERATORS = [
[EQUAL, _(u"Equal to")],
[LESS_THAN, _(u"Less than")],
[LESS_THAN_EQUAL, _(u"Less than equal to")],
[GREATER_THAN, _(u"Greater than")],
[GREATER_THAN_EQUAL, _(u"Greater than equal to")],
]
SELECTION_OPERATORS = [
[IS_SELECTED, _(u"Is selected")],
[IS_NOT_SELECTED, _(u"Is not selected")],
]
VALID_OPERATORS = [
[IS_VALID, _(u"Is valid")],
[IS_NOT_VALID, _(u"Is not valid")],
]
STRING_OPERATORS = [
[EQUAL, _(u"Equal to")],
[CONTAINS, _(u"Contains")],
]
def __unicode__(self):
""" We're using force unicode as this basically fails:
from django.utils import translation
from django.utils.translation import ugettext_lazy as _
translation.activate('pl')
u'test: %s' % _('Payment method')
"""
return ugettext("%(name)s: %(operator)s %(value)s") % {
'name': force_unicode(self.get_name()),
'operator': force_unicode(self.get_current_operator_as_string()),
'value': force_unicode(self.get_value_as_string())
}
def save(self, *args, **kwargs):
if self.sub_type == "":
self.sub_type = self.__class__.__name__.lower()
super(Criterion, self).save(*args, **kwargs)
@property
def cart(self):
"""
Returns the current cart of the current customer.
"""
return lfs.cart.utils.get_cart(self.request)
def get_content_object(self):
"""
Returns the specific content object of the criterion.
This can be call on Criterion instances to get the specific criterion
instance.
"""
if self.__class__.__name__.lower() == "criterion":
return getattr(self, self.sub_type)
else:
return self
def get_current_operator_as_string(self):
"""
Returns the current operator as string.
"""
for operator in self.get_operators():
if self.operator == operator[0]:
return operator[1]
def get_name(self):
"""
Returns the name of the criterion as string.
"""
klass = "%s.%s" % (self.__class__.__module__, self.__class__.__name__)
for x in settings.LFS_CRITERIA:
if x[0] == klass:
return x[1]
return self.__class__.__name__
def get_operators(self):
"""
Returns the selectable operators of the criterion which are displayed to
the shop manager. This is a list of list, whereas the the first value is
integer, which is stored within the criterion and the second value is
the string which is displayed to the shop manager, e.g.:
.. code-block:: python
[
[0, _(u"Equal to")],
[1, _(u"Less than")],
[2, _(u"Less than equal to")],
[3, _(u"Greater than")],
[4, _(u"Greater than equal to")],
]
.. note::
You can use one of the provided class attributes, see above.
* NUMBER_OPERATORS
* SELECTION_OPERATORS
* VALID_OPERATORS
* STRING_OPERATORS
"""
raise NotImplementedError
def get_selectable_values(self, request):
"""
Returns the selectable values as a list of dictionary:
[
{
"id": 0,
"name": "Name 0",
"selected": False,
},
{
"id": 1,
"name": "Name 1",
"selected": True,
},
]
"""
return []
def get_template(self, request):
"""
Returns the template to render the criterion.
"""
return "manage/criteria/base.html"
def get_value(self):
"""
Returns the current value of the criterion.
"""
return self.value
def get_value_type(self):
"""
Returns the type of the selectable values field. Must return one of:
* self.INPUT
* self.SELECT
* self.MULTIPLE_SELECT
"""
return self.INPUT
def get_value_as_string(self):
"""
Returns the current value of the criterion as string.
"""
value = self.get_value()
if value.__class__.__name__ == "ManyRelatedManager":
values = []
for value in self.get_value().all():
values.append(value.name)
return ", ".join(values)
else:
return value
def is_valid(self):
"""
Returns ``True`` if the criterion is valid otherwise ``False``.
"""
raise NotImplementedError
def render(self, request, position):
"""
Renders the criterion as html in order to displayed it within the
management form.
"""
operators = []
for operator in self.get_operators():
if self.operator == operator[0]:
selected = True
else:
selected = False
operators.append({
"id": operator[0],
"name": operator[1],
"selected": selected,
})
criteria = []
for criterion in settings.LFS_CRITERIA:
klass = criterion[0].split(".")[-1]
if self.__class__.__name__ == klass:
selected = True
else:
selected = False
criteria.append({
"module": criterion[0],
"name": criterion[1],
"selected": selected,
})
if self.id:
id = "ex%s" % self.id
else:
id = datetime.datetime.now().microsecond
return render_to_string(self.get_template(request), RequestContext(request, {
"id": id,
"operator": self.operator,
"value": self.get_value(),
"position": position,
"operators": operators,
"criteria": criteria,
"selectable_values": self.get_selectable_values(request),
"value_type": self.get_value_type(),
"criterion": self,
}))
def update(self, value):
"""
Updates the value of the criterion.
**Parameters:**
value
The value the shop user has entered for the criterion.
"""
if isinstance(self.value, float):
try:
value = float(value)
except (ValueError, TypeError):
value = 0.0
self.value = value
elif isinstance(self.value, int):
try:
value = int(value)
except (ValueError, TypeError):
value = 0
self.value = value
elif self.value.__class__.__name__ == "ManyRelatedManager":
for value_id in value:
self.value.add(value_id)
else:
self.value = value
self.save()
class CartPriceCriterion(Criterion):
"""
Criterion to check against cart/product price.
"""
value = models.FloatField(_(u"Price"), default=0.0)
def get_operators(self):
"""
Returns the available operators for the criterion.
"""
return self.NUMBER_OPERATORS
def is_valid(self):
"""
If product is given, the price is taken from the product, otherwise it
is the total price of all products within the cart.
"""
if self.product:
price = self.product.get_price(self.request)
elif self.cart:
price = self.cart.get_price_gross(self.request)
else:
price = 0
if (self.operator == self.EQUAL) and (price == self.value):
return True
elif (self.operator == self.LESS_THAN) and (price < self.value):
return True
elif (self.operator == self.LESS_THAN_EQUAL) and (price <= self.value):
return True
elif (self.operator == self.GREATER_THAN) and (price > self.value):
return True
elif (self.operator == self.GREATER_THAN_EQUAL) and (price >= self.value):
return True
else:
return False
class CombinedLengthAndGirthCriterion(Criterion):
"""
Criterion to check against combined length and girth.
"""
value = models.FloatField(_(u"CLAG"), default=0.0)
def get_operators(self):
"""
Returns the available operators for the criterion.
"""
return self.NUMBER_OPERATORS
def is_valid(self):
"""
If product is given, the clag is taken from the product, otherwise it is
the clag of all products within the cart.
"""
if self.product:
clag = (2 * self.product.get_width()) + (2 * self.product.get_height()) + self.product.get_length()
else:
if self.cart is None:
clag = 0
else:
max_width = 0
max_length = 0
total_height = 0
for item in cart.get_items():
if max_length < item.product.get_length():
max_length = item.product.get_length()
if max_width < item.product.get_width():
max_width = item.product.get_width()
total_height += item.product.get_height()
clag = (2 * max_width) + (2 * total_height) + max_length
if (self.operator == self.EQUAL) and (clag == self.value):
return True
elif (self.operator == self.LESS_THAN) and (clag < self.value):
return True
elif (self.operator == self.LESS_THAN_EQUAL) and (clag <= self.value):
return True
elif (self.operator == self.GREATER_THAN) and (clag > self.value):
return True
elif (self.operator == self.GREATER_THAN_EQUAL) and (clag >= self.value):
return True
else:
return False
class CountryCriterion(Criterion):
"""
Criterion to check against shipping country.
"""
value = models.ManyToManyField(Country, verbose_name=_(u"Countries"))
def get_operators(self):
"""
Returns the available operators for the criterion.
"""
return self.SELECTION_OPERATORS
def get_selectable_values(self, request):
shop = lfs.core.utils.get_default_shop(request)
countries = []
for country in shop.shipping_countries.all():
if country in self.value.all():
selected = True
else:
selected = False
countries.append({
"id": country.id,
"name": country.name,
"selected": selected,
})
return countries
def get_value_type(self):
return self.MULTIPLE_SELECT
def is_valid(self):
country = shipping.utils.get_selected_shipping_country(self.request)
if self.operator == self.IS_SELECTED:
return country in self.value.all()
else:
return country not in self.value.all()
class HeightCriterion(Criterion):
"""
Criterion to check against product's height / cart's total height.
"""
value = models.FloatField(_(u"Height"), default=0.0)
def get_operators(self):
"""
Returns the available operators for the criterion.
"""
return self.NUMBER_OPERATORS
def is_valid(self):
"""
If product is given, the height is taken from the product, otherwise it
is the total height of all products within the cart.
"""
if self.product:
height = self.product.get_height()
elif self.cart:
sum([item.product.get_height() * item.amount for item in cart.get_items()])
else:
height = 0
if (self.operator == self.EQUAL) and (height == self.value):
return True
elif (self.operator == self.LESS_THAN) and (height < self.value):
return True
elif (self.operator == self.LESS_THAN_EQUAL) and (height <= self.value):
return True
elif (self.operator == self.GREATER_THAN) and (height > self.value):
return True
elif (self.operator == self.GREATER_THAN_EQUAL) and (height >= self.value):
return True
else:
return False
class LengthCriterion(Criterion):
"""
Criterion to check against product's length / cart's max length.
"""
value = models.FloatField(_(u"Length"), default=0.0)
def get_operators(self):
"""
Returns the available operators for the criterion.
"""
return self.NUMBER_OPERATORS
def is_valid(self):
"""
If product is given, the length is taken from the product otherwise it
is the max length of all products within the cart.
"""
if self.product:
max_length = self.product.get_length()
elif self.cart:
max_length = max([item.product.get_length() for item in self.cart.get_items()])
else:
max_length = 0
if (self.operator == self.LESS_THAN) and (max_length < self.value):
return True
elif (self.operator == self.LESS_THAN_EQUAL) and (max_length <= self.value):
return True
elif (self.operator == self.GREATER_THAN) and (max_length > self.value):
return True
elif (self.operator == self.GREATER_THAN_EQUAL) and (max_length >= self.value):
return True
elif (self.operator == self.EQUAL) and (max_length == self.value):
return True
else:
return False
class PaymentMethodCriterion(Criterion):
"""
Criterion to check against payment methods.
"""
value = models.ManyToManyField(PaymentMethod, verbose_name=_(u"Payment methods"))
def get_operators(self):
"""
Returns the available operators for the criterion.
"""
return self.SELECTION_OPERATORS + self.VALID_OPERATORS
def get_selectable_values(self, request):
selected_payment_methods = self.value.all()
payment_methods = []
for pm in PaymentMethod.objects.filter(active=True):
if pm in selected_payment_methods:
selected = True
else:
selected = False
payment_methods.append({
"id": pm.id,
"name": pm.name,
"selected": selected,
})
return payment_methods
def get_value_type(self):
return self.MULTIPLE_SELECT
def is_valid(self):
# see ShippingMethodCriterion for what's going on here
import lfs.shipping.utils
if isinstance(self.content, PaymentMethod):
is_payment_method = True
else:
is_payment_method = False
if (not is_payment_method) and (self.operator == self.IS_SELECTED):
payment_method = lfs.payment.utils.get_selected_payment_method(self.request)
return payment_method in self.value.all()
elif (not is_payment_method) and (self.operator == self.IS_NOT_SELECTED):
payment_method = lfs.payment.utils.get_selected_payment_method(self.request)
return payment_method not in self.value.all()
elif self.operator == self.IS_VALID:
for pm in self.value.all():
if not lfs.criteria.utils.is_valid(self.request, pm, self.product):
return False
return True
elif self.operator == self.IS_NOT_VALID:
for pm in self.value.all():
if lfs.criteria.utils.is_valid(self.request, pm, self.product):
return False
return True
else:
return False
class ShippingMethodCriterion(Criterion):
"""
Criterion to check against shipping methods.
"""
value = models.ManyToManyField(ShippingMethod, verbose_name=_(u"Shipping methods"))
def get_operators(self):
"""
Returns the available operators for the criterion.
"""
return self.SELECTION_OPERATORS + self.VALID_OPERATORS
def get_selectable_values(self, request):
selected_shipping_methods = self.value.all()
shipping_methods = []
for sm in ShippingMethod.objects.filter(active=True):
if sm in selected_shipping_methods:
selected = True
else:
selected = False
shipping_methods.append({
"id": sm.id,
"name": sm.name,
"selected": selected,
})
return shipping_methods
def get_value_type(self):
return self.MULTIPLE_SELECT
def is_valid(self):
# Check whether the criteria is used of a shipping method. If so the
# operator IS_SELECTED and IS_NOT_SELECTED are not allowed. The reason
# why we have to check this is that the get_selected_shipping_method
# checks for valid shipping methods and call this method again, so that
# we get an infinte recursion.
import lfs.shipping.utils
if isinstance(self.content, ShippingMethod):
is_shipping_method = True
else:
is_shipping_method = False
if (not is_shipping_method) and (self.operator == self.IS_SELECTED):
shipping_method = lfs.shipping.utils.get_selected_shipping_method(self.request)
return shipping_method in self.value.all()
elif (not is_shipping_method) and (self.operator == self.IS_NOT_SELECTED):
shipping_method = lfs.shipping.utils.get_selected_shipping_method(self.request)
return shipping_method not in self.value.all()
elif self.operator == self.IS_VALID:
for sm in self.value.all():
if not lfs.criteria.utils.is_valid(self.request, sm, self.product):
return False
return True
elif self.operator == self.IS_NOT_VALID:
for sm in self.value.all():
if lfs.criteria.utils.is_valid(self.request, sm, self.product):
return False
return True
else:
return False
class WeightCriterion(Criterion):
"""
Criterion to check against product's weight / cart's total weight.
"""
value = models.FloatField(_(u"Weight"), default=0.0)
def get_operators(self):
"""
Returns the available operators for the criterion.
"""
return self.NUMBER_OPERATORS
def is_valid(self):
"""
If product is given, the weigth is taken from the product, otherwise it
is the total weight of all products within the cart.
"""
if self.product:
weight = self.product.get_weight()
elif self.cart:
weight = sum([item.product.get_weight() * item.amount for item in cart.get_items()])
else:
weight = 0
if (self.operator == self.LESS_THAN) and (weight < self.value):
return True
elif (self.operator == self.LESS_THAN_EQUAL) and (weight <= self.value):
return True
elif (self.operator == self.GREATER_THAN) and (weight > self.value):
return True
elif (self.operator == self.GREATER_THAN_EQUAL) and (weight >= self.value):
return True
elif (self.operator == self.EQUAL) and (weight == self.value):
return True
else:
return False
class WidthCriterion(Criterion):
"""
Criterion to check against product's width / cart's max width.
"""
value = models.FloatField(_(u"Width"), default=0.0)
def get_operators(self):
"""
Returns the available operators for the criterion.
"""
return self.NUMBER_OPERATORS
def is_valid(self):
"""
If product is given, the width is taken from the product, otherwise it
is the max width of all products within the cart.
"""
if self.product:
max_width = self.product.get_width()
elif self.cart:
max_width = max([item.product.get_width() for item in self.cart.get_items()])
else:
max_width = 0
if self.operator == self.LESS_THAN and (max_width < self.width):
return True
if self.operator == self.LESS_THAN_EQUAL and (max_width <= self.width):
return True
if self.operator == self.GREATER_THAN and (max_width > self.width):
return True
if self.operator == self.GREATER_THAN_EQUAL and (max_width >= self.width):
return True
if self.operator == self.EQUAL and (max_width == self.width):
return True
return False