def get_relateditems_options(context, value, separator, vocabulary_name, vocabulary_view, field_name=None): if IForm.providedBy(context): context = context.context request = getRequest() site = get_top_site_from_url(context, request) options = get_ajaxselect_options(site, value, separator, vocabulary_name, vocabulary_view, field_name) msgstr = translate(_(u'Search'), context=request) options.setdefault('searchText', msgstr) msgstr = translate(_(u'Entire site'), context=request) options.setdefault('searchAllText', msgstr) msgstr = translate(_('tabs_home', default=u'Home'), context=request) options.setdefault('homeText', msgstr) options.setdefault('folderTypes', ['Folder']) options.setdefault('sort_on', 'sortable_title') options.setdefault('sort_order', 'ascending') nav_root = getNavigationRootObject(context, site) options['basePath'] = '/'.join(nav_root.getPhysicalPath()) if nav_root else '/' # noqa options['rootPath'] = '/'.join(site.getPhysicalPath()) if site else '/' options['rootUrl'] = site.absolute_url() if site else '' return options
def get_relateditems_options(context, value, separator, vocabulary_name, vocabulary_view, field_name=None): portal = get_portal() options = get_ajaxselect_options(portal, value, separator, vocabulary_name, vocabulary_view, field_name) if IForm.providedBy(context): context = context.context request = getRequest() msgstr = translate(_(u'Search'), context=request) options.setdefault('searchText', msgstr) msgstr = translate(_(u'Entire site'), context=request) options.setdefault('searchAllText', msgstr) msgstr = translate(_('tabs_home', default=u'Home'), context=request) options.setdefault('homeText', msgstr) options.setdefault('folderTypes', ['Folder']) options.setdefault( 'treeVocabularyUrl', '{}/@@getVocabulary?name=plone.app.vocabularies.Catalog'.format( portal is not None and portal.absolute_url() or '') ) options.setdefault('sort_on', 'sortable_title') options.setdefault('sort_order', 'ascending') nav_root = getNavigationRootObject(context, portal) options['basePath'] = ( '/'.join(nav_root.getPhysicalPath()) if nav_root else '/' ) options['rootPath'] = ( '/'.join(portal.getPhysicalPath()) if portal else '/' ) return options
def validate_input(self, formname, fieldname, fieldset=None, value=None, warning_only=True): """Given a form (view) name, a field name and the submitted value, validate the given field. """ # Abort if there was no value changed. Note that the actual value # comes along the submitted form, since a widget may require more than # a single form field to validate properly. if value is None: return context = aq_inner(self.context) request = aq_inner(self.request) alsoProvides(request, IFormLayer) # Find the form, the field and the widget form = request.traverseName(context, formname) if IFormWrapper.providedBy(form): formWrapper = form form = form.form_instance if not z2.IFixedUpRequest.providedBy(request): z2.switch_on(form, request_layer=formWrapper.request_layer) if not IForm.providedBy(form): return form.update() if getattr(form, "extractData", None): data, errors = form.extractData() else: return #if we validate a field in a group we operate on the group if fieldset is not None: fieldset = int(fieldset) form = form.groups[fieldset] index = len(form.prefix) + len(form.widgets.prefix) raw_fieldname = fieldname[index:] validationError = None for error in errors: if error.widget == form.widgets.get(raw_fieldname, None): validationError = error.message break if isinstance(validationError, Message): validationError = translate(validationError, context=self.request) # Attempt to convert the value - this will trigger validation ksscore = self.getCommandSet('core') kssplone = self.getCommandSet('plone') validate_and_issue_message(ksscore, validationError, fieldname, fieldset, kssplone, warning_only)
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 get_context_url(context): if IForm.providedBy(context): # Use the request URL if we are looking at an addform url = context.request.get('URL') elif hasattr(context, 'absolute_url'): url = context.absolute_url if callable(url): url = url() else: url = get_portal_url(context) return url
def getItemKey(self, item): form = self.table.__parent__ if not IForm.providedBy(form): return super(ItemKeyRadioColumn, self).getItemKey(item) try: result = form.widgets[self.__name__].name except KeyError: # XXX perhaps we have some sort of warning here as the form # does not define the widget we need, or that this column is # registered with a wrong name. result = super(ItemKeyRadioColumn, self).getItemKey(item) return result
def _view_context(self): 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(self.context): view_context = self.form.context else: view_context = self.context elif not IForm.providedBy(view_context): view_context = self.context return view_context
def get_relateditems_options(context, value, separator, vocabulary_name, vocabulary_view, field_name=None): if IForm.providedBy(context): context = context.context request = getRequest() site = get_top_site_from_url(context, request) options = get_ajaxselect_options(site, value, separator, vocabulary_name, vocabulary_view, field_name) nav_root = getNavigationRootObject(context, site) # basePath - start to search/browse in here. base_path_context = context if not IFolder.providedBy(base_path_context): base_path_context = aq_parent(base_path_context) if not base_path_context: base_path_context = nav_root options['basePath'] = '/'.join(base_path_context.getPhysicalPath()) # rootPath - Only display breadcrumb elements deeper than this path. options['rootPath'] = '/'.join(site.getPhysicalPath()) if site else '/' # rootUrl: Visible URL up to the rootPath. This is prepended to the # currentPath to generate submission URLs. options['rootUrl'] = site.absolute_url() if site else '' # contextPath - current edited object. Will not be available to select. options['contextPath'] = '/'.join(context.getPhysicalPath()) if base_path_context != nav_root: options['favorites'] = [ { # 'title': _(u'Current Content'), 'title': u'Aktueller Inhalt', 'path': '/'.join(base_path_context.getPhysicalPath()) }, { 'title': _(u'Start Page'), 'path': '/'.join(nav_root.getPhysicalPath()) } ] return options
def update(self): context = self.context request = self.request maincontext = self.maincontext mainview = self.mainview ws = mainview view = mainview viewclass = mainview.__class__.__bases__[0] ct = IContentType(maincontext, None) self.portal_url = '%s/' % absoluteURL(context, request) self.context_title = getMultiAdapter( (maincontext, request), IBreadcrumb).name if not ISite.providedBy(maincontext): self.notRoot = True self.portal_title = getMultiAdapter( (getSite(), request), IBreadcrumb).name self.url = '%s/' % request.URL self.base_url = '%s/' % request.URL[-1] # body id while not IWorkspace.providedBy(ws): ws = ws.__parent__ if ws is None: break self.contentClass = 'aero' if ws is not None: self.contentClass += ' section-workspace-%s' % ws.__name__.replace( '.', '-') if IConfiglet.providedBy(maincontext): self.contentClass += ' section-controlpanel-%s' % maincontext.__name__.replace( '.', '-') # body class if ct is None: ctname = maincontext.__name__ else: ctname = ct.name if IConfiglet.providedBy(maincontext): ctname = maincontext.__name__ self.contentId = '-'.join( ('section', ctname, mainview.__name__)).replace('.', '-').replace('@', '') if IForm.providedBy(mainview): self.contentClass += ' ' + mainview.mode if not viewclass is BrowserPagelet: self.contentClass += ' %s.%s' % ( viewclass.__module__, viewclass.__name__) if mainview.template: self.contentClass += ' %s' % ( os.path.split(mainview.template.filename)[1]) self.contentClass = self.contentClass.replace('.', '-').lower() self.sitePath = getPath(getSite())[1:].replace('/', '-') self.contentPath = getPath(maincontext)[1:].replace('/', '-') while not IContentWizard.providedBy(view): view = view.__parent__ if view is None: break self.wizard = view is not None self.wizard = self.wizard or isinstance(maincontext, Feeds) if self.wizard: self.contentClass += ' wizard'
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(AjaxSelectWidget, self)._base_args() args['name'] = self.name args['value'] = self.value args.setdefault('pattern_options', {}) 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 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 IChoice.providedBy(field): args['pattern_options']['allowNewItems'] = 'false' args['pattern_options'] = dict_merge( get_ajaxselect_options(view_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 = '{0:s}/++widget++{1:s}/@@getSource'.format( form_url, self.name ) args['pattern_options']['vocabularyUrl'] = source_url # ISequence represents an orderable collection if ISequence.providedBy(self.field) or self.orderable: args['pattern_options']['orderable'] = True if self.vocabulary == 'plone.app.vocabularies.Keywords': membership = getToolByName(context, 'portal_membership') user = membership.getAuthenticatedMember() registry = getUtility(IRegistry) roles_allowed_to_add_keywords = registry.get( 'plone.roles_allowed_to_add_keywords', []) roles = set(user.getRolesInContext(context)) allowNewItems = 'false' if roles.intersection(roles_allowed_to_add_keywords): allowNewItems = 'true' args['pattern_options']['allowNewItems'] = allowNewItems 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 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 get_relateditems_options(context, value, separator, vocabulary_name, vocabulary_view, field_name=None, include_recently_added=True): if IForm.providedBy(context): context = context.context request = getRequest() site = get_top_site_from_url(context, request) options = get_ajaxselect_options( site, value, separator, vocabulary_name, vocabulary_view, field_name ) nav_root = getNavigationRootObject(context, site) if not ISimpleItem.providedBy(context): context = nav_root # basePath - start to search/browse in here. base_path_context = context if not IFolder.providedBy(base_path_context): base_path_context = aq_parent(base_path_context) if not base_path_context: base_path_context = nav_root options['basePath'] = '/'.join(base_path_context.getPhysicalPath()) # rootPath - Only display breadcrumb elements deeper than this path. options['rootPath'] = '/'.join(site.getPhysicalPath()) if site else '/' # rootUrl: Visible URL up to the rootPath. This is prepended to the # currentPath to generate submission URLs. options['rootUrl'] = site.absolute_url() if site else '' # contextPath - current edited object. Will not be available to select. options['contextPath'] = '/'.join(context.getPhysicalPath()) if base_path_context != nav_root: options['favorites'] = [ { 'title': _(u'Current Content'), 'path': '/'.join(base_path_context.getPhysicalPath()) }, { 'title': _(u'Start Page'), 'path': '/'.join(nav_root.getPhysicalPath()) } ] if include_recently_added: # Options for recently used key tool = getToolByName(context, 'portal_membership') user = tool.getAuthenticatedMember() options['recentlyUsed'] = False # Keep that off in Plone 5.1 options['recentlyUsedKey'] = (u'relateditems_recentlyused_%s_%s' % ( field_name or '', user.id )) # use string substitution with %s here for automatic str casting. return options
def get_relateditems_options(context, value, separator, vocabulary_name, vocabulary_view, field_name=None, include_recently_added=True): if IForm.providedBy(context): context = context.context request = getRequest() site = get_top_site_from_url(context, request) options = { 'separator': separator, } if not vocabulary_name: # we need a vocabulary! raise ValueError('RelatedItems needs a vocabulary') options['vocabularyUrl'] = '{0}/{1}?name={2}'.format( get_context_url(site), vocabulary_view, vocabulary_name, ) if field_name: options['vocabularyUrl'] += '&field={0}'.format(field_name) if value: options['initialValues'] = {} catalog = False if vocabulary_name == 'plone.app.vocabularies.Catalog': catalog = getToolByName(getSite(), 'portal_catalog') for value in value.split(separator): title = value if catalog: result = catalog(UID=value) title = result[0].Title if result else value options['initialValues'][value] = title nav_root = getNavigationRootObject(context, site) if not ISimpleItem.providedBy(context): context = nav_root # basePath - start to search/browse in here. base_path_context = context if not IFolder.providedBy(base_path_context): base_path_context = aq_parent(base_path_context) if not base_path_context: base_path_context = nav_root options['basePath'] = '/'.join(base_path_context.getPhysicalPath()) # rootPath - Only display breadcrumb elements deeper than this path. options['rootPath'] = '/'.join(site.getPhysicalPath()) if site else '/' # rootUrl: Visible URL up to the rootPath. This is prepended to the # currentPath to generate submission URLs. options['rootUrl'] = site.absolute_url() if site else '' # contextPath - current edited object. Will not be available to select. options['contextPath'] = '/'.join(context.getPhysicalPath()) if base_path_context != nav_root: options['favorites'] = [{ 'title': _(u'Current Content'), 'path': '/'.join(base_path_context.getPhysicalPath()) }, { 'title': _(u'Start Page'), 'path': '/'.join(nav_root.getPhysicalPath()) }] if include_recently_added: # Options for recently used key tool = getToolByName(context, 'portal_membership') user = tool.getAuthenticatedMember() options['recentlyUsed'] = False # Keep that off in Plone 5.1 options['recentlyUsedKey'] = ( u'relateditems_recentlyused_%s_%s' % (field_name or '', user.id) ) # use string substitution with %s here for automatic str casting. return 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(AjaxSelectWidget, self)._base_args() args['name'] = self.name args['value'] = self.value args.setdefault('pattern_options', {}) 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 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 IChoice.providedBy(field): args['pattern_options']['allowNewItems'] = 'false' args['pattern_options'] = dict_merge( get_ajaxselect_options(view_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 = '{0:s}/++widget++{1:s}/@@getSource'.format( form_url, self.name, ) args['pattern_options']['vocabularyUrl'] = source_url # ISequence represents an orderable collection if ISequence.providedBy(self.field) or self.orderable: args['pattern_options']['orderable'] = True if self.vocabulary == 'plone.app.vocabularies.Keywords': membership = getToolByName(context, 'portal_membership') user = membership.getAuthenticatedMember() registry = getUtility(IRegistry) roles_allowed_to_add_keywords = registry.get( 'plone.roles_allowed_to_add_keywords', []) roles = set(user.getRolesInContext(context)) allowNewItems = 'false' if roles.intersection(roles_allowed_to_add_keywords): allowNewItems = 'true' args['pattern_options']['allowNewItems'] = allowNewItems return args