def _base_args(self, context, field, request): args = super(RelatedItemsWidget, self)._base_args(context, field, request) value = request.get(field.getName(), None) if value is None: value = [IUUID(o) for o in field.getAccessor(context)() if o] else: value = [v.split('/')[0] for v in value.strip().split(self.separator)] vocabulary_factory = getattr(field, 'vocabulary_factory', None) if not self.vocabulary: self.vocabulary = vocabulary_factory args['name'] = field.getName() args['value'] = self.separator.join(value) args.setdefault('pattern_options', {}) args['pattern_options']['orderable'] = self.allow_sorting args['pattern_options'] = dict_merge( get_relateditems_options(context, args['value'], self.separator, self.vocabulary, self.vocabulary_view, field.getName()), args['pattern_options']) return args
def _base_args(self, context, field, request): args = super(RelatedItemsWidget, self)._base_args(context, field, request) value = request.get(field.getName(), None) if value is None: value = [IUUID(o) for o in field.getAccessor(context)() if o] else: value = [ v.split('/')[0] for v in value.strip().split(self.separator) ] vocabulary_factory = getattr(field, 'vocabulary_factory', None) if not self.vocabulary: self.vocabulary = vocabulary_factory args['name'] = field.getName() args['value'] = self.separator.join(value) args.setdefault('pattern_options', {}) args['pattern_options']['orderable'] = self.allow_sorting args['pattern_options'] = dict_merge( get_relateditems_options(context, args['value'], self.separator, self.vocabulary, self.vocabulary_view, field.getName()), args['pattern_options']) return args
def _base_args(self): """Method which will calculate _base class arguments. Returns (as python dictionary): - `pattern`: pattern name - `pattern_options`: pattern options - `name`: field name - `value`: field value :returns: Arguments which will be passed to _base :rtype: dict """ args = super(RelatedItemsWidget, self)._base_args() args['name'] = self.name args['value'] = self.value args.setdefault('pattern_options', {}) if IChoice.providedBy(self.field): args['pattern_options']['maximumSelectionSize'] = 1 field_name = self.field and self.field.__name__ or None args['pattern_options'] = dict_merge( get_relateditems_options(self.context, args['value'], self.separator, self.vocabulary, self.vocabulary_view, field_name), args['pattern_options']) return args
def getRelatedRtemsOptions(context): return get_relateditems_options( context=context, value=None, separator=";", vocabulary_name="plone.app.vocabularies.Catalog", vocabulary_view="@@getVocabulary", field_name="relatedItems", )
def _base_args(self): """Method which will calculate _base class arguments. Returns (as python dictionary): - `pattern`: pattern name - `pattern_options`: pattern options - `name`: field name - `value`: field value :returns: Arguments which will be passed to _base :rtype: dict """ args = super(RelatedItemsWidget, self)._base_args() args['name'] = self.name args['value'] = self.value args.setdefault('pattern_options', {}) field = None if IChoice.providedBy(self.field): args['pattern_options']['maximumSelectionSize'] = 1 field = self.field elif ICollection.providedBy(self.field): field = self.field.value_type vocabulary_name = self.vocabulary field_name = self.field and self.field.__name__ or None context = self.context view_context = get_widget_form(self) # For EditForms and non-Forms (in tests), the vocabulary is looked # up on the context, otherwise on the view if (IEditForm.providedBy(view_context) or not IForm.providedBy(view_context)): view_context = context args['pattern_options'] = dict_merge( get_relateditems_options( view_context, args['value'], self.separator, vocabulary_name, self.vocabulary_view, field_name, ), args['pattern_options']) if (not self.vocabulary_override and field and getattr(field, 'vocabulary', None)): # widget vocab takes precedence over field form_url = self.request.getURL() source_url = '{0:s}/++widget++{1:s}/@@getSource'.format( form_url, self.name) args['pattern_options']['vocabularyUrl'] = source_url return args
def _base_args(self): """Method which will calculate _base class arguments. Returns (as python dictionary): - `pattern`: pattern name - `pattern_options`: pattern options - `name`: field name - `value`: field value :returns: Arguments which will be passed to _base :rtype: dict """ args = super(RelatedItemsWidget, self)._base_args() args['name'] = self.name args['value'] = self.value args.setdefault('pattern_options', {}) field = None if IChoice.providedBy(self.field): args['pattern_options']['maximumSelectionSize'] = 1 field = self.field elif ICollection.providedBy(self.field): field = self.field.value_type vocabulary_name = self.vocabulary if not vocabulary_name: if field is not None and field.vocabularyName: vocabulary_name = field.vocabularyName else: vocabulary_name = 'plone.app.vocabularies.Catalog' field_name = self.field and self.field.__name__ or None args['pattern_options'] = dict_merge( get_relateditems_options(self.context, args['value'], self.separator, vocabulary_name, self.vocabulary_view, field_name), args['pattern_options']) if not self.vocabulary: # widget vocab takes precedence over field if field and getattr(field, 'vocabulary', None): form_url = self.request.getURL() source_url = "%s/++widget++%s/@@getSource" % ( form_url, self.name) args['pattern_options']['vocabularyUrl'] = source_url return args
def test__subfolder_relateditems_options(self): """Test related items options on subfolder: Vocab called on root, start path is folder, have favorites. """ portal = self.layer['portal'] portal.invokeFactory('Folder', 'sub') sub = portal.sub options = get_relateditems_options(sub, None, '#!@', 'test_vocab', '@@vocab', 'testfield') # vocab is correctly set self.assertTrue('@@vocab?name=test_vocab&field=testfield' in options['vocabularyUrl']) # rootUrl contains something self.assertTrue(bool(options['rootUrl'])) root_path = '/'.join(portal.getPhysicalPath()) root_url = portal.absolute_url() context_path = '/'.join(sub.getPhysicalPath()) context_url = sub.absolute_url() # context_path contains something, otherwise this test is meaningless self.assertTrue(bool(context_path)) # context_url contains something, otherwise this test is meaningless self.assertTrue(bool(context_url)) self.assertEquals(options['rootUrl'], root_url) self.assertEquals(options['rootPath'], root_path) self.assertEquals( options['vocabularyUrl'], root_url + '/@@vocab?name=test_vocab&field=testfield') self.assertEquals(options['basePath'], context_path) self.assertEquals(options['contextPath'], context_path) self.assertEquals(options['separator'], '#!@') self.assertEquals(len(options['favorites']), 2) self.assertEquals(sorted(options['favorites'][0].keys()), ['path', 'title'])
def _base_args(self): """Method which will calculate _base class arguments. Returns (as python dictionary): - `pattern`: pattern name - `pattern_options`: pattern options - `name`: field name - `value`: field value :returns: Arguments which will be passed to _base :rtype: dict """ args = super(RelatedItemsWidget, self)._base_args() args["name"] = self.name args["value"] = self.value args.setdefault("pattern_options", {}) vocabulary_name = self.vocabulary field = None if IChoice.providedBy(self.field): args["pattern_options"]["maximumSelectionSize"] = 1 field = self.field elif ICollection.providedBy(self.field): field = self.field.value_type if field is not None and field.vocabularyName: vocabulary_name = field.vocabularyName field_name = self.field and self.field.__name__ or None args["pattern_options"] = dict_merge( get_relateditems_options( self.context, args["value"], self.separator, vocabulary_name, self.vocabulary_view, field_name ), args["pattern_options"], ) if field and getattr(field, "vocabulary", None): form_url = self.request.getURL() source_url = "%s/++widget++%s/@@getSource" % (form_url, self.name) args["pattern_options"]["vocabularyUrl"] = source_url return args
def test__base_relateditems_options(self): """Test related items options on root: All URLs and paths equal root url and path, no favorites """ portal = self.layer['portal'] options = get_relateditems_options(portal, None, '#!@', 'test_vocab', '@@vocab', 'testfield') # vocab is correctly set self.assertTrue('@@vocab?name=test_vocab&field=testfield' in options['vocabularyUrl']) # rootUrl contains something self.assertTrue(bool(options['rootUrl'])) root_path = context_path = '/'.join(portal.getPhysicalPath()) root_url = context_url = portal.absolute_url() # context_path contains something, otherwise this test is meaningless self.assertTrue(bool(context_path)) # context_url contains something, otherwise this test is meaningless self.assertTrue(bool(context_url)) self.assertEquals(options['rootUrl'], root_url) self.assertEquals(options['rootPath'], root_path) self.assertEquals( options['vocabularyUrl'], root_url + '/@@vocab?name=test_vocab&field=testfield') self.assertEquals(options['basePath'], context_path) self.assertEquals(options['contextPath'], context_path) self.assertEquals(options['separator'], '#!@') self.assertTrue('favorites' not in options)
def tinymce(self): """ data-pat-tinymce : JSON.stringify({ relatedItems: { vocabularyUrl: config.portal_url + '/@@getVocabulary?name=plone.app.vocabularies.Catalog' }, tiny: config, prependToUrl: 'resolveuid/', linkAttribute: 'UID', prependToScalePart: '/@@images/image/' }) """ generator = TinyMCESettingsGenerator(self.context, self.request) settings = generator.settings folder = aq_inner(self.context) # Test if we are currently creating an Archetype object if IFactoryTempFolder.providedBy(aq_parent(folder)): folder = aq_parent(aq_parent(aq_parent(folder))) if not IFolderish.providedBy(folder): folder = aq_parent(folder) if IPloneSiteRoot.providedBy(folder): initial = None else: initial = IUUID(folder, None) portal = get_portal() portal_url = portal.absolute_url() current_path = folder.absolute_url()[len(portal_url):] image_types = settings.image_objects or [] server_url = self.request.get('SERVER_URL', '') site_path = portal_url[len(server_url):] related_items_config = get_relateditems_options( context=self.context, value=None, separator=';', vocabulary_name='plone.app.vocabularies.Catalog', vocabulary_view='@@getVocabulary', field_name=None ) related_items_config = call_callables( related_items_config, self.context ) configuration = { 'base_url': self.context.absolute_url(), 'imageTypes': image_types, 'imageScales': self.image_scales, 'linkAttribute': 'UID', # This is for loading the languages on tinymce 'loadingBaseUrl': '{}/++plone++static/components/tinymce-builded/' 'js/tinymce'.format(portal_url), 'relatedItems': related_items_config, 'prependToScalePart': '/@@images/image/', 'prependToUrl': '{}/resolveuid/'.format(site_path.rstrip('/')), 'tiny': generator.get_tiny_config(), 'upload': { 'baseUrl': portal_url, 'currentPath': current_path, 'initialFolder': initial, 'maxFiles': 1, 'relativePath': '@@fileUpload', 'showTitle': False, 'uploadMultiple': False, }, } return {'data-pat-tinymce': json.dumps(configuration)}
def tinymce(self): """ data-pat-tinymce : JSON.stringify({ relatedItems: { vocabularyUrl: config.portal_url + '/@@getVocabulary?name=plone.app.vocabularies.Catalog' }, tiny: config, prependToUrl: 'resolveuid/', linkAttribute: 'UID', prependToScalePart: '/@@images/image/' }) """ generator = TinyMCESettingsGenerator(self.context, self.request) settings = generator.settings folder = aq_inner(self.context) # Test if we are currently creating an Archetype object if IFactoryTempFolder.providedBy(aq_parent(folder)): folder = aq_parent(aq_parent(aq_parent(folder))) if not IFolderish.providedBy(folder): folder = aq_parent(folder) if IPloneSiteRoot.providedBy(folder): initial = None else: initial = IUUID(folder, None) portal = get_portal() portal_url = portal.absolute_url() current_path = folder.absolute_url()[len(portal_url):] image_types = settings.image_objects or [] server_url = self.request.get("SERVER_URL", "") site_path = portal_url[len(server_url):] related_items_config = get_relateditems_options( context=self.context, value=None, separator=";", vocabulary_name="plone.app.vocabularies.Catalog", vocabulary_view="@@getVocabulary", field_name=None, ) related_items_config = call_callables(related_items_config, self.context) configuration = { "base_url": self.context.absolute_url(), "imageTypes": image_types, "imageScales": self.image_scales, "linkAttribute": "UID", # This is for loading the languages on tinymce "loadingBaseUrl": "{}/++plone++static/components/tinymce-builded/" "js/tinymce".format(portal_url), "relatedItems": related_items_config, "prependToScalePart": "/@@images/image/", "prependToUrl": "{}/resolveuid/".format(site_path.rstrip("/")), "tiny": generator.get_tiny_config(), "upload": { "baseUrl": portal_url, "currentPath": current_path, "initialFolder": initial, "maxFiles": 1, "relativePath": "@@fileUpload", "showTitle": False, "uploadMultiple": False, }, } return {"data-pat-tinymce": json.dumps(configuration)}
def _base_args(self): """Method which will calculate _base class arguments. Returns (as python dictionary): - `pattern`: pattern name - `pattern_options`: pattern options - `name`: field name - `value`: field value :returns: Arguments which will be passed to _base :rtype: dict """ args = super(RelatedItemsWidget, self)._base_args() args['name'] = self.name args['value'] = self.value args.setdefault('pattern_options', {}) field = None if IChoice.providedBy(self.field): args['pattern_options']['maximumSelectionSize'] = 1 field = self.field elif ICollection.providedBy(self.field): field = self.field.value_type vocabulary_name = self.vocabulary field_name = self.field and self.field.__name__ or None context = self.context view_context = get_widget_form(self) # For EditForms and non-Forms (in tests), the vocabulary is looked # up on the context, otherwise on the view if IEditForm.providedBy(view_context): if self.is_subform_widget(): view_context = self.form.parentForm.context elif not ISimpleItem.providedBy(context): view_context = self.form.context else: view_context = context elif not IForm.providedBy(view_context): view_context = context else: pass # view_context is defined above already root_search_mode = (args['pattern_options'].get('mode', None) and 'basePath' not in args['pattern_options']) args['pattern_options'] = dict_merge( get_relateditems_options( view_context, args['value'], self.separator, vocabulary_name, self.vocabulary_view, field_name, ), args['pattern_options'], ) if root_search_mode: # Delete default basePath option in search mode, when no basePath # was explicitly set. del args['pattern_options']['basePath'] if (not self.vocabulary_override and field and getattr(field, 'vocabulary', None)): # widget vocab takes precedence over field form_url = self.request.getURL() source_url = '{0:s}/++widget++{1:s}/@@getSource'.format( form_url, self.name, ) args['pattern_options']['vocabularyUrl'] = source_url return args
def test__base_relateditems_options(self): """Test related items options on root: All URLs and paths equal root url and path, no favorites """ portal = self.layer['portal'] options = get_relateditems_options( portal, None, '#!@', 'test_vocab', '@@vocab', 'testfield' ) # vocab is correctly set self.assertTrue( '@@vocab?name=test_vocab&field=testfield' in options['vocabularyUrl'] ) # rootUrl contains something self.assertTrue( bool(options['rootUrl']) ) root_path = context_path = '/'.join(portal.getPhysicalPath()) root_url = context_url = portal.absolute_url() # context_path contains something, otherwise this test is meaningless self.assertTrue(bool(context_path)) # context_url contains something, otherwise this test is meaningless self.assertTrue(bool(context_url)) self.assertEqual( options['rootUrl'], root_url ) self.assertEqual( options['rootPath'], root_path ) self.assertEqual( options['vocabularyUrl'], root_url + '/@@vocab?name=test_vocab&field=testfield' ) self.assertEqual( options['basePath'], context_path ) self.assertEqual( options['contextPath'], context_path ) self.assertEqual( options['separator'], '#!@' ) self.assertTrue( 'favorites' not in options ) # Recently used is configured, but off per default. self.assertEqual( options['recentlyUsed'], False ) self.assertEqual( options['recentlyUsedKey'], 'relateditems_recentlyused_testfield_' + TEST_USER_ID )
def test__subdocument_relateditems_options(self): """Test related items options on subdoc: Vocab called on root, start path is root as document is not folderish, no favorites. """ portal = self.layer['portal'] portal.invokeFactory('Document', 'sub') sub = portal.sub options = get_relateditems_options( sub, None, '#!@', 'test_vocab', '@@vocab', 'testfield' ) # vocab is correctly set self.assertTrue( '@@vocab?name=test_vocab&field=testfield' in options['vocabularyUrl'] ) # rootUrl contains something self.assertTrue( bool(options['rootUrl']) ) root_path = '/'.join(portal.getPhysicalPath()) root_url = portal.absolute_url() context_path = '/'.join(sub.getPhysicalPath()) context_url = sub.absolute_url() # context_path contains something, otherwise this test is meaningless self.assertTrue(bool(context_path)) # context_url contains something, otherwise this test is meaningless self.assertTrue(bool(context_url)) self.assertEqual( options['rootUrl'], root_url ) self.assertEqual( options['rootPath'], root_path ) self.assertEqual( options['vocabularyUrl'], root_url + '/@@vocab?name=test_vocab&field=testfield' ) self.assertEqual( options['basePath'], root_path ) self.assertEqual( options['contextPath'], context_path ) self.assertEqual( options['separator'], '#!@' ) self.assertTrue( 'favorites' not in options )
def _base_args(self): """Method which will calculate _base class arguments. Returns (as python dictionary): - `pattern`: pattern name - `pattern_options`: pattern options - `name`: field name - `value`: field value :returns: Arguments which will be passed to _base :rtype: dict """ args = super(RelatedItemsWidget, self)._base_args() args['name'] = self.name args['value'] = self.value args.setdefault('pattern_options', {}) field = None if IChoice.providedBy(self.field): args['pattern_options']['maximumSelectionSize'] = 1 field = self.field elif ICollection.providedBy(self.field): field = self.field.value_type vocabulary_name = self.vocabulary field_name = self.field and self.field.__name__ or None context = self.context view_context = get_widget_form(self) # For EditForms and non-Forms (in tests), the vocabulary is looked # up on the context, otherwise on the view if ( IEditForm.providedBy(view_context) or not IForm.providedBy(view_context) ): view_context = context args['pattern_options'] = dict_merge( get_relateditems_options( view_context, args['value'], self.separator, vocabulary_name, self.vocabulary_view, field_name, ), args['pattern_options'] ) if ( not self.vocabulary_override and field and getattr(field, 'vocabulary', None) ): # widget vocab takes precedence over field form_url = self.request.getURL() source_url = '{0:s}/++widget++{1:s}/@@getSource'.format( form_url, self.name ) args['pattern_options']['vocabularyUrl'] = source_url return args
def tinymce(self): """ data-pat-tinymce : JSON.stringify({ relatedItems: { vocabularyUrl: config.portal_url + '/@@getVocabulary?name=plone.app.vocabularies.Catalog' }, tiny: config, prependToUrl: 'resolveuid/', linkAttribute: 'UID', prependToScalePart: '/@@images/image/' }) """ generator = TinyMCESettingsGenerator(self.context, self.request) settings = generator.settings folder = aq_inner(self.context) # Test if we are currently creating an Archetype object if IFactoryTempFolder.providedBy(aq_parent(folder)): folder = aq_parent(aq_parent(aq_parent(folder))) if not IFolderish.providedBy(folder): folder = aq_parent(folder) if IPloneSiteRoot.providedBy(folder): initial = None else: initial = IUUID(folder, None) portal = get_portal() portal_url = portal.absolute_url() current_path = folder.absolute_url()[len(portal_url):] image_types = settings.image_objects or [] server_url = self.request.get('SERVER_URL', '') site_path = portal_url[len(server_url):] related_items_config = get_relateditems_options( context=self.context, value=None, separator=';', vocabulary_name='plone.app.vocabularies.Catalog', vocabulary_view='@@getVocabulary', field_name=None ) related_items_config = call_callables( related_items_config, self.context ) configuration = { 'base_url': self.context.absolute_url(), 'imageTypes': image_types, 'imageScales': self.image_scales, 'linkAttribute': 'UID', # This is for loading the languages on tinymce 'loadingBaseUrl': '{0}/++plone++static/components/tinymce-builded/' 'js/tinymce'.format(portal_url), 'relatedItems': related_items_config, 'prependToScalePart': '/@@images/image/', 'prependToUrl': '{0}/resolveuid/'.format(site_path.rstrip('/')), 'tiny': generator.get_tiny_config(), 'upload': { 'baseUrl': portal_url, 'currentPath': current_path, 'initialFolder': initial, 'maxFiles': 1, 'relativePath': '@@fileUpload', 'showTitle': False, 'uploadMultiple': False, }, } return {'data-pat-tinymce': json.dumps(configuration)}