# -*- coding: utf-8 # Copyright 2006 by Vahur Rebas from AccessControl import ClassSecurityInfo from Products.Archetypes.config import REFERENCE_CATALOG from Products.Archetypes.Registry import registerField, registerWidget from Products.Archetypes.Field import StringField, LinesField, ReferenceField, ObjectField,FileField,ComputedField from Products.Archetypes.Widget import TextAreaWidget, StringWidget, LinesWidget, SelectionWidget, TypesWidget, MultiSelectionWidget, VisualWidget, FileWidget, InAndOutWidget from Products.Archetypes.ReferenceEngine import Reference from Products.Archetypes import config from string import letters, punctuation from Products.CMFCore.utils import getToolByName from ZODB.PersistentMapping import PersistentMapping from Products.Archetypes.utils import shasattr from Acquisition import aq_base from types import ListType, TupleType, StringType, UnicodeType, InstanceType import re STRING_TYPES = [StringType, UnicodeType] class TagsWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro' : 'widget_tags', }) registerWidget(TagsWidget, title='Tags Widget', description='Tags Widget', used_for=('Products.Eportfoolio.TagsField.TagsField',) ) class TagsField(LinesField): """ A field that stores tags """ __implements__ = LinesField.__implements__ _properties = LinesField._properties.copy() _properties.update({ 'widget' : TagsWidget, }) security = ClassSecurityInfo() def set(self, instance, value, **kwargs): tags = [] tags_dirty = [] if isinstance(value,str): value = value.lower() tags_dirty = value.split(',') value = tags_dirty tags = [] if isinstance(value, tuple) or isinstance(value, list): tags_dirty = value [ tags.append(t.strip().lower()) for t in tags_dirty if t.strip().lower() not in tags ] value = ','.join(tags) if isinstance(value,str): value = value.replace(',','\n') LinesField.set(self, instance, value, **kwargs) def getRaw(self, instance, **kwargs): value = self.get(instance, **kwargs) return ', '.join(value) def get(self, instance, **kwargs): return LinesField.get(self, instance, **kwargs) registerField(TagsField, title='Tags Field', description=('Tags Field'), )