# -*- 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.Widget import RichWidget as BaseRichWidget 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 TargetWidget(LinesWidget): _properties = LinesWidget._properties.copy() _properties.update({ 'macro' : 'widget_target', }) registerWidget(TargetWidget, title='Target Widget', description='Target Widget', used_for=('LinesField',) ) class TrackbackLinkWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro' : 'widget_trackbacklink', }) registerWidget(TrackbackLinkWidget, title='TrackbackLink Widget', description='TrackbackLink Widget', used_for=('ComputedField',) ) class TagsWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro' : 'widget_tags', }) registerWidget(TagsWidget, title='Tags Widget', description='Tags Widget', used_for=('Products.Eportfolio.TagsField.TagsField',) ) class HelpTagsWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro' : 'widget_helptags', }) registerWidget(TagsWidget, title='Tags Widget', description='Tags Widget', used_for=('Products.Eportfolio.TagsField.TagsField',) ) class HelpWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro' : 'widget_help', }) registerWidget(HelpWidget, title='Help Widget', description='Help Widget', used_for=('Products.Eportfolio.StringField.StringField',) ) class TrackbackWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro' : 'widget_trackback', }) registerWidget(TrackbackWidget, title='Trackback Widget', description='Trackback Widget', used_for=('Products.Eportfolio.DictField.DictField',) ) class TrackbacksMadeToWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro' : 'widget_trackbacks_made_to', }) registerWidget(TrackbacksMadeToWidget, title='Trackbacks Made To Widget', description='Trackbacks Made To Widget', used_for=('Products.Eportfolio.DictField.DictField',) ) class StructuredTaskWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro' : 'widget_structured_task', }) registerWidget(StructuredTaskWidget, title='Structured task Widget', description='Structured task Widget', used_for=('Products.Eportfolio.DictField.DictField',) ) class LinksWidget(StringWidget): _properties = StringWidget._properties.copy() _properties.update({ 'macro' : 'widget_links', }) registerWidget(LinksWidget, title='Links Widget', description='Links Widget', used_for=('Products.Eportfolio.DictField.DictField',) ) 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'), ) class DictField(LinesField): """ dictionary field """ _properties = LinesField._properties.copy() _properties.update({ 'widget' : TrackbackWidget, }) security = ClassSecurityInfo() def setValue(self, instance, key, value): fname = self.getName() i = aq_base(instance) if not shasattr(i, '_ivalues'): i._ivalues = {} if not i._ivalues.has_key(fname): i._ivalues[fname] = {} i._ivalues[fname][key] = value i._p_changed = 1 def delValue(self, instance, keys): fname=self.getName() i = aq_base(instance) if not shasattr(i,'_ivalues'):return if i._ivalues.has_key(fname): for key in keys: del i._ivalues[fname][key] i._p_changed=1 def getKeys(self, instance): i = aq_base(instance) fname = self.getName() if not shasattr(i, '_ivalues'): return [] elif not i._ivalues.has_key(fname): return [] else: return i._ivalues[fname].keys() def get(self, instance, **kwargs): i= instance fname = self.getName() if not shasattr(i, '_ivalues'): i._ivalues = {} i._p_changed = 1 if not i._ivalues.has_key(fname): i._ivalues[fname] = {} i._p_changed = 1 return aq_base(instance)._ivalues[fname] getRaw = get def getValue(self, instance, key): fname = self.getName() i = aq_base(instance) if not shasattr(i, '_ivalues'): i._ivalues = {} i._p_changed = 1 return "" if not i._ivalues.has_key(fname): i._ivalues[fname] = {} i._p_changed = 1 if not i._ivalues[fname].has_key(key): return "" return i._ivalues[fname][key] registerField(DictField, title='Dict Field', description=('Dict Field'), ) class TwoWayReferenceField(ReferenceField): """ A field that stores competency links """ __implements__ = ReferenceField.__implements__ _properties = ReferenceField._properties.copy() _properties.update({ 'multiValued' : True, }) security = ClassSecurityInfo() def _field_helper(self, instance): fname = self.getName() i = aq_base(instance) if not shasattr(i, '_refvalues'): i._refvalues = {} if not i._refvalues.has_key(fname): i._refvalues[fname] = [] return i, fname def setValue(self, instance, value, append=0): """ sets value """ i, fname = self._field_helper(instance) if append: i._refvalues[fname].append(value) else: i._refvalues[fname] = value i._p_changed = 1 def getValue(self, instance, **kwargs): """ get value """ i, fname = self._field_helper(instance) return i._refvalues[fname] registerField(TwoWayReferenceField, title='Two Way Reference Field', description='Two Way Reference Field'), class AnswerReferenceWidget(InAndOutWidget): _properties = InAndOutWidget._properties.copy() _properties.update({ 'macro' : "widget_answerreference", }) registerWidget(AnswerReferenceWidget, title='Answer Reference Widget', description='Answer Reference Widget', used_for=('ReferenceField',) ) class RichWidget(BaseRichWidget): _properties = BaseRichWidget._properties.copy() _properties.update({ 'macro' : 'widget_rich', })