def assert_value_persisted(self, field, initial_default, new_default, browser=None): """Create an object (by delegating to add_object() implemented by the specific testcase) and assert that a field's default value will actually be persisted on the object after creation. We do this by - first patching the field's defaultFactory to return a known value - creating the object using that default - modifiying the defaultFactory to return a different default - checking that the field value on the object didn't also change """ # We're using the defaultFactory here to be able to distinguish # a default of `None` from "no default" with TempMonkeyPatch(field, 'defaultFactory', lambda: initial_default): obj = self.add_object(browser) initial_value = field.get(field.interface(obj)) with TempMonkeyPatch(field, 'defaultFactory', lambda: new_default): current_value = field.get(field.interface(obj)) self.assertEqual( initial_value, current_value, 'Value %r should have been persisted, got %r instead' % ( initial_value, current_value)) # In addition to asserting initial_value == current_value, also check # persistence using our own helper function which should raise an # AttributeError if no value has been persisted. persisted_value = get_persisted_value_for_field(obj, field) self.assertEqual(persisted_value, current_value)
def update_field(self, obj, field, item): if field.readonly: return name = field.getName() value = self.get_value_from_pipeline(field, item) if value is not _tm_marker: field.set(field.interface(obj), value) return # Get the field's current value, if it has one then leave it alone try: value = get_persisted_value_for_field(obj, field) except AttributeError: value = NO_VALUE # Fix default description to be an empty unicode instead of # an empty bytestring because of this bug: # https://github.com/plone/plone.dexterity/pull/33 if name == 'description' and value == '': field.set(field.interface(obj), u'') return if not (value is field.missing_value or value is NO_VALUE): return # Finally, set a default value if nothing is set so far default = self.determine_default_value(obj, field) field.set(field.interface(obj), default)
def update_field(self, obj, field, item): if field.readonly: return name = field.getName() value = self.get_value_from_pipeline(field, item) if value is not _tm_marker: field.set(field.interface(obj), value) return # Get the field's current value, if it has one then leave it alone try: value = get_persisted_value_for_field(obj, field) except AttributeError: value = NO_VALUE # Fix default description to be an empty unicode instead of # an empty bytestring because of this bug: # https://github.com/plone/plone.dexterity/pull/33 if name == 'description' and value == '': field.set(field.interface(obj), u'') return if not(value is field.missing_value or value is NO_VALUE): return # Finally, set a default value if nothing is set so far default = self.determine_default_value(obj, field) field.set(field.interface(obj), default)
def assert_value_persisted(self, field, initial_default, new_default, browser=None): """Create an object (by delegating to add_object() implemented by the specific testcase) and assert that a field's default value will actually be persisted on the object after creation. We do this by - first patching the field's defaultFactory to return a known value - creating the object using that default - modifiying the defaultFactory to return a different default - checking that the field value on the object didn't also change """ # We're using the defaultFactory here to be able to distinguish # a default of `None` from "no default" with TempMonkeyPatch(field, 'defaultFactory', lambda: initial_default): obj = self.add_object(browser) initial_value = field.get(field.interface(obj)) with TempMonkeyPatch(field, 'defaultFactory', lambda: new_default): current_value = field.get(field.interface(obj)) self.assertEqual( initial_value, current_value, 'Value %r should have been persisted, got %r instead' % (initial_value, current_value)) # In addition to asserting initial_value == current_value, also check # persistence using our own helper function which should raise an # AttributeError if no value has been persisted. persisted_value = get_persisted_value_for_field(obj, field) self.assertEqual(persisted_value, current_value)
def changedField(field, value, context=None): """Figure if a field's value changed Comparing the value of the context attribute and the given value""" if context is None: context = field.context if context is None: # IObjectWidget madness return True if zope.schema.interfaces.IObject.providedBy(field): return True if not IPersistent.providedBy(context): # Field is not persisted, delegate to original implementation # Could be a z3c.formwidget QueryContex or an AQ wrapped dict # instance from Plone's TTW registry editor. assert any(( isinstance(context, QueryContext), isinstance(context, RecordsProxy), isinstance(context, ImplicitAcquisitionWrapper) and isinstance(aq_base(context), dict), )) return original_changedField(field, value, context) dm = zope.component.getMultiAdapter((context, field), interfaces.IDataManager) if not dm.canAccess(): # Can't get the original value, assume it changed return True # Determine the original value # Use a helper method that actually returns the persisted value, # *without* triggering any fallbacks to default values or # missing values. try: stored_value = get_persisted_value_for_field(context, field) except AttributeError: return True if stored_value != value: return True return False
def changedField(field, value, context=None): """Figure if a field's value changed Comparing the value of the context attribute and the given value""" if context is None: context = field.context if context is None: # IObjectWidget madness return True if zope.schema.interfaces.IObject.providedBy(field): return True if not IPersistent.providedBy(context): # Field is not persisted, delegate to original implementation # Could be a z3c.formwidget QueryContex or an AQ wrapped dict # instance from Plone's TTW registry editor. assert any(( isinstance(context, QueryContext), isinstance(context, RecordsProxy), isinstance(context, ImplicitAcquisitionWrapper) and isinstance(aq_base(context), dict), )) return original_changedField(field, value, context) dm = zope.component.getMultiAdapter( (context, field), interfaces.IDataManager) if not dm.canAccess(): # Can't get the original value, assume it changed return True # Determine the original value # Use a helper method that actually returns the persisted value, # *without* triggering any fallbacks to default values or # missing values. try: stored_value = get_persisted_value_for_field(context, field) except AttributeError: return True if stored_value != value: return True return False