Esempio n. 1
0
def determine_default_value(field, container):
    """Determine a field's default value during object creation.
    """
    # We deliberately ignore form level defaults here.

    # zope.schema defaultFactory
    #
    # Based on zope.schema._boostrapfields.DefaultProperty. We don't use
    # the class level 'default' attribute, which is a DefaultProperty,
    # because we want to be able to distinguish "default of None" and
    # "no default", at least for the defaultFactory. That's why it's
    # necessary to check in the instance dict for a defaultFactory first,
    # instead of letting the DefaultProperty implementation handle it.

    default_factory = field.__dict__.get('defaultFactory')
    if default_factory is not None:

        if IContextAwareDefaultFactory.providedBy(default_factory):
            # Access DefaultProperty descriptor to trigger validation
            field.bind(container).default
            return default_factory(container)
        else:
            # Access DefaultProperty descriptor to trigger validation
            field.default
            return default_factory()

    field_default = field.__dict__.get('default')
    if field_default is not None:
        # Access DefaultProperty descriptor to trigger validation
        field.default
        return field_default

    return NO_DEFAULT_MARKER
Esempio n. 2
0
def determine_default_value(field, container):
    """Determine a field's default value during object creation.
    """
    # We deliberately ignore form level defaults here.

    # zope.schema defaultFactory
    #
    # Based on zope.schema._boostrapfields.DefaultProperty. We don't use
    # the class level 'default' attribute, which is a DefaultProperty,
    # because we want to be able to distinguish "default of None" and
    # "no default", at least for the defaultFactory. That's why it's
    # necessary to check in the instance dict for a defaultFactory first,
    # instead of letting the DefaultProperty implementation handle it.

    default_factory = field.__dict__.get('defaultFactory')
    if default_factory is not None:

        if IContextAwareDefaultFactory.providedBy(default_factory):
            # Access DefaultProperty descriptor to trigger validation
            field.bind(container).default
            return default_factory(container)
        else:
            # Access DefaultProperty descriptor to trigger validation
            field.default
            return default_factory()

    field_default = field.__dict__.get('default')
    if field_default is not None:
        # Access DefaultProperty descriptor to trigger validation
        field.default
        return field_default

    return NO_DEFAULT_MARKER
Esempio n. 3
0
def burnDefaultValues(ob, event):
    schema = load_schema(
        aq_base(ob).schema,
        cache_key=aq_base(ob).schema_digest,
    )
    for name in schema.names():
        field = schema[name]
        try:
            factory = field.defaultFactory
        except AttributeError:
            factory = None
        try:
            default = field.default
        except AttributeError:
            default = None
        if not (factory or default):
            continue
        bound = field.bind(ob)
        try:
            value = bound.get(ob)
        except AttributeError:
            value = None
        if value:
            continue
        if factory:
            if IContextAwareDefaultFactory.providedBy(factory):
                value = factory(ob)
            else:
                value = factory()
        else:
            value = deepcopy(default)
        if value:
            bound.set(ob, value)
            set_contained(value, name, ob)
Esempio n. 4
0
 def register_interface(self, iface, omit=(), prefix=None):
     proxy = RecordsProxy(self, iface, prefix)
     for name in iface.names():
         if name in omit:
             continue
         field = iface[name]
         if field.defaultFactory is not None:
             if IContextAwareDefaultFactory.providedBy(field.defaultFactory):  # noqa
                 setattr(proxy, name, field.defaultFactory(self))
             else:
                 setattr(proxy, name, field.defaultFactory())
         elif field.default is not None:
             setattr(proxy, name, field.default)
Esempio n. 5
0
 def register_interface(self, iface, omit=(), prefix=None):
     proxy = RecordsProxy(self, iface, prefix)
     for name in iface.names():
         if name in omit:
             continue
         field = iface[name]
         if field.defaultFactory is not None:
             if IContextAwareDefaultFactory.providedBy(
                     field.defaultFactory):  # noqa
                 setattr(proxy, name, field.defaultFactory(self))
             else:
                 setattr(proxy, name, field.defaultFactory())
         elif field.default is not None:
             setattr(proxy, name, field.default)
Esempio n. 6
0
    def getDefaultValue(self, form):
        if self.defaultFactory is not None:
            if IContextAwareDefaultFactory.providedBy(self.defaultFactory):
                default = self.defaultFactory(form.getContent()) 
            else: 
                default = self.defaultFactory()
        else:
            default = super(BaseField, self).getDefaultValue(form)

        if default is NO_VALUE:
            default = self.defaultValue

        if default is None:
            return NO_VALUE

        return default
 def __get__(self, inst, owner):
     name, check = self._info
     defaultFactory = inst.__dict__.get('defaultFactory')
     # If there is no default factory, simply return the default.
     if defaultFactory is None:
         return inst.__dict__[name]
     # Get the default value by calling the factory. Some factories might
     # require a context to produce a value.
     if IContextAwareDefaultFactory.providedBy(defaultFactory):
         value = defaultFactory(inst.context)
     else:
         value = defaultFactory()
     # Check that the created value is valid.
     if check is not None:
         check(inst, value)
     elif value != inst.missing_value:
         inst.validate(value)
     return value
Esempio n. 8
0
    def getDefaultValue(self, form):
        if self.defaultFactory is not None:
            if IContextAwareDefaultFactory.providedBy(self.defaultFactory):
                if form is None:
                    raise TypeError('defaultFactory context required.')
                default = self.defaultFactory(form.getContent()) 
            else: 
                default = self.defaultFactory()
        else:
            default = super(Field, self).getDefaultValue(form)

        if default is NO_VALUE:
            default = self.defaultValue

        if default is None:
            return NO_VALUE

        return default
Esempio n. 9
0
 def __get__(self, inst, owner):
     name, check = self._info
     defaultFactory = inst.__dict__.get('defaultFactory')
     # If there is no default factory, simply return the default.
     if defaultFactory is None:
         return inst.__dict__[name]
     # Get the default value by calling the factory. Some factories might
     # require a context to produce a value.
     if IContextAwareDefaultFactory.providedBy(defaultFactory):
         value = defaultFactory(inst.context)
     else:
         value = defaultFactory()
     # Check that the created value is valid.
     if check is not None:
         check(inst, value)
     elif value != inst.missing_value:
         inst.validate(value)
     return value
Esempio n. 10
0
def _interpolate(template, ob, request=None):
    if request is None:
        request = getRequest()
    schema = load_schema(
        aq_base(ob).schema,
        cache_key=aq_base(ob).schema_digest,
    )
    mapping = {}
    for name in schema.names():
        field = schema[name]
        bound = field.bind(ob)
        try:
            value = bound.get(ob)
        except AttributeError:
            value = ''
        if not value:
            try:
                factory = field.defaultFactory
            except AttributeError:
                factory = None
            if not factory:
                continue
            if IContextAwareDefaultFactory.providedBy(factory):
                value = factory(ob)
            else:
                value = factory()
        if not value:
            mapping[name] = ''
            continue
        try:
            mapping[name] = translate(
                bound.vocabulary.getTerm(value).title,
                context=request,
            )
        except (AttributeError, LookupError):
            mapping[name] = value
    interpolator = IStringInterpolator(ob)
    value = interpolator(i18n_interpolate(template or u'', mapping))
    return value