Back to index

plone3  3.1.7
markup.py
Go to the documentation of this file.
00001 from plone.fieldsets.fieldsets import FormFieldsets
00002 
00003 from zope.interface import Interface
00004 from zope.component import adapts
00005 from zope.interface import implements
00006 from zope.schema import Choice
00007 from zope.schema import Tuple
00008 
00009 from zope.schema.interfaces import IVocabularyFactory
00010 from zope.schema.vocabulary import SimpleVocabulary
00011 from zope.schema.vocabulary import SimpleTerm
00012 
00013 from Products.CMFCore.utils import getToolByName
00014 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00015 from Products.CMFPlone import PloneMessageFactory as _
00016 from Products.CMFPlone.interfaces import IPloneSiteRoot
00017 
00018 from plone.app.controlpanel.form import ControlPanelForm
00019 from plone.app.controlpanel.widgets import AllowedTypesWidget
00020 from plone.app.controlpanel.widgets import MultiCheckBoxVocabularyWidget
00021 
00022 # For Archetypes markup
00023 
00024 from Products.Archetypes.mimetype_utils import getDefaultContentType, \
00025     setDefaultContentType, getAllowedContentTypes, getAllowableContentTypes, \
00026     setForbiddenContentTypes
00027 
00028 # For Wicked
00029 
00030 from persistent import Persistent
00031 from zope.annotation.interfaces import IAnnotations
00032 
00033 from wicked.plone.registration import basic_type_regs as wicked_basic_type_regs
00034 from wicked.txtfilter import BrackettedWickedFilter
00035 
00036 WICKED_SETTING_KEY="plone.app.controlpanel.wicked"
00037 
00038 class WickedSettings(Persistent):
00039     """Settings for Wicked markup
00040     """
00041     types_enabled = []
00042     enable_mediawiki = False
00043 
00044 wicked_type_regs = dict((factory.type, factory) for factory in \
00045                         wicked_basic_type_regs)
00046 
00047 class WickedTypesVocabulary(object):
00048     """Vocabulary factory for wickedized portal types.
00049     """
00050     implements(IVocabularyFactory)
00051 
00052     def __call__(self, context):
00053         context = getattr(context, 'context', context)
00054         ttool = getToolByName(context, 'portal_types')
00055         items = []
00056         # Pretty insane code, but wicked uses different internal names for
00057         # the types :(
00058         for t in ttool.listContentTypes():
00059             for reg in wicked_basic_type_regs:
00060                 if reg.type_id == t:
00061                     items.append(SimpleTerm(reg.type, reg.type, ttool[t].Title()))
00062         return SimpleVocabulary(items)
00063 
00064 WickedTypesVocabularyFactory = WickedTypesVocabulary()
00065 
00066 #
00067 # Archetypes markup types
00068 #
00069 
00070 class ITextMarkupSchema(Interface):
00071 
00072     default_type = Choice(title=_(u'Default format'),
00073         description=_(u"Select the default format of textfields for newly "
00074                        "created content objects."),
00075         default=u'text/html',
00076         missing_value=set(),
00077         vocabulary="plone.app.vocabularies.AllowableContentTypes",
00078         required=True)
00079 
00080     allowed_types = Tuple(title=_(u'Alternative formats'),
00081         description=_(u"Select which formats are available for users as "
00082                        "alternative to the default format. Note that if new "
00083                        "formats are installed, they will be enabled for text "
00084                        "fields by default unless explicitly turned off here "
00085                        "or by the relevant installer."),
00086         required=True,
00087         missing_value=set(),
00088         value_type=Choice(
00089             vocabulary="plone.app.vocabularies.AllowableContentTypes"))
00090 
00091 #
00092 # Wicked behaviour
00093 #
00094 
00095 class IWikiMarkupSchema(Interface):
00096 
00097     wiki_enabled_types = Tuple(title=_(u"Choose which types will have wiki "
00098                                         "behavior."),
00099                                description=_(u"Each type chosen will have a "
00100                                              "wiki enabled primary text area. "
00101                                              "At least one type must be chosen "
00102                                              "to turn wiki behavior on."),
00103                                required=False,
00104                                missing_value=tuple(),
00105                                value_type=Choice(vocabulary="plone.app.\
00106 controlpanel.WickedPortalTypes"))
00107 
00108 #
00109 # Combined schemata and fieldsets
00110 #
00111 
00112 class IMarkupSchema(ITextMarkupSchema, IWikiMarkupSchema):
00113     """Combined schema for the adapter lookup.
00114     """
00115 
00116 class MarkupControlPanelAdapter(SchemaAdapterBase):
00117 
00118     adapts(IPloneSiteRoot)
00119     implements(IMarkupSchema)
00120 
00121     def __init__(self, context):
00122         super(MarkupControlPanelAdapter, self).__init__(context)
00123         self.context = context
00124         self.toggle_mediawiki = False
00125 
00126     # Text markup settings
00127 
00128     def get_default_type(self):
00129         return getDefaultContentType(self.context)
00130 
00131     def set_default_type(self, value):
00132         setDefaultContentType(self.context, value)
00133 
00134     default_type = property(get_default_type, set_default_type)
00135 
00136     def get_allowed_types(self):
00137         return getAllowedContentTypes(self.context)
00138 
00139     def set_allowed_types(self, value):
00140         # The menu pretends to be a whitelist, but we are storing a blacklist
00141         # so that new types are available by default. So, we inverse the list.
00142         allowable_types = getAllowableContentTypes(self.context)
00143         forbidden_types = [t for t in allowable_types if t not in value]
00144         setForbiddenContentTypes(self.context, forbidden_types)
00145 
00146     allowed_types = property(get_allowed_types, set_allowed_types)
00147 
00148     # Wiki settings
00149 
00150     def get_enable_mediawiki(self):
00151         return self.wicked_settings.enable_mediawiki
00152 
00153     def set_enable_mediawiki(self, value):
00154         settings = self.wicked_settings
00155         if settings.enable_mediawiki != value:
00156             self.toggle_mediawiki = True
00157             settings.enable_mediawiki = value
00158 
00159     enable_mediawiki = property(get_enable_mediawiki, set_enable_mediawiki)
00160 
00161     def get_wiki_enabled_types(self):
00162         return self.wicked_settings.types_enabled
00163 
00164     def set_wiki_enabled_types(self, value):
00165         settings = self.wicked_settings
00166         if not self.toggle_mediawiki and value == settings.types_enabled:
00167             return
00168 
00169         self.unregister_wicked_types() # @@ use sets to avoid thrashing
00170         for name in value:
00171             reg = wicked_type_regs[name](self.context)
00172             if self.enable_mediawiki:
00173                 reg.txtfilter = BrackettedWickedFilter
00174             reg.handle()
00175 
00176         self.toggle_mediawiki = False
00177         settings.types_enabled = value
00178 
00179     wiki_enabled_types = property(get_wiki_enabled_types,
00180                                   set_wiki_enabled_types)
00181 
00182     @property
00183     def wicked_settings(self):
00184         ann = IAnnotations(self.context)
00185         return ann.setdefault(WICKED_SETTING_KEY, WickedSettings())
00186 
00187     def unregister_wicked_types(self):
00188         """Unregisters all previous registration objects
00189         """
00190         for name in wicked_type_regs.keys():
00191             wicked_type_regs[name](self.context).handle(unregister=True)
00192 
00193 textset = FormFieldsets(ITextMarkupSchema)
00194 textset.id = 'textmarkup'
00195 textset.label = _(u'Text markup')
00196 
00197 wikiset = FormFieldsets(IWikiMarkupSchema)
00198 wikiset.id = 'wiki'
00199 wikiset.label = _(u'Wiki behavior')
00200 
00201 class MarkupControlPanel(ControlPanelForm):
00202 
00203     form_fields = FormFieldsets(textset, wikiset)
00204     form_fields['allowed_types'].custom_widget = AllowedTypesWidget
00205     form_fields['wiki_enabled_types'].custom_widget = MultiCheckBoxVocabularyWidget
00206 
00207     label = _("Markup settings")
00208     description = _("Lets you control what markup is available when editing "
00209                     "content.")
00210     form_name = _("Markup settings")