Browse Source

added custom money input schema

php2python
Holger Frey 7 years ago
parent
commit
8a2c072c35
  1. 32
      ordr2/schemas/__init__.py
  2. 11
      ordr2/schemas/orders.py
  3. 4
      ordr2/static/css/style.css
  4. 36
      ordr2/templates/deform/money_mapping.pt
  5. 36
      ordr2/templates/deform/money_mapping_disabled.pt
  6. 25
      ordr2/templates/deform/money_mapping_item.pt
  7. 25
      ordr2/templates/deform/money_mapping_item_diabled.pt
  8. 4
      ordr2/templates/deform/textinput_disabled.pt
  9. 14
      ordr2/views/admin.py

32
ordr2/schemas/__init__.py

@ -32,3 +32,35 @@ class CSRFSchema(colander.Schema):
form = deform.Form(schema, action=url, **kwargs) form = deform.Form(schema, action=url, **kwargs)
return form return form
class MoneyInputSchema(colander.Schema):
amount = colander.SchemaNode(
colander.Decimal(),
widget=deform.widget.MoneyInputWidget(
readonly_template='textinput_disabled.pt',
css_class='moneyinput amount'
),
)
currency = colander.SchemaNode(
colander.String(),
default='EUR',
widget=deform.widget.TextInputWidget(
readonly_template='textinput_disabled.pt',
css_class='moneyinput currency'
)
)
def __init__(self, *args, **kwargs):
if 'widget' not in kwargs:
readonly = kwargs.pop('readonly', False)
kwargs['widget'] = deform.widget.MappingWidget(
category='default',
template='money_mapping.pt',
readonly_template='money_mapping_disabled.pt',
item_template='money_mapping_item.pt',
item_readonly_template='money_mapping_item_diabled.pt',
readonly=readonly,
)
super().__init__(*args, **kwargs)

11
ordr2/schemas/orders.py

