Created
December 2, 2010 09:47
-
-
Save malthe/725048 to your computer and use it in GitHub Desktop.
Rich text field on a property sheet in a control panel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from z3c.form import form | |
from z3c.form import field | |
from plone.app.registry.browser.controlpanel import ControlPanelFormWrapper | |
from plone.app.textfield.value import RichTextValue | |
from plone.z3cform import layout | |
from Products.CMFCore.utils import getToolByName | |
class IMyFormSchema(Interface): | |
text = RichText(title=u"Text", required=False, default=u"") | |
class MyFormPropertySheetAdapter(object): | |
def __init__(self, context): | |
self.context = context | |
text = RichTextProxy(IMyFormSchema['text']) | |
class MyForm(form.EditForm): | |
fields = field.Fields(IMyFormSchema) | |
def getContent(self): | |
props = getToolByName(self.context, 'portal_properties') | |
sheet = props['my_properties'] | |
return MyFormPropertySheetAdapter(sheet) | |
MyControlPanel = layout.wrap_form(MyForm, ControlPanelFormWrapper) | |
class RichTextProxy(object): | |
def __init__(self, context): | |
self.context = context | |
def __get__(self, inst, cls=None): | |
if inst is None: | |
return self | |
d = {} | |
for name, string in inst.context.propertyItems(): | |
s = self.context.__name__ + "_" | |
if name.startswith(s): | |
key = name[len(s):] | |
d[key] = string | |
if d: | |
return RichTextValue(**d) | |
return self.context.default | |
def __set__(self, inst, value): | |
field = self.context.bind(inst) | |
field.validate(value) | |
for key in ('raw', 'mimeType', 'outputMimeType', 'encoding'): | |
name = field.__name__ + "_" + key | |
string = getattr(value, key) | |
if not hasattr(inst.context, name): | |
inst.context.manage_addProperty(name, string, 'string') | |
else: | |
inst.context._updateProperty(name, string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment