|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
from __future__ import unicode_literals |
|
3
|
|
|
|
|
4
|
|
|
from django import forms |
|
5
|
|
|
from django.db.models import Q |
|
6
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
7
|
|
|
|
|
8
|
|
|
from reservable_pricing.models import ReservableProductPrice |
|
9
|
|
|
from shoop.admin.form_part import FormPart, TemplatedFormDef |
|
10
|
|
|
from shoop.core.models import ContactGroup, Shop |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class ReservablePricingForm(forms.Form): |
|
14
|
|
|
def __init__(self, **kwargs): |
|
15
|
|
|
self.product = kwargs.pop("product", None) |
|
16
|
|
|
super(ReservablePricingForm, self).__init__(**kwargs) |
|
17
|
|
|
self.shops = [] |
|
18
|
|
|
self.groups = [] |
|
19
|
|
|
if self.product: |
|
20
|
|
|
self._build_fields() |
|
21
|
|
|
|
|
22
|
|
|
def _build_fields(self): |
|
23
|
|
|
self.shops = list(Shop.objects.all()) |
|
24
|
|
|
self.groups = list(ContactGroup.objects.filter( |
|
25
|
|
|
Q(show_pricing=True) | |
|
26
|
|
|
Q( |
|
27
|
|
|
id__in=ReservableProductPrice.objects.filter(product=self.product) |
|
|
|
|
|
|
28
|
|
|
.values_list("group_id", flat=True).distinct() |
|
29
|
|
|
) |
|
30
|
|
|
)) |
|
31
|
|
|
prices_by_shop_and_group = dict( |
|
32
|
|
|
((shop_id or 0, group_id or 0), price) |
|
33
|
|
|
for (shop_id, group_id, price) |
|
34
|
|
|
in ReservableProductPrice.objects.filter(product=self.product) |
|
|
|
|
|
|
35
|
|
|
.values_list("shop_id", "group_id", "price_value") |
|
36
|
|
|
) |
|
37
|
|
|
|
|
38
|
|
|
for group in self.groups: |
|
39
|
|
|
for shop in self.shops: |
|
40
|
|
|
shop_group_id_tuple = self._get_id_tuple(shop, group) |
|
41
|
|
|
name = self._get_field_name(shop_group_id_tuple) |
|
42
|
|
|
price = prices_by_shop_and_group.get(shop_group_id_tuple) |
|
43
|
|
|
price_field = forms.DecimalField( |
|
44
|
|
|
min_value=0, initial=price, |
|
45
|
|
|
label=_("Price (%s/%s)") % (shop, group), required=False |
|
46
|
|
|
) |
|
47
|
|
|
self.fields[name] = price_field |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def _get_id_tuple(self, shop, group): |
|
50
|
|
|
return ( |
|
51
|
|
|
shop.id if shop else 0, |
|
52
|
|
|
group.id if group else 0 |
|
53
|
|
|
) |
|
54
|
|
|
|
|
55
|
|
|
def _get_field_name(self, id_tuple): |
|
56
|
|
|
return "s_%d_g_%d" % id_tuple |
|
57
|
|
|
|
|
58
|
|
|
def _process_single_save(self, shop, group): |
|
59
|
|
|
shop_group_id_tuple = self._get_id_tuple(shop, group) |
|
60
|
|
|
name = self._get_field_name(shop_group_id_tuple) |
|
61
|
|
|
value = self.cleaned_data.get(name) |
|
|
|
|
|
|
62
|
|
|
clear = (value is None or value < 0) |
|
63
|
|
|
if clear: |
|
64
|
|
|
ReservableProductPrice.objects.filter(product=self.product, group=group, shop=shop).delete() |
|
|
|
|
|
|
65
|
|
|
else: |
|
66
|
|
|
(spp, created) = ReservableProductPrice.objects.get_or_create( |
|
|
|
|
|
|
67
|
|
|
product=self.product, group=group, shop=shop, |
|
68
|
|
|
defaults={'price_value': value}) |
|
69
|
|
|
if not created: |
|
70
|
|
|
spp.price_value = value |
|
71
|
|
|
spp.save() |
|
72
|
|
|
|
|
73
|
|
|
def save(self): |
|
74
|
|
|
if not self.has_changed(): # No changes, so no need to do anything. |
|
|
|
|
|
|
75
|
|
|
# (This is required because `.full_clean()` would create an empty `.cleaned_data`, |
|
76
|
|
|
# but short-circuits out if `has_changed()` returns false. |
|
77
|
|
|
# That, in kind, would cause `self.cleaned_data.get(name)` in `_process_single_save` |
|
78
|
|
|
# to return Nones, clearing out all prices. Oops.) |
|
79
|
|
|
return |
|
80
|
|
|
|
|
81
|
|
|
for group in self.groups: |
|
82
|
|
|
for shop in self.shops: |
|
83
|
|
|
self._process_single_save(shop, group) |
|
84
|
|
|
|
|
85
|
|
|
def get_shop_group_field(self, shop, group): |
|
86
|
|
|
shop_group_id_tuple = self._get_id_tuple(shop, group) |
|
87
|
|
|
name = self._get_field_name(shop_group_id_tuple) |
|
88
|
|
|
return self[name] |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
class ReservablePricingFormPart(FormPart): |
|
92
|
|
|
priority = 10 |
|
93
|
|
|
|
|
94
|
|
|
def get_form_defs(self): |
|
95
|
|
|
yield TemplatedFormDef( |
|
96
|
|
|
name="reservable_pricing", |
|
97
|
|
|
form_class=ReservablePricingForm, |
|
98
|
|
|
template_name="reservable_pricing/admin/form_part.jinja", |
|
99
|
|
|
required=False, |
|
100
|
|
|
kwargs={"product": self.object} |
|
|
|
|
|
|
101
|
|
|
) |
|
102
|
|
|
|
|
103
|
|
|
def form_valid(self, form): |
|
104
|
|
|
form["reservable_pricing"].save() |
|
105
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.