@ -3,7 +3,7 @@ import deform
from ordr2.models import Category from ordr2.models import Category
from . import CSRFSchema from . import CSRFSchema, MoneyInputSchema
CATEGORIES = [(c.name, c.value.capitalize()) for c in Category] CATEGORIES = [(c.name, c.value.capitalize()) for c in Category]
@ -28,13 +28,8 @@ class ConsumableSchema(CSRFSchema):
package_size = colander.SchemaNode( package_size = colander.SchemaNode(
colander.String() colander.String()
) )
unit_price = colander.SchemaNode( unit_price = MoneyInputSchema(
colander.Decimal(), readonly=False
widget=deform.widget.MoneyInputWidget()
)
currency = colander.SchemaNode(
colander.String(),
default='EUR'
) )
comment = colander.SchemaNode( comment = colander.SchemaNode(
colander.String(), colander.String(),

4
ordr2/static/css/style.css

@ -733,3 +733,7 @@ input[value="new_password:mapping"] + div { margin-bottom:10px; }
border-bottom: 1px solid #aaa;} border-bottom: 1px solid #aaa;}
div.alert a { color:inherit; text-decoration:underline; } div.alert a { color:inherit; text-decoration:underline; }
td.column-pkg, td.column-price { text-align:right; } td.column-pkg, td.column-price { text-align:right; }
.moneyinput .amount { width:167px; text-align:right;}
.moneyinput .currency { width:30px; text-align:center;}

36
ordr2/templates/deform/money_mapping.pt

@ -0,0 +1,36 @@
<span tal:define="error_class error_class|field.widget.error_class;
description description|field.description;
title title|field.title;
hidden hidden|field.widget.hidden;
category category|field.widget.category;
item_template item_template|field.widget.item_template;
amount_field field.children[0];
currency_field field.children[1];
oid oid|amount_field.oid;
required required|amount_field.required;"
i18n:domain="deform"
class="moneyinput">
${field.start_mapping()}
<div tal:repeat="child field.children"
tal:replace="structure child.render_template(item_template)" >
</div>
${field.end_mapping()}
<p class="help-inline"
tal:define="errstr 'error-%s' % field.oid"
tal:repeat="msg amount_field.error.messages()|currency_field.error.messages()"
i18n:translate=""
tal:attributes="id repeat.msg.index==0 and errstr or
('%s-%s' % (errstr, repeat.msg.index))"
tal:condition="(amount_field.error or currency_field.error) and not field.widget.hidden">
${msg}
</p>
<p tal:condition="field.description and not field.widget.hidden"
class="help-inline" >
${field.description}
</p>
</span>

36
ordr2/templates/deform/money_mapping_disabled.pt

@ -0,0 +1,36 @@
<span tal:define="error_class error_class|field.widget.error_class;
description description|field.description;
title title|field.title;
hidden hidden|field.widget.hidden;
category category|field.widget.category;
item_template item_template|field.widget.item_readonly_template;
amount_field field.children[0];
currency_field field.children[1];
oid oid|amount_field.oid;
required required|amount_field.required;"
i18n:domain="deform"
class="moneyinput">
${field.start_mapping()}
<div tal:repeat="child field.children"
tal:replace="structure child.render_template(item_template)" >
</div>
${field.end_mapping()}
<p class="help-inline"
tal:define="errstr 'error-%s' % field.oid"
tal:repeat="msg amount_field.error.messages()|currency_field.error.messages()"
i18n:translate=""
tal:attributes="id repeat.msg.index==0 and errstr or
('%s-%s' % (errstr, repeat.msg.index))"
tal:condition="(amount_field.error or currency_field.error) and not field.widget.hidden">
${msg}
</p>
<p tal:condition="field.description and not field.widget.hidden"
class="help-inline" >
${field.description}
</p>
</span>

25
ordr2/templates/deform/money_mapping_item.pt

@ -0,0 +1,25 @@
<span tal:define="error_class error_class|field.widget.error_class;
description description|field.description;
title title|field.title;
oid oid|field.oid;
hidden hidden|field.widget.hidden;
category category|field.widget.category;
structural hidden or category == 'structural';
required required|field.required;"
class="${field.error and 'error' or ''} ${field.widget.item_css_class or ''} ${field.default_item_css_class()}"
title="${description}"
id="item-${oid}"
tal:omit-tag="structural"
i18n:domain="deform">
<div tal:define="input_prepend field.widget.input_prepend | None;
input_append field.widget.input_append | None"
tal:omit-tag="not (input_prepend or input_append)"
class="input-group">
<span class="input-group-addon"
tal:condition="input_prepend">${input_prepend}</span
><span tal:replace="structure field.serialize(cstruct).strip()"
/><span class="input-group-addon"
tal:condition="input_append">${input_append}</span>
</div>
</span>

25
ordr2/templates/deform/money_mapping_item_diabled.pt

@ -0,0 +1,25 @@
<span tal:define="error_class error_class|field.widget.error_class;
description description|field.description;
title title|field.title;
oid oid|field.oid;
hidden hidden|field.widget.hidden;
category category|field.widget.category;
structural hidden or category == 'structural';
required required|field.required;"
class="${field.error and 'error' or ''} ${field.widget.item_css_class or ''} ${field.default_item_css_class()}"
title="${description}"
id="item-${oid}"
tal:omit-tag="structural"
i18n:domain="deform">
<div tal:define="input_prepend field.widget.input_prepend | None;
input_append field.widget.input_append | None"
tal:omit-tag="not (input_prepend or input_append)"
class="input-group">
<span class="input-group-addon"
tal:condition="input_prepend">${input_prepend}</span
><span tal:replace="structure field.serialize(cstruct, readonly=True).strip()"
/><span class="input-group-addon"
tal:condition="input_append">${input_append}</span>
</div>
</span>

4
ordr2/templates/deform/textinput_disabled.pt

@ -1,8 +1,8 @@
<span tal:define="name name|field.name; <span tal:define="name name|field.name;
css_class css_class|field.widget.css_class; css_class css_class|field.widget.css_class;
oid oid|field.oid; oid oid|field.oid;
mask mask|field.widget.mask; mask mask|field.widget.mask|None;
mask_placeholder mask_placeholder|field.widget.mask_placeholder; mask_placeholder mask_placeholder|field.widget.mask_placeholder|'_';
style style|field.widget.style; style style|field.widget.style;
" "
tal:omit-tag=""> tal:omit-tag="">

14
ordr2/views/admin.py

@ -309,8 +309,8 @@ def consumable_new_form_processing(context, request):
vendor=appstruct['vendor'], vendor=appstruct['vendor'],
catalog_nr=appstruct['catalog_nr'], catalog_nr=appstruct['catalog_nr'],
package_size=appstruct['package_size'], package_size=appstruct['package_size'],
unit_price=appstruct['unit_price'], unit_price=appstruct['unit_price']['amount'],
currency=appstruct['currency'], currency=appstruct['unit_price']['currency'],
comment=appstruct['comment'] comment=appstruct['comment']
) )
request.dbsession.add(consumable) request.dbsession.add(consumable)
@ -336,8 +336,10 @@ def consumable_edit_form(context, request):
'vendor': context.model.vendor, 'vendor': context.model.vendor,
'catalog_nr': context.model.catalog_nr, 'catalog_nr': context.model.catalog_nr,
'package_size': context.model.package_size, 'package_size': context.model.package_size,
'unit_price': context.model.unit_price, 'unit_price': {
'currency': context.model.currency, 'amount': context.model.unit_price,
'currency': context.model.currency
},
'comment': context.model.comment 'comment': context.model.comment
} }
form.set_appstruct(form_data) form.set_appstruct(form_data)
@ -367,8 +369,8 @@ def consumable_edit_form_processing(context, request):
context.model.vendor = appstruct['vendor'] context.model.vendor = appstruct['vendor']
context.model.catalog_nr = appstruct['catalog_nr'] context.model.catalog_nr = appstruct['catalog_nr']
context.model.package_size = appstruct['package_size'] context.model.package_size = appstruct['package_size']
context.model.unit_price = appstruct['unit_price'] context.model.unit_price = appstruct['unit_price']['amount']
context.model.currency = appstruct['currency'] context.model.currency = appstruct['unit_price']['currency']
context.model.comment = appstruct['comment'] context.model.comment = appstruct['comment']
msg = 'Consumable <em>{!s}</em> updated.'.format(context.model) msg = 'Consumable <em>{!s}</em> updated.'.format(context.model)