-
Notifications
You must be signed in to change notification settings - Fork 2
/
discount.py
60 lines (49 loc) · 1.79 KB
/
discount.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
from django import forms
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from plata.discount.models import DiscountBase
def explicit_products(product_model):
"""
Explicitly define products eligible for discounting
Example::
from ... import Product
explicit_products(Product)
"""
DiscountBase.CONFIG_OPTIONS.append(('products', {
'title': _('Explicitly define discountable products'),
'form_fields': [
('products', forms.ModelMultipleChoiceField(
product_model._default_manager.all(),
label=_('products'),
required=True,
widget=FilteredSelectMultiple(
verbose_name=_('products'),
is_stacked=False,
),
)),
],
'product_query': lambda products: Q(product__in=products),
}))
def only_categories(category_model):
"""
Define categories whose products are eligible for discounting
Example::
from ... import Product
only_categories(Product)
"""
DiscountBase.CONFIG_OPTIONS.append(('only_categories', {
'title': _('Only products from selected categories'),
'form_fields': [
('categories', forms.ModelMultipleChoiceField(
category_model._default_manager.all(),
label=_('categories'),
required=True,
widget=FilteredSelectMultiple(
verbose_name=_('categories'),
is_stacked=False,
),
)),
],
'product_query': lambda categories: Q(product__categories__in=categories),
}))