"""Converters for zope.schema-based datatypes. """ import datetime import grok from zope.component import getMultiAdapter from zope.publisher.browser import TestRequest try: from zope.app.form.browser.interfaces import ITerms except ImportError: from zope.browser.interfaces import ITerms from zope.schema.interfaces import IBool, IText, IInt, IChoice, IDate from waeup.sirp.interfaces import ISchemaTypeConverter # If a string has this value, it is considered as 'missing_value' or None. NONE_STRING_VALUE = '' class Converter(grok.Adapter): """Base for string-or-none to zope.schema field converters. """ grok.baseclass() grok.provides(ISchemaTypeConverter) def __init__(self, context): """Create a converter with context, a zope.schema.Field, as context. """ self.context = context def _convertValueFromString(self, string): """You must at least override this method to build a working converter. """ raise NotImplementedError('method not implemented') def fromString(self, string=None, strict=True): """Convert ``string`` to value according to assigned field type. """ result = None if string is NONE_STRING_VALUE: string = None if string is None: if self.context.required is True: result = self.context.default else: result = self.context.missing_value else: result = self._convertValueFromString(string) if strict: self.context.validate(result) return result def _convertValueToString(self, value): return str(value) def toString(self, value, strict=True): """Convert given `value` to string according to assigned field type. """ if strict: self.context.validate(value) if value == self.context.missing_value: return None return self._convertValueToString(value) class BoolConverter(Converter): """A converter for zope.schema.Bool fields. """ grok.context(IBool) grok.provides(ISchemaTypeConverter) def _convertValueFromString(self, string): if string is NONE_STRING_VALUE: string = None if string is None: return None if string.lower() in ['1', 'true', 'yes']: return True return False def _convertValueToString(self, value): if value is None: return None if value: return '1' return '0' class TextConverter(Converter): """A converter for zope.schema.interfaces.IText fields. """ grok.context(IText) grok.provides(ISchemaTypeConverter) def _convertValueFromString(self, string): return unicode(string) class IntConverter(Converter): """A converter for zope.schema.Int fields. """ grok.context(IInt) grok.provides(ISchemaTypeConverter) def _convertValueFromString(self, string): return int(string) class ChoiceConverter(Converter): """A converter for zope.schema.Choice fields. """ grok.context(IChoice) grok.provides(ISchemaTypeConverter) tokens = None values = None def __init__(self, context): self.context = context if not hasattr(self.context.source, 'factory'): try: self.terms = getMultiAdapter( (self.context.source, TestRequest()), ITerms) except: self.terms = None return if not hasattr(self.context.source.factory, 'getToken'): return # For expensive token/key lookups we create a 'cache' # here. This speeds up mass operations with many conversions # by factor 10 or more. # Mapping token -> value self.tokens = dict([(self.context.source.factory.getToken(x), x) for x in self.context.source.factory.getValues()]) # Mapping value -> token self.values = dict([(y,x) for x,y in self.tokens.items()]) def _convertValueFromString(self, string): if self.tokens is not None: result = None try: result = self.tokens[string] except KeyError: # Be gentle... try: result = self.tokens[string.lower()] return result except KeyError: tokenlist = (','.join(self.tokens[:2])) raise ValueError( 'The token %s is not valid. Use one of %s, ...' % ( string, tokenlist)) return result if self.terms is not None: return self.terms.getValue(string) result = self.context.source.getTermByToken(string).value return result def _convertValueToString(self, value): if self.values is not None: return self.values[value] if self.terms is not None: return self.terms.getTerm(value).token return str(value) def fromString(self, string=None, strict=False): """Convert ``string`` to value according to assigned field type. We change the default for ``strict``: this disables extra validation checks for Choice fields and saves lots of time. If a string/value is out of allowed range we get a value or key error anyway. """ return super(ChoiceConverter, self).fromString(string=string, strict=strict) def toString(self, value, strict=False): """Convert ``value`` to string according to assigned field type. We change the default for ``strict``: this disables extra validation checks for Choice fields and saves lots of time. If a string/value is out of allowed range we get a value or key error anyway. """ return super(ChoiceConverter, self).toString(value=value, strict=strict) class DateConverter(Converter): """A converter for zope.schema.IDate fields. Converts date to string and vice versa. Stringified dates are expected in format ``YYYY-MM-DD``. To support at least some other formats, we accept for conversion from string also the following formats: * ``YYYY/MM/DD`` * ``DD/MM/YYYY`` * ``D/M/YYYY`` * ``DD.MM.YYYY`` * ``D.M.YYYY`` When converting to strings, always 'YYYY-MM-DD' is returned. For convenience, when converting from strings also string data separated by space is stripped before processing. Therefore strings like '1990-04-01 12:12:01 GMT +1' will also work. """ grok.context(IDate) grok.provides(ISchemaTypeConverter) #: List of supported date formats in `strftime()` notation. formats = ['%Y-%m-%d', '%Y/%m/%d' , '%d/%m/%Y', '%D/%M/%Y', '%d.%m.%Y', '%D.%M.%Y'] def _convertValueFromString(self, string): orig_string = string if string is NONE_STRING_VALUE: string = None if string is None: return None value = None if ' ' in string: string = string.split(' ')[0] for format in self.formats: try: value = datetime.datetime.strptime(string, format) break except ValueError: pass if value is None: raise ValueError( 'Cannot convert to date: %s. Use YYYY-MM-DD.' % orig_string) value = value.date() return value def _convertValueToString(self, value): return datetime.date.strftime(value, '%Y-%m-%d') from zope import schema from zope.component import createObject from zope.interface import Interface from zope.formlib import form from zope.formlib.form import ( _widgetKey, WidgetInputError, ValidationError, InputErrors, expandPrefix) from zope.formlib.interfaces import IInputWidget from zope.publisher.browser import TestRequest def getWidgetsData(widgets, form_prefix, data): errors = [] form_prefix = expandPrefix(form_prefix) for input, widget in widgets.__iter_input_and_widget__(): if input and IInputWidget.providedBy(widget): name = _widgetKey(widget, form_prefix) if not widget.hasInput(): continue try: data[name] = widget.getInputValue() except ValidationError, error: # convert field ValidationError to WidgetInputError error = WidgetInputError(widget.name, widget.label, error) errors.append((name, error)) except InputErrors, error: errors.append((name, error)) return errors class IObjectConverter(Interface): def __init__(iface): """Create an converter. `iface` denotes the interface to which we want to turn any passed object. """ def applyRowData(data_dict, context, form_fields=None): """Apply data in `data_dict` to `context`. `data_dict` is a dict containing field names as keys and an object or string as `context`. If `context` is a string, this is understood as a factory name and we will try to create a proper object calling ``createObject()``. `form_fields` are by default (``None``) buildt from the given `iface` but can also be passed in to override the default. This might be handy if you want to omit or select certains fields from the interface. Returns a tuple ``()`` where ``ERROR_DICT`` is a dict of errors for single fields (if happened), ``INV_ERR_LIST`` is a list of invariant errors happened (errors that apply to several fields), and ``OBJ`` is the created/updated object. """ class DefaultObjectConverter(grok.Adapter): """An object converter can apply CSV data to objects. Thus, in a way, it can turn CSV data into real objects. """ grok.context(Interface) grok.provides(IObjectConverter) def __init__(self, iface): self.iface = iface self.form_fields = form.Fields(iface) return def applyRowData(self, data_dict, context, form_fields=None): if form_fields is None: form_fields = self.form_fields obj = context if isinstance(context, basestring): obj = createObject(context) request = TestRequest(form={}) for key, val in data_dict.items(): request.form['form.%s' % key] = val widgets = form.setUpWidgets( form_fields, 'form', obj, request) errors = getWidgetsData(widgets, 'form', data_dict) err_messages = [] if errors: for key, error in errors: message = error.args[0] err_messages.append((key, message)) invariant_errors = form.checkInvariants(form_fields, data_dict) invariant_errors = [err.message for err in invariant_errors] if not errors and not invariant_errors: changed = form.applyChanges( obj, form_fields, data_dict) return err_messages, invariant_errors, obj