-
Notifications
You must be signed in to change notification settings - Fork 26
/
forms.py
75 lines (61 loc) · 2.34 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
73
74
75
# -*- coding: utf-8 -*-
"""
forms.py
:copyright: (c) 2014 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
from flask_wtf import Form
from wtforms import TextField, TextAreaField, SelectField, DecimalField, \
validators
from wtforms.validators import ValidationError
from nereid import abort
from trytond.pool import Pool
class GiftCardForm(Form):
"""
A form for purchasing gift cards
"""
recipient_name = TextField('Recipient Name', [validators.Optional()])
recipient_email = TextField('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 %s and %s is allowed." % (
form.gc_product.gc_min, form.gc_product.gc_max
)
)