def validate_categories_subset(obj): """Ensure that unique/required categories are a subset of the actual categories used in tags. """ from collective.gtags.utils import get_categories tags = obj.tags if tags is None: tags = set() categories = get_categories(tags) if obj.unique_categories: for category in obj.unique_categories: if category not in categories: raise InvalidCategories( _(u"${category} is not a valid category", mapping={'category': category})) if obj.required_categories: for category in obj.required_categories: if category not in categories: raise InvalidCategories( _(u"${category} is not a valid category", mapping={'category': category}))
def __init__(self, disallowed, nonunique, required): self.disallowed = disallowed self.nonunique = nonunique self.required = required # This is obscene... better solutions on a postcard, please. ids = ['tags_error'] messages = [] mapping = {} if self.disallowed: ids.append('disallowed') messages.append(u"Tags not allowed: ${disallowed}") mapping['disallowed'] = ', '.join(self.disallowed) if self.nonunique: ids.append('nonunique') messages.append(u"You may only have one tag in the following categories: ${nonunique}") mapping['nonunique'] = ', '.join(self.nonunique) if self.required: ids.append('required') messages.append(u"You must have at least one tag in the following categories: ${required}") mapping['required'] = ', '.join(self.required) self.error_message = _('_'.join(ids), default='; '.join(messages), mapping=mapping)
class ITags(form.Schema): """Add tags to content """ form.fieldset( 'categorization', label=_(u'Categorization'), fields=('tags', ), ) tags = Tags( title=_(u"Tags"), description=_(u"Applicable tags"), required=False, allow_uncommon=True, )
def __init__(self, disallowed, nonunique, required): self.disallowed = disallowed self.nonunique = nonunique self.required = required # This is obscene... better solutions on a postcard, please. ids = ['tags_error'] messages = [] mapping = {} if self.disallowed: ids.append('disallowed') messages.append(u"Tags not allowed: ${disallowed}") mapping['disallowed'] = ', '.join(self.disallowed) if self.nonunique: ids.append('nonunique') messages.append( u"You may only have one tag in the following categories: ${nonunique}" ) mapping['nonunique'] = ', '.join(self.nonunique) if self.required: ids.append('required') messages.append( u"You must have at least one tag in the following categories: ${required}" ) mapping['required'] = ', '.join(self.required) self.error_message = _('_'.join(ids), default='; '.join(messages), mapping=mapping)
class ITags(ISet): """A field containing a set of tags """ validate_categories = schema.Bool( title=_(u"Validate categories"), description=_(u"Set to False to disable validation against globally " "specified unique and required categories."), default=True, required=False, ) allow_uncommon = schema.Bool( title=_(u"Allow uncommon tags"), description=_(u"If enabled, it will be possible to add " "tags other than those in the pre-defined tags list"), required=False)
class ITags(form.Schema): """Add tags to content """ form.fieldset( 'categorization', label=_(u'Categorization'), fields=('tags', ), ) tags = Tags( title=_(u"Tags"), value_type=schema.TextLine(), description=_(u"Applicable tags"), required=False, allow_uncommon=False, ) directives.widget('tags', AjaxSelectFieldWidget, vocabulary='collective.gtags.Keywords')
class TagSettingsEditForm(controlpanel.RegistryEditForm): schema = ITagSettings label = _(u"Tagging settings") description = _(u"Please enter details of available tags") def updateFields(self): super(TagSettingsEditForm, self).updateFields() self.fields['tags'].widgetFactory = TextLinesFieldWidget self.fields['unique_categories'].widgetFactory = TextLinesFieldWidget self.fields['required_categories'].widgetFactory = TextLinesFieldWidget def updateWidgets(self): super(TagSettingsEditForm, self).updateWidgets() self.widgets['tags'].rows = 8 self.widgets['tags'].style = u'width: 30%;' self.widgets['unique_categories'].rows = 8 self.widgets['unique_categories'].style = u'width: 30%;' self.widgets['required_categories'].rows = 8 self.widgets['required_categories'].style = u'width: 30%;'
def __init__(self, validate_categories=True, allow_uncommon=None, **kw): self.validate_categories = validate_categories self.allow_uncommon = allow_uncommon # Avoid validation for the 'default' property, if set self._init_field = True super(Tags, self).__init__(**kw) self._init_field = False if self.value_type is None: self.value_type = Choice( title=_(u"Tag"), source=TagsSourceBinder(allow_uncommon=allow_uncommon))
def __init__(self, validate_categories=True, allow_uncommon=None, **kw): self.validate_categories = validate_categories self.allow_uncommon = allow_uncommon # Avoid validation for the 'default' property, if set self._init_field = True super(Tags, self).__init__(**kw) self._init_field = False if self.value_type is None: self.value_type = Choice( title=_(u"Tag"), source=TagsSourceBinder(allow_uncommon=allow_uncommon) )
def validate_categories_subset(obj): """Ensure that unique/required categories are a subset of the actual categories used in tags. """ from collective.gtags.utils import get_categories tags = obj.tags if tags is None: tags = set() categories = get_categories(tags) if obj.unique_categories: for category in obj.unique_categories: if category not in categories: raise InvalidCategories(_(u"${category} is not a valid category", mapping={'category': category})) if obj.required_categories: for category in obj.required_categories: if category not in categories: raise InvalidCategories(_(u"${category} is not a valid category", mapping={'category': category}))
class TagsError(ValidationError): __doc__ = _(u"Invalid tags") def __init__(self, disallowed, nonunique, required): self.disallowed = disallowed self.nonunique = nonunique self.required = required # This is obscene... better solutions on a postcard, please. ids = ['tags_error'] messages = [] mapping = {} if self.disallowed: ids.append('disallowed') messages.append(u"Tags not allowed: ${disallowed}") mapping['disallowed'] = ', '.join(self.disallowed) if self.nonunique: ids.append('nonunique') messages.append( u"You may only have one tag in the following categories: ${nonunique}" ) mapping['nonunique'] = ', '.join(self.nonunique) if self.required: ids.append('required') messages.append( u"You must have at least one tag in the following categories: ${required}" ) mapping['required'] = ', '.join(self.required) self.error_message = _('_'.join(ids), default='; '.join(messages), mapping=mapping) def __str__(self): message = self.error_message default = message.default mapping = message.mapping for k, v in mapping.items(): default = default.replace('${%s}' % k, v) return unicode(default)
class ITagSettings(Interface): """A utility used to manage which tags are available and how categories are treated. """ allow_uncommon = schema.Bool( title=_(u"Allow uncommon tags globally"), description=_(u"If enabled, it will be possible to add " "tags other than those in the list below to " "content objects that support this. If disabled, " "all tags must be in the pre-defined list."), default=True, required=False, ) tags = schema.Set( title=_(u"Pre-defined tags"), description=_(u"List pre-defined tags here. Tags can either be " "simple strings or categorised using the form " "Category-TagName."), required=True, default=set(), value_type=schema.TextLine(title=_(u"Tag")), ) unique_categories = schema.Set( title=_(u"Unique categories"), description=_(u"If you want to ensure that users pick at most " "one tag in a given category, list the category " "name here."), required=True, default=set(), value_type=schema.TextLine(title=_(u"Category")), ) required_categories = schema.Set( title=u"Required categories", description=_(u"If you want to ensure that users pick " "at least one tag in a given category, " "list the category name here."), required=True, default=set(), value_type=schema.TextLine(title=_(u"Category")), ) @invariant def validate_categories_subset(obj): """Ensure that unique/required categories are a subset of the actual categories used in tags. """ from collective.gtags.utils import get_categories tags = obj.tags if tags is None: tags = set() categories = get_categories(tags) if obj.unique_categories: for category in obj.unique_categories: if category not in categories: raise InvalidCategories( _(u"${category} is not a valid category", mapping={'category': category})) if obj.required_categories: for category in obj.required_categories: if category not in categories: raise InvalidCategories( _(u"${category} is not a valid category", mapping={'category': category}))
class InvalidCategories(Invalid): __doc__ = _( u"There must be a least one tag available for each category selected.")