forked from trytonus/nereid-webshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
72 lines (59 loc) · 2.43 KB
/
forms.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
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from flask_wtf import FlaskForm as Form
from nereid import abort
from nereid.contrib.locale import make_lazy_gettext
from trytond.pool import Pool
from wtforms import (
DecimalField, SelectField, StringField, TextAreaField, validators)
from wtforms.validators import ValidationError
_ = make_lazy_gettext('nereid_webshop')
class GiftCardForm(Form):
"""
A form for purchasing gift cards
"""
recipient_name = StringField('Recipient Name', [validators.Optional()])
recipient_email = StringField('Recipient Email')
message = TextAreaField('Message', [validators.Optional()])
selected_amount = SelectField('Select Amount', choices=[], coerce=int)
open_amount = DecimalField('Amount', default=0)
def __init__(self, product, *args, **kwargs):
super(GiftCardForm, self).__init__(*args, **kwargs)
Product = Pool().get('product.product')
if not isinstance(product, Product):
abort(400)
try:
self.gc_product, = Product.search([
('id', '=', product.id),
('is_gift_card', '=', True)
], limit=1)
except ValueError as e:
e.message = 'Expected Gift Card, Got %s' % (product.rec_name)
raise
self.fill_choices()
if self.gc_product.gift_card_delivery_mode in ['virtual', 'combined']:
self.recipient_email.validators = [
validators.DataRequired(), validators.Email()
]
else:
self.recipient_email.validators = [
validators.Optional(), validators.Email()
]
def fill_choices(self):
choices = []
if self.gc_product.allow_open_amount:
choices = [(0, _('Set my Own'))]
self.selected_amount.choices = choices + [
(p.id, p.price) for p in self.gc_product.gift_card_prices
]
def validate_open_amount(form, field):
if not form.gc_product.allow_open_amount:
return
if (form.selected_amount.data == 0) and not (
form.gc_product.gc_min <= field.data <= form.gc_product.gc_max
):
raise ValidationError(
_('Amount between {gc_min} and {gc_max} is allowed.').format(
gc_min=form.gc_product.gc_min, gc_max=form.gc_product.gc_max
)
)