def __call__(self): context = aq_inner(self.context) fieldname = self.request.get('fieldname') portal_type = self.request.get('portal_type') fti = zope.component.getUtility(IDexterityFTI, name=portal_type) schema = fti.lookupSchema() field = schema.get(fieldname) if field is None: # The field might be defined in a behavior schema behavior_assignable = IBehaviorAssignable(context, None) for behavior_reg in behavior_assignable.enumerateBehaviors(): behavior_schema = IFormFieldProvider(behavior_reg.interface, None) if behavior_schema is not None: field = behavior_schema.get(fieldname) if field is not None: break vname = field.vocabularyName factory = zope.component.getUtility(IVocabularyFactory, vname) tree = factory(context) # XXX: "selected" is not set in input.pt, so does it make sense to check # for it here? Only if this json view is called elsewhere, which # doesn't seem to be the case... selected = self.request.get('selected', '').split('|') return JSONWriter().write(dict2dynatree(tree, selected, True, False))
def getAdditionalSchemata(context=None, portal_type=None): """Get additional schemata for this context or this portal_type. Additional schemata can be defined in behaviors. Usually either context or portal_type should be set, not both. The idea is that for edit forms or views you pass in a context (and we get the portal_type from there) and for add forms you pass in a portal_type (and the context is irrelevant then). If both are set, the portal_type might get ignored, depending on which code path is taken. """ log.debug("getAdditionalSchemata with context %r and portal_type %s", context, portal_type) if context is None and portal_type is None: return if context: behavior_assignable = IBehaviorAssignable(context, None) else: behavior_assignable = None if behavior_assignable is None: log.debug("No behavior assignable found, only checking fti.") # Usually an add-form. if portal_type is None: portal_type = context.portal_type fti = getUtility(IDexterityFTI, name=portal_type) for behavior_name in fti.behaviors: behavior_interface = None behavior_instance = queryUtility(IBehavior, name=behavior_name) if not behavior_instance: try: behavior_interface = resolveDottedName(behavior_name) except (ValueError, ImportError): log.warning("Error resolving behaviour %s", behavior_name) continue else: behavior_interface = behavior_instance.interface if behavior_interface is not None: behavior_schema = IFormFieldProvider(behavior_interface, None) if behavior_schema is not None: yield behavior_schema else: log.debug("Behavior assignable found for context.") for behavior_reg in behavior_assignable.enumerateBehaviors(): behavior_schema = IFormFieldProvider(behavior_reg.interface, None) if behavior_schema is not None: yield behavior_schema
class AddTranslationsForm(AutoExtensibleForm, Form): schema = IFormFieldProvider(IAddTranslation) ignoreContext = True label = _(u"label_add_translations", default=u"Add translations") description = _( u"long_description_add_translations", default=u"This form allows you to add currently existing " u"objects to be the translations of the current " u"object. You have to manually select both the " u"language and the object." ) @button.buttonAndHandler(_(u"add_translations", default=u"Add translations")) def handle_add(self, action): data, errors = self.extractData() if not errors: content = data['content'] language = data['language'] ITranslationManager(self.context)\ .register_translation(language, content) ILanguage(content).set_language(language) return self.request.response.redirect( self.context.absolute_url() + '/add_translations')
class RemoveTranslationsForm(AutoExtensibleForm, Form): schema = IFormFieldProvider(IRemoveTranslation) ignoreContext = True label = _(u"label_remove_translations", default=u"Remove translations") description = _(u"long_description_remove_translations", default=u"This form allows you to remove the existing " u"translations of the current object. You can " u"just delete the link between the objects " u"or you can delete the object itself.") @button.buttonAndHandler(_(u"unlink selected"), name='unlink') def handle_unlink(self, action): data, errors = self.extractData() manager = ITranslationManager(self.context) if not errors: for language in data['languages']: manager.remove_translation(language) return self.request.response.redirect(self.context.absolute_url() + '/remove_translations') @button.buttonAndHandler(_(u"remove selected"), name='remove') def handle_remove(self, action): data, errors = self.extractData() manager = ITranslationManager(self.context) if not errors: for language in data['languages']: content = manager.get_translation(language) manager.remove_translation(language) aq_parent(content).manage_delObjects([content.getId()]) return self.request.response.redirect(self.context.absolute_url() + '/remove_translations')
class ConnectTranslation(AutoExtensibleForm, Form): schema = IFormFieldProvider(IConnectTranslation) ignoreContext = True label = _(u"label_connect_translation", default=u"Connect translation") description = _( u"long_description_connect_translation", default=u"This form allows you to connect a currently existing " u"translations of the current object.") @button.buttonAndHandler( _(u"connect_translation", default=u"Connect translation")) def handle_add(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return content = data['content'] language = data['language'] ILanguage(content).set_language(language) itm = ITranslationManager(self.context) # the 'register_translation'-method takes content OR # UUID as second parameter. We need to use the UUID # here because otherwise the catalog can't be acquired # and the translation index is not updated itm.register_translation(language, IUUID(content)) return self.request.response.redirect(self.context.absolute_url() + '/modify_translations')
def get_ttw_fields(obj): """Returns names of the fields that were added to obj through the web""" fti = getUtility(IDexterityFTI, name=obj.portal_type) full_schema = fti.lookupSchema() all_fields = schema.getFieldsInOrder(full_schema) schema_policy = getUtility(ISchemaPolicy, name=fti.schema_policy) original_schema = schema_policy.bases(None, None)[0] original_fields = schema.getFieldsInOrder(original_schema) new_fields = [field[0] for field in all_fields if field[0] not in dict(original_fields).keys()] for behavior_id in fti.behaviors: behavior = getUtility(IBehavior, behavior_id).interface if behavior == IContactDetails or not IFormFieldProvider.providedBy(behavior): continue try: default_fieldset_fields = non_fieldset_fields(behavior) behavior_name = behavior_id.split('.')[-1] # @TODO: get generic method to get widget id new_fields.extend(['%s.%s' % (behavior_name, field_name) for field_name in default_fieldset_fields]) except: pass return new_fields
def get_ttw_fields(obj): """Returns names of the fields that were added to obj through the web""" fti = getUtility(IDexterityFTI, name=obj.portal_type) full_schema = fti.lookupSchema() all_fields = schema.getFieldsInOrder(full_schema) schema_policy = getUtility(ISchemaPolicy, name=fti.schema_policy) original_schema = schema_policy.bases(None, None)[0] original_fields = schema.getFieldsInOrder(original_schema) new_fields = [ field[0] for field in all_fields if field[0] not in dict(original_fields).keys() ] for behavior_id in fti.behaviors: behavior = getUtility(IBehavior, behavior_id).interface if behavior in IGNORED_BEHAVIORS or not IFormFieldProvider.providedBy( behavior): continue try: default_fieldset_fields = non_fieldset_fields(behavior) behavior_name = behavior_id.split('.')[-1] # @TODO: get generic method to get widget id new_fields.extend([ '%s.%s' % (behavior_name, field_name) for field_name in default_fieldset_fields ]) except: pass return new_fields
def get_fields(fti): schema = fti.lookupSchema() fields = getFields(schema) for behavior_id in fti.behaviors: schema = getUtility(IBehavior, behavior_id).interface if not IFormFieldProvider.providedBy(schema): continue fields.update(getFields(schema)) return fields
def get_ordered_fields(fti): """ return fields in fieldset order """ # NOTE: code extracted from collective.excelexport. Original comments # preserved # this code is much complicated because we have to get sure # we get the fields in the order of the fieldsets # the order of the fields in the fieldsets can differ # of the getFieldsInOrder(schema) order... # that's because fields from different schemas # can take place in the same fieldset schema = fti.lookupSchema() fieldset_fields = {} ordered_fieldsets = ['default'] for fieldset in schema.queryTaggedValue(FIELDSETS_KEY, []): ordered_fieldsets.append(fieldset.__name__) fieldset_fields[fieldset.__name__] = fieldset.fields if fieldset_fields.get('default', []): fieldset_fields['default'] += non_fieldset_fields(schema) else: fieldset_fields['default'] = non_fieldset_fields(schema) # Get the behavior fields fields = getFieldsInOrder(schema) for behavior_id in fti.behaviors: schema = getUtility(IBehavior, behavior_id).interface if not IFormFieldProvider.providedBy(schema): continue fields.extend(getFieldsInOrder(schema)) for fieldset in schema.queryTaggedValue(FIELDSETS_KEY, []): fieldset_fields.setdefault( fieldset.__name__, []).extend(fieldset.fields) ordered_fieldsets.append(fieldset.__name__) fieldset_fields['default'].extend(non_fieldset_fields(schema)) ordered_fields = [] for fieldset in ordered_fieldsets: ordered_fields.extend(fieldset_fields[fieldset]) fields.sort(key=lambda field: ordered_fields.index(field[0])) return fields
def is_field(self, attr_name): ttool = api.portal.get_tool('portal_types') fti = ttool[self.context.portal_type] is_field = bool(fti.lookupSchema().get(attr_name)) if not is_field: # check if it is a behavior field for behavior_id in fti.behaviors: schema = getUtility(IBehavior, behavior_id).interface if not IFormFieldProvider.providedBy(schema): continue is_field = bool(schema.get(attr_name)) return is_field
def getAdditionalSchemata(context=None, portal_type=None): """Get additional schemata for this context or this portal_type. Additional form field schemata can be defined in behaviors. Usually either context or portal_type should be set, not both. The idea is that for edit forms or views you pass in a context (and we get the portal_type from there) and for add forms you pass in a portal_type (and the context is irrelevant then). If both are set, the portal_type might get ignored, depending on which code path is taken. """ log.debug("getAdditionalSchemata with context %r and portal_type %s", context, portal_type) if context is None and portal_type is None: return if context: behavior_assignable = IBehaviorAssignable(context, None) else: behavior_assignable = None if behavior_assignable is None: log.debug("No behavior assignable found, only checking fti.") # Usually an add-form. if portal_type is None: portal_type = context.portal_type for schema_interface in SCHEMA_CACHE.behavior_schema_interfaces( portal_type ): form_schema = IFormFieldProvider(schema_interface, None) if form_schema is not None: yield form_schema else: log.debug("Behavior assignable found for context.") for behavior_reg in behavior_assignable.enumerateBehaviors(): form_schema = IFormFieldProvider(behavior_reg.interface, None) if form_schema is not None: yield form_schema
def get_ordered_fields(fti): # this code is much complicated because we have to get sure # we get the fields in the order of the fieldsets # the order of the fields in the fieldsets can differ # of the getFieldsInOrder(schema) order... # that's because fields from different schemas # can take place in the same fieldset schema = fti.lookupSchema() fieldset_fields = {} ordered_fieldsets = ['default'] labels = {'default': u'Default'} for fieldset in schema.queryTaggedValue(FIELDSETS_KEY, []): ordered_fieldsets.append(fieldset.__name__) labels[fieldset.__name__] = fieldset.label fieldset_fields[fieldset.__name__] = fieldset.fields fieldset_fields['default'] = non_fieldset_fields(schema) # Get the behavior fields fields = getFieldsInOrder(schema) for behavior_id in fti.behaviors: schema = getUtility(IBehavior, behavior_id).interface if not IFormFieldProvider.providedBy(schema): continue fields.extend(getFieldsInOrder(schema)) for fieldset in schema.queryTaggedValue(FIELDSETS_KEY, []): fieldset_fields.setdefault(fieldset.__name__, []).extend( fieldset.fields) if fieldset.__name__ not in ordered_fieldsets: ordered_fieldsets.append(fieldset.__name__) labels[fieldset.__name__] = fieldset.label fieldset_fields['default'].extend(non_fieldset_fields(schema)) ordered_fields = [] for fieldset in ordered_fieldsets: ordered_fields.extend(fieldset_fields[fieldset]) ordered_fieldsets_fields = [{ 'id': fieldset, 'fields': fieldset_fields[fieldset], 'title': labels[fieldset], } for fieldset in ordered_fieldsets] fields.sort(key=lambda field: ordered_fields.index(field[0])) return (fields, ordered_fieldsets_fields)
def test_behaviors_installation(self): contact_details_behavior = getUtility(IBehavior, name='collective.contact.core.behaviors.IContactDetails') global_positioning_behavior = getUtility(IBehavior, name='collective.contact.core.behaviors.IGlobalPositioning') birthday_behavior = getUtility(IBehavior, name='collective.contact.core.behaviors.IBirthday') self.assertEqual(contact_details_behavior.interface, IContactDetails) self.assertEqual(global_positioning_behavior.interface, IGlobalPositioning) self.assertEqual(birthday_behavior.interface, IBirthday) IFormFieldProvider.providedBy(contact_details_behavior.interface) IFormFieldProvider.providedBy(global_positioning_behavior.interface) IFormFieldProvider.providedBy(birthday_behavior.interface)
def test_behaviors_installation(self): contact_details_behavior = getUtility( IBehavior, name='collective.contact.core.behaviors.IContactDetails') global_positioning_behavior = getUtility( IBehavior, name='collective.contact.core.behaviors.IGlobalPositioning') birthday_behavior = getUtility( IBehavior, name='collective.contact.core.behaviors.IBirthday') self.assertEqual(contact_details_behavior.interface, IContactDetails) self.assertEqual(global_positioning_behavior.interface, IGlobalPositioning) self.assertEqual(birthday_behavior.interface, IBirthday) IFormFieldProvider.providedBy(contact_details_behavior.interface) IFormFieldProvider.providedBy(global_positioning_behavior.interface) IFormFieldProvider.providedBy(birthday_behavior.interface)
class ConnectTranslation(AutoExtensibleForm, Form): schema = IFormFieldProvider(IConnectTranslation) ignoreContext = True label = _(u"label_connect_translation", default=u"Connect translation") description = _( u"long_description_connect_translation", default=u"This form allows you to connect a currently existing " u"translations of the current object." ) @button.buttonAndHandler(_(u"connect_translation", default=u"Connect translation")) def handle_add(self, action): data, errors = self.extractData() if not errors: content = data['content'] language = data['language'] ITranslationManager(self.context)\ .register_translation(language, content) ILanguage(content).set_language(language) return self.request.response.redirect( self.context.absolute_url() + '/modify_translations')
def get_ordered_fields(fti, context): # this code is much complicated because we have to get sure # we get the fields in the order of the fieldsets # the order of the fields in the fieldsets can differ # of the getFieldsInOrder(schema) order... # that's because fields from different schemas # can take place in the same fieldset schema = fti.lookupSchema() fieldset_fields = {} ordered_fieldsets = ['default'] for fieldset in schema.queryTaggedValue(FIELDSETS_KEY, []): ordered_fieldsets.append(fieldset.__name__) fieldset_fields[fieldset.__name__] = fieldset.fields fieldset_fields['default'] = non_fieldset_fields(schema, context) # Get the behavior fields fields = get_fields_in_order_with_permission(schema, context) for behavior_id in fti.behaviors: schema = getUtility(IBehavior, behavior_id).interface if not IFormFieldProvider.providedBy(schema): continue fields.extend(get_fields_in_order_with_permission(schema, context)) for fieldset in schema.queryTaggedValue(FIELDSETS_KEY, []): fieldset_fields.setdefault(fieldset.__name__, []).extend(fieldset.fields) ordered_fieldsets.append(fieldset.__name__) fieldset_fields['default'].extend(non_fieldset_fields(schema, context)) ordered_fields = [] for fieldset in ordered_fieldsets: ordered_fields.extend(fieldset_fields[fieldset]) fields.sort(key=lambda field: ordered_fields.index(field[0])) return fields
def test_alsoProvides(self): from plone.autoform.interfaces import IFormFieldProvider from collective.behavior.price.behavior import IPrice self.assertTrue(IFormFieldProvider.providedBy(IPrice))
def test_behaviors_installation(self): sending_type_behavior = getUtility(IBehavior, name='collective.dms.mailcontent.behaviors.ISendingType') self.assertEqual(sending_type_behavior.interface, ISendingType) IFormFieldProvider.providedBy(sending_type_behavior.interface)