From 8a77fec13dae16f842cd56aa1d9c1a59f3ff107a Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sat, 7 Jul 2012 10:05:00 +0200 Subject: [PATCH 1/3] provide separate float field instance; predefine cssClass in row base class --- composer/report/result.py | 4 +++- composer/schema/README.txt | 6 +++--- composer/schema/factory.py | 8 ++++---- composer/schema/field.py | 27 +++++++++++++++++++++++++++ composer/schema/interfaces.py | 2 ++ 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/composer/report/result.py b/composer/report/result.py index 0bf3901..95db82b 100644 --- a/composer/report/result.py +++ b/composer/report/result.py @@ -42,9 +42,11 @@ class BaseRow(object): class Row(BaseRow): attributeHandlers = {} + cssClass = u'' def getRawValue(self, attr): - return self.attributeHandlers.get(attr, self.getContextAttr)(self, attr) + return self.attributeHandlers.get( + attr, self.getContextAttr)(self, attr) @staticmethod def getContextAttr(obj, attr): diff --git a/composer/schema/README.txt b/composer/schema/README.txt index a5579c6..dcf169c 100644 --- a/composer/schema/README.txt +++ b/composer/schema/README.txt @@ -49,9 +49,9 @@ Field types >>> from cybertools.composer.schema.interfaces import fieldTypes >>> sorted(t.token for t in fieldTypes) - ['checkbox', 'checkboxes', 'date', 'display', 'dropdown', 'email', 'explanation', - 'fileupload', 'heading', 'html', 'list', 'number', 'password', 'radiobuttons', - 'spacer', 'textarea', 'textline'] + ['checkbox', 'checkboxes', 'date', 'display', 'dropdown', 'email', + 'explanation', 'fileupload', 'float', 'heading', 'html', 'list', 'number', + 'password', 'radiobuttons', 'spacer', 'textarea', 'textline'] >>> from zope.schema.vocabulary import SimpleVocabulary >>> textFieldTypes = SimpleVocabulary([t for t in fieldTypes if t.token in diff --git a/composer/schema/factory.py b/composer/schema/factory.py index 465b147..ed168c6 100644 --- a/composer/schema/factory.py +++ b/composer/schema/factory.py @@ -41,7 +41,7 @@ class Email(schema.TextLine): schema.Field.__typeInfo__ = ('textline',) schema.Password.__typeInfo__ = ('password',) schema.Int.__typeInfo__ = ('number',) -schema.Float.__typeInfo__ = ('number',) +schema.Float.__typeInfo__ = ('float',) schema.Choice.__typeInfo__ = ('dropdown',) @@ -56,13 +56,13 @@ class SchemaFactory(object): fieldMapping = { #schema.TextLine: ('textline',), #schema.ASCIILine: ('textline',), - schema.Password: ('password',), + #schema.Password: ('password',), schema.Text: ('textarea',), schema.ASCII: ('textarea',), schema.Date: ('date',), schema.Datetime: ('date',), - schema.Int: ('number',), - schema.Float: ('number',), + #schema.Int: ('number',), + #schema.Float: ('float',), schema.Bool: ('checkbox',), schema.List: ('list',), #schema.Choice: ('dropdown',), diff --git a/composer/schema/field.py b/composer/schema/field.py index 18d6243..d96b63d 100644 --- a/composer/schema/field.py +++ b/composer/schema/field.py @@ -24,6 +24,7 @@ from datetime import datetime from logging import getLogger from time import strptime, strftime from zope.app.form.browser.interfaces import ITerms +from zope.i18n.locales import locales from zope.interface import implements from zope.cachedescriptors.property import Lazy from zope.component import adapts @@ -262,6 +263,32 @@ class NumberFieldInstance(FieldInstance): self.setError('invalid_number') +class FloatFieldInstance(NumberFieldInstance): + + format = 'decimal' + + def marshall(self, value): + return self.display(value, pattern=u'0.00;-0.00') + + def display(self, value, pattern=u'#,##0.00;-#,##0.00'): + if value is None: + return '' + view = self.clientInstance.view + langInfo = view and getattr(view, 'languageInfo', None) or None + if langInfo: + locale = locales.getLocale(langInfo.language) + fmt = locale.numbers.getFormatter(self.format) + return fmt.format(value, pattern=pattern) + return '%.2f' % value + + def unmarshall(self, value): + if not value: + return None + if ',' in value: + value = value.replace(',', '.') + return float(value) + + class DateFieldInstance(NumberFieldInstance): def marshall(self, value): diff --git a/composer/schema/interfaces.py b/composer/schema/interfaces.py index 9e3f809..04749cb 100644 --- a/composer/schema/interfaces.py +++ b/composer/schema/interfaces.py @@ -98,6 +98,8 @@ fieldTypes = SimpleVocabulary(( FieldType('html', 'html', u'HTML Text'), FieldType('number', 'number', u'Number', inputRenderer='input_textline', instanceName='number'), + FieldType('float', 'float', u'Float', + inputRenderer='input_textline', instanceName='float'), FieldType('date', 'date', u'Date', instanceName='date'), FieldType('email', 'email', u'E-Mail Address', displayRenderer='display_email', inputRenderer='input_textline', From 784ca70365c967d26caab06982fcfca67306ee5f Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sat, 7 Jul 2012 15:42:30 +0200 Subject: [PATCH 2/3] rename 'float' field to 'decimal' --- composer/schema/README.txt | 4 ++-- composer/schema/factory.py | 6 ++---- composer/schema/field.py | 2 +- composer/schema/interfaces.py | 4 ++-- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/composer/schema/README.txt b/composer/schema/README.txt index dcf169c..96b84cd 100644 --- a/composer/schema/README.txt +++ b/composer/schema/README.txt @@ -49,8 +49,8 @@ Field types >>> from cybertools.composer.schema.interfaces import fieldTypes >>> sorted(t.token for t in fieldTypes) - ['checkbox', 'checkboxes', 'date', 'display', 'dropdown', 'email', - 'explanation', 'fileupload', 'float', 'heading', 'html', 'list', 'number', + ['checkbox', 'checkboxes', 'date', 'decimal', 'display', 'dropdown', + 'email', 'explanation', 'fileupload', 'heading', 'html', 'list', 'number', 'password', 'radiobuttons', 'spacer', 'textarea', 'textline'] >>> from zope.schema.vocabulary import SimpleVocabulary diff --git a/composer/schema/factory.py b/composer/schema/factory.py index ed168c6..31a231d 100644 --- a/composer/schema/factory.py +++ b/composer/schema/factory.py @@ -18,8 +18,6 @@ """ Schema factory stuff. - -$Id$ """ from zope.component import adapts @@ -41,7 +39,7 @@ class Email(schema.TextLine): schema.Field.__typeInfo__ = ('textline',) schema.Password.__typeInfo__ = ('password',) schema.Int.__typeInfo__ = ('number',) -schema.Float.__typeInfo__ = ('float',) +schema.Float.__typeInfo__ = ('decimal',) schema.Choice.__typeInfo__ = ('dropdown',) @@ -62,7 +60,7 @@ class SchemaFactory(object): schema.Date: ('date',), schema.Datetime: ('date',), #schema.Int: ('number',), - #schema.Float: ('float',), + #schema.Float: ('decimal',), schema.Bool: ('checkbox',), schema.List: ('list',), #schema.Choice: ('dropdown',), diff --git a/composer/schema/field.py b/composer/schema/field.py index d96b63d..2d51988 100644 --- a/composer/schema/field.py +++ b/composer/schema/field.py @@ -263,7 +263,7 @@ class NumberFieldInstance(FieldInstance): self.setError('invalid_number') -class FloatFieldInstance(NumberFieldInstance): +class DecimalFieldInstance(NumberFieldInstance): format = 'decimal' diff --git a/composer/schema/interfaces.py b/composer/schema/interfaces.py index 04749cb..c55ad98 100644 --- a/composer/schema/interfaces.py +++ b/composer/schema/interfaces.py @@ -98,8 +98,8 @@ fieldTypes = SimpleVocabulary(( FieldType('html', 'html', u'HTML Text'), FieldType('number', 'number', u'Number', inputRenderer='input_textline', instanceName='number'), - FieldType('float', 'float', u'Float', - inputRenderer='input_textline', instanceName='float'), + FieldType('decimal', 'decimal', u'Decimal', + inputRenderer='input_textline', instanceName='decimal'), FieldType('date', 'date', u'Date', instanceName='date'), FieldType('email', 'email', u'E-Mail Address', displayRenderer='display_email', inputRenderer='input_textline', From b8db38cf9827b5903392e577545d44aa65d73c83 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sun, 8 Jul 2012 18:46:49 +0200 Subject: [PATCH 3/3] tolerate missing input items when filling grid --- composer/schema/grid/field.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer/schema/grid/field.py b/composer/schema/grid/field.py index 9c93d7a..dd69c90 100644 --- a/composer/schema/grid/field.py +++ b/composer/schema/grid/field.py @@ -114,7 +114,7 @@ class GridFieldInstance(ListFieldInstance): def unmarshallRow(self, row): item = {} for fi in self.columnFieldInstances: - value = fi.unmarshall(row[fi.name]) + value = fi.unmarshall(row.get(fi.name) or u'') if isinstance(value, basestring): value = value.strip() if fi.default is not None: