Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Integrates the pricing grammar with the form pricing attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed May 30, 2017
1 parent 5999c39 commit 209891b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
13 changes: 10 additions & 3 deletions onegov/form/parser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,25 +549,31 @@ def handle_block(builder, block, dependency=None):
checked = [c.label for c in field.choices if c.checked]
default = checked and checked[0] or None

pricing = {c.label: c.pricing for c in field.choices if c.pricing}

field_id = builder.add_field(
field_class=RadioField,
label=identifier.label,
dependency=dependency,
required=identifier.required,
choices=choices,
default=default
default=default,
pricing=pricing or None
)
elif field.type == 'checkbox':
choices = [(c.label, c.label) for c in field.choices]
default = [c.label for c in field.choices if c.checked]

pricing = {c.label: c.pricing for c in field.choices if c.pricing}

field_id = builder.add_field(
field_class=MultiCheckboxField,
label=identifier.label,
dependency=dependency,
required=identifier.required,
choices=choices,
default=default
default=default,
pricing=pricing or None
)
else:
raise NotImplementedError
Expand Down Expand Up @@ -785,7 +791,7 @@ def get_unique_field_id(self, label, dependency):
return field_id

def add_field(self, field_class, label, required,
dependency=None, field_id=None, **kwargs):
dependency=None, field_id=None, pricing=None, **kwargs):
validators = kwargs.pop('validators', [])

# labels in wtforms are not escaped correctly - for safety we make sure
Expand All @@ -800,6 +806,7 @@ def add_field(self, field_class, label, required,
label=label,
validators=validators,
fieldset=self.current_fieldset,
pricing=pricing,
**kwargs
))

Expand Down
4 changes: 2 additions & 2 deletions onegov/form/parser/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ def marker_box(characters):

check = mark_enclosed_in(characters)('checked')
label = with_whitespace_inside(text_without(characters + '()'))('label')
price = enclosed_in(decimal() + currency(), '()')('price')
pricing = enclosed_in(decimal() + currency(), '()')('pricing')

return check + label + Optional(price)
return check + label + Optional(pricing)


def radio():
Expand Down
20 changes: 10 additions & 10 deletions onegov/form/tests/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,38 +193,38 @@ def test_prices():
assert f.type == 'radio'
assert f.label == 'Default Choice'
assert not f.checked
assert f.price.decimal == Decimal('100.00')
assert f.price.currency == 'CHF'
assert f.pricing.decimal == Decimal('100.00')
assert f.pricing.currency == 'CHF'

f = field.parseString("(x) Luxurious Choice (200 CHF)")
assert f.type == 'radio'
assert f.label == 'Luxurious Choice'
assert f.checked
assert f.price.decimal == Decimal('200.00')
assert f.price.currency == 'CHF'
assert f.pricing.decimal == Decimal('200.00')
assert f.pricing.currency == 'CHF'

field = checkbox()

f = field.parseString("[x] Extra Luggage (150.50 USD)")
assert f.type == 'checkbox'
assert f.label == 'Extra Luggage'
assert f.checked
assert f.price.decimal == Decimal('150.50')
assert f.price.currency == 'USD'
assert f.pricing.decimal == Decimal('150.50')
assert f.pricing.currency == 'USD'

f = field.parseString("[ ] Priority Boarding (15.00 USD)")
assert f.type == 'checkbox'
assert f.label == 'Priority Boarding'
assert not f.checked
assert f.price.decimal == Decimal('15.00')
assert f.price.currency == 'USD'
assert f.pricing.decimal == Decimal('15.00')
assert f.pricing.currency == 'USD'

f = field.parseString("[ ] Discount (-5.00 USD)")
assert f.type == 'checkbox'
assert f.label == 'Discount'
assert not f.checked
assert f.price.decimal == Decimal('-5.00')
assert f.price.currency == 'USD'
assert f.pricing.decimal == Decimal('-5.00')
assert f.pricing.currency == 'USD'


def test_decimal():
Expand Down
31 changes: 31 additions & 0 deletions onegov/form/tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from decimal import Decimal
from onegov.form import Form, errors
from onegov.form.parser import parse_form
from textwrap import dedent
Expand Down Expand Up @@ -229,6 +230,36 @@ def test_parse_checkbox():
assert form.extras.default == ['Priority Seating', 'Early Boarding']


def test_parse_radio_with_pricing():

text = dedent("""
Drink =
( ) Coffee (2.50 CHF)
(x) Tea (1.50 CHF)
""")

form = parse_form(text)()
assert form.drink.pricing.rules == {
'Coffee': (Decimal(2.5), 'CHF'),
'Tea': (Decimal(1.5), 'CHF')
}


def test_parse_checkbox_with_pricing():

text = dedent("""
Extras =
[ ] Bacon (2.50 CHF)
[x] Cheese (1.50 CHF)
""")

form = parse_form(text)()
assert form.extras.pricing.rules == {
'Bacon': (Decimal(2.5), 'CHF'),
'Cheese': (Decimal(1.5), 'CHF')
}


def test_dependent_validation():

text = dedent("""
Expand Down

0 comments on commit 209891b

Please sign in to comment.