def _get_field_factory(cls, key, field): '''Find a factory for a field. If key is supplied, we try to find it via a class attribute. If it fails (or key is not supplied), we fall back to an adapter lookup on field's schema. ''' # Check if a factory is defined explicitly as a class attribute if key and hasattr(cls, key): factory = getattr(cls, key) return factory if callable(factory) else None # If reached here, there is no hint via class attribute. # Try to find a factory for this field. if not field: schema = cls.get_schema() field = schema.get(key) assert field, 'No field %r in schema %s' % (key, schema) factory = None if isinstance(field, zope.schema.Object): factory = adapter_registry.lookup([], field.schema) else: factory = field.defaultFactory return factory
def _get_field(self, kt): k, kt = kt[0], kt[1:] schema = self.get_schema() field = schema.get(k) value = getattr(self, k) if kt: return self._get_field_field(field, value, kt) else: yf = field.bind(FieldContext(key=k, value=value)) yf.context.title = yf.title return (yf, value)
def _dictize_errors(self, errors): global_key = ErrorDict.global_key schema = self.get_schema() res = ErrorDict() for k, ef in errors: if k is None: # Found failed invariants res[global_key] = [str(ex) for ex in ef] else: # Found a field-level error field = schema.get(k) if not field: continue f = field.get(self) res[k] = self._dictize_errors_for_field(ef, f, field) return res
def _dictize_errors(self, errors): global_key = ErrorDict.global_key schema = self.get_schema() res = ErrorDict() for k, ef in errors: if k is None: # Found failed invariants res[global_key] = [ str(ex) for ex in ef ] else: # Found a field-level error field = schema.get(k) if not field: continue f = field.get(self) res[k] = self._dictize_errors_for_field(ef, f, field) return res
def _from_xml(self, e): schema = self.obj.get_schema() factory = type(self.obj) obj = factory() # Fixme: Why dont we directly update self.obj? for p in e: k = QName(p.tag).localname k = inflection.underscore(k) yf = schema.get(k) assert yf, "Expected a field named %s at schema %s" % (k, schema) yf1 = yf.bind(FieldContext(key=k, value=yf.get(self.obj))) ys = serializer_for_field(yf1) ys.target_namespace = self.target_namespace yf.set(obj, ys.from_xml(p)) return obj
def _from_xml(self, e): schema = self.obj.get_schema() factory = type(self.obj) obj = factory() # Fixme: Why dont we directly update self.obj? for p in e: k = QName(p.tag).localname k = inflection.underscore(k) yf = schema.get(k) assert yf, 'Expected a field named %s at schema %s' % (k, schema) yf1 = yf.bind(FieldContext(key=k, value=yf.get(self.obj))) ys = serializer_for_field(yf1) ys.target_namespace = self.target_namespace yf.set(obj, ys.from_xml(p)) return obj
def _dictize_errors(self, errors): global_key = ErrorDict.global_key schema = self.get_schema() #log1.debug('\n\n IN DICITZE ERRORS: %s\n\n', errors) res = ErrorDict() if errors: for k, ef in errors: if k is None: # Found failed invariants res[global_key] = [ str(ex) for ex in ef ] else: #log1.debug('\n\n IN INNER DICITZE ERRORS: k is %s, type is %s,ef is %s, type[ef] is %s, ef[0] is %s, type[0] is %s\n\n', k, type(k),ef, type(ef), ef[0], type(ef[0])) # Found a field-level error field = schema.get(k) if not field: continue f = field.get(self) res[k] = self._dictize_errors_for_field(ef, f, field) return res
def widget(self): for schema in iterSchemata(self.context): field = schema.get(self.id, None) if field is not None: break; widget = queryMultiAdapter((field, self.request), interfaces.IFieldWidget, name=self.id) if widget is None: widget = queryMultiAdapter((field, self.request), interfaces.IFieldWidget) if queryMultiAdapter((None, self.request, None, None, widget), interface=IPageTemplate, name='display_{0}'.format(self.id)): widget.mode = 'display_{0}'.format(self.id) else: widget.mode = 'display' widget.context = self.context alsoProvides(widget, interfaces.IContextAware) widget.update() zope.event.notify(AfterWidgetUpdateEvent(widget)) return widget