def testWidgetExtract(self): from zope.interface import Interface, implementer from plone.app.textfield import RichText from zope.publisher.browser import TestRequest from Products.CMFCore.PortalContent import PortalContent from plone.app.textfield.widget import RichTextWidget from z3c.form.widget import FieldWidget from z3c.form.interfaces import NOVALUE class IWithText(Interface): text = RichText(title=u"Text", default_mime_type='text/structured', output_mime_type='text/html') @implementer(IWithText) class Context(PortalContent): text = None request = TestRequest() widget = FieldWidget(IWithText['text'], RichTextWidget(request)) widget.update() value = widget.extract() self.assertEquals(NOVALUE, value) request.form['%s' % widget.name] = u"Sample **text**" request.form['%s.mimeType' % widget.name] = 'text/structured' value = widget.extract() self.assertEquals( u"<p>Sample <strong>text</strong></p>", value.output.strip())
def testWidgetConverter(self): from zope.interface import Interface from plone.app.textfield import RichText from zope.publisher.browser import TestRequest from plone.app.textfield.value import RichTextValue from plone.app.textfield.widget import RichTextWidget from plone.app.textfield.widget import RichTextConverter from z3c.form.widget import FieldWidget _marker = object() class IWithText(Interface): text = RichText(title=u"Text", default_mime_type='text/structured', output_mime_type='text/html', missing_value=_marker) request = TestRequest() widget = FieldWidget(IWithText['text'], RichTextWidget(request)) widget.update() converter = RichTextConverter(IWithText['text'], widget) self.assertTrue(converter.toFieldValue(u'') is _marker) self.assertTrue(converter.toFieldValue(RichTextValue(u'')) is _marker)
def testWidgetAllowedTypesField(self): from zope.interface import Interface, implementer from plone.app.textfield import RichText from zope.publisher.browser import TestRequest from Products.CMFCore.PortalContent import PortalContent from plone.app.textfield.widget import RichTextWidget from z3c.form.widget import FieldWidget class IWithText(Interface): text = RichText( title=u"Text", default_mime_type='text/structured', output_mime_type='text/html', allowed_mime_types=( 'text/structured', 'text/html')) @implementer(IWithText) class Context(PortalContent): text = None request = TestRequest() widget = FieldWidget(IWithText['text'], RichTextWidget(request)) widget.update() self.portal['portal_properties']['site_properties']._setPropValue( 'forbidden_contenttypes', ['text/structured']) allowed = widget.allowedMimeTypes() self.failUnless('text/html' in allowed) self.failUnless('text/structured' in allowed)
def DateFieldWidget(field, request): """IFieldWidget factory for DatetimeWidget.""" widget = FieldWidget(field, DateWidget(request)) currentYear = datetime.date.today().year minimumYearRange = currentYear - 1900 # don't display dates before 1900 widget.years_range = (-minimumYearRange, 1) return widget
def ImageFieldWidget(field, request): image = FieldWidget(field, ImageWidget(request)) image.value = field.title # Get the full resource URL for the image: site = hooks.getSite() image.src = unicode(zope.traversing.api.traverse(site, "++resource++" + field.image, request=request)()) return image
def OptgroupFieldWidget(field, request): """Factory for OptgroupWidget.""" widget = FieldWidget(field, OptgroupWidget(request)) if hasattr(field, "value_type"): widget.multiple = getMultiAdapter((field, field.value_type, request), interfaces.IFieldWidget).multiple else: widget.multiple = getMultiAdapter((field, request), interfaces.IFieldWidget).multiple return widget
def DateFieldWidget(field, request): widget = FieldWidget(field, DateWidget(request)) widget.pattern_options.setdefault('date', {}) try: widget.pattern_options['date']['firstDay'] = first_weekday() except ComponentLookupError: pass return widget
def CarouselBackgroundLinkWidget(field, request): widget = FieldWidget(field, RelatedItemsWidget(request)) widget.vocabulary = 'plone.app.vocabularies.Catalog' widget.pattern_options = widget.pattern_options.copy() widget.pattern_options.update({ 'selectableTypes': ['Image'], 'maximumSelectionSize': 1 }) return widget
def CarouselReferencesWidget(field, request): widget = FieldWidget(field, RelatedItemsWidget(request)) widget.vocabulary = 'plone.app.vocabularies.Catalog' widget.pattern_options = widget.pattern_options.copy() widget.pattern_options.update({ 'selectableTypes': None, 'maximumSelectionSize': None }) return widget
def test_widget_values(self): from plone.app.widgets.dx import RichTextWidget from plone.app.textfield.value import RichTextValue widget = FieldWidget(self.field, RichTextWidget(self.request)) # set the context so we can get tinymce settings widget.context = self.portal widget.value = RichTextValue(u'Lorem ipsum \u2026') base_args = widget._base_args() self.assertEquals(base_args['value'], u'Lorem ipsum \u2026')
def RecurrenceFieldWidget(field, request): # Please note: If you create a new behavior with superclasses IEventBasic # and IRecurrence, then you have to reconfigure the dotted path value of # the start_field parameter for the RecurrenceWidget to the new # behavior name, like: IMyNewBehaviorName.start. widget = FieldWidget(field, RecurrenceWidget(request)) widget.start_field = 'IEventBasic.start' widget.first_day = first_weekday_sun0 widget.show_repeat_forever = False return widget
def testRichTextAreaWidgetConverter(self): from zope.interface import Interface from plone.app.textfield import RichText from zope.publisher.browser import TestRequest from plone.app.textfield.value import RichTextValue from plone.app.textfield.widget import RichTextWidget from plone.app.textfield.widget import RichTextAreaConverter from z3c.form.widget import FieldWidget _marker = object() class IWithText(Interface): text = RichText(title=u"Text", default_mime_type='text/structured', output_mime_type='text/html', missing_value=_marker) request = TestRequest() widget = FieldWidget(IWithText['text'], RichTextWidget(request)) widget.update() converter = RichTextAreaConverter(IWithText['text'], widget) # Test with None input. self.assertRaises(ValueError, converter.toFieldValue, None) self.assertTrue(converter.toWidgetValue(None) is None) # Test with string input. self.assertTrue(converter.toFieldValue('') is _marker) self.assertRaises(ValueError, converter.toFieldValue, b'Foo') self.assertRaises(ValueError, converter.toWidgetValue, b'') self.assertRaises(ValueError, converter.toWidgetValue, b'Foo') # Test with unicode input. self.assertTrue(converter.toFieldValue(u'') is _marker) self.assertEqual(converter.toFieldValue(u'Foo').raw, u'Foo') self.assertTrue( isinstance(converter.toFieldValue(u'Foo'), RichTextValue) ) self.assertEqual(converter.toWidgetValue(u''), u'') self.assertEqual(converter.toWidgetValue(u'Foo'), u'Foo') # Test with RichTextValue input. self.assertTrue(converter.toFieldValue(RichTextValue(u'')) is _marker) rich_text = RichTextValue(u'Foo') self.assertEqual(converter.toFieldValue(rich_text), rich_text) self.assertEqual(converter.toFieldValue(rich_text).raw, u'Foo') self.assertEqual(converter.toWidgetValue(rich_text), u'Foo')
def NotificationRulesFieldWidget(field, request): widget = FieldWidget(field, PatternWidget(request)) widget.pattern = 'uutask-notification-rules' widget.pattern_options = dict() widget.pattern_options['rule'] = dict( field2=[i[:2] for i in TIME_UNITS], field3=[i[:2] for i in TIME_RELATIONS], field4=[i[:2] for i in SOURCE_NOTIFY_DATE], ) widget.pattern_options['i18n'] = dict( add_rule=_(u"Add rule"), remove=_(u"Remove"), ) alsoProvides(widget, IInheritParentValue) return widget
def test_dx_tinymcewidget_single_mimetype(self): """A RichTextWidget with only one available mimetype should render the pattern class directly on itself. """ if IMarkupSchema: # if not, don't run this test self._set_mimetypes(allowed=('text/html',)) from plone.app.z3cform.widget import RichTextWidget widget = FieldWidget(self.field, RichTextWidget(self.request)) # set the context so we can get tinymce settings widget.context = self.portal rendered = widget.render() self.assertTrue('<select' not in rendered) self.assertTrue('pat-tinymce' in rendered) self.assertTrue('data-pat-tinymce' in rendered)
def test_widget_params(self): from plone.app.z3cform.widget import RichTextWidget widget = FieldWidget(self.field, RichTextWidget(self.request)) # set the context so we can get tinymce settings widget.context = self.portal widget.update() base_args = widget._base_args() self.assertEqual(base_args['name'], 'text') self.assertEqual(base_args['value'], u'') self.assertEqual(base_args['pattern'], 'tinymce') self.assertEqual(base_args['pattern_options']['prependToUrl'], 'resolveuid/') self.assertEqual( base_args['pattern_options']['upload']['relativePath'], '@@fileUpload')
def test_widget_params(self): from plone.app.widgets.dx import RichTextWidget widget = FieldWidget(self.field, RichTextWidget(self.request)) widget.update() base_args = widget._base_args() self.assertEqual(base_args['name'], 'text') self.assertEqual(base_args['value'], u'') self.assertEqual(base_args['pattern'], 'tinymce') if PLONE50: self.assertEqual(base_args['pattern_options'], {}) else: self.assertEqual(base_args['pattern_options']['prependToUrl'], 'resolveuid/') self.assertEqual(base_args['pattern_options']['prependToUrl'], 'resolveuid/') self.assertEqual(base_args['pattern_options']['anchorSelector'], self.portal.portal_tinymce.anchor_selector)
def test_widget_params(self): from plone.app.widgets.dx import RichTextWidget widget = FieldWidget(self.field, RichTextWidget(self.request)) # set the context so we can get tinymce settings widget.context = self.portal widget.update() base_args = widget._base_args() self.assertEqual(base_args['name'], 'text') self.assertEqual(base_args['value'], u'') self.assertEqual(base_args['pattern'], 'tinymce') self.assertEqual(base_args['pattern_options']['prependToUrl'], 'resolveuid/') self.assertEqual(base_args['pattern_options']['rel_upload_path'], '@@fileUpload') if not PLONE50: self.assertEqual(base_args['pattern_options']['anchorSelector'], self.portal.portal_tinymce.anchor_selector)
def test_dx_tinymcewidget_multiple_mimetypes_create(self): """A RichTextWidget with multiple available mimetypes should render a mimetype selection widget along with the textfield. When there is no field value, the default mimetype should be preselected. """ if IMarkupSchema: # if not, don't run this test self._set_mimetypes(allowed=('text/html', 'text/plain')) from plone.app.z3cform.widget import RichTextWidget widget = FieldWidget(self.field, RichTextWidget(self.request)) # set the context so we can get tinymce settings widget.context = self.portal rendered = widget.render() self.assertTrue('<select' in rendered) self.assertTrue('pat-textareamimetypeselector' in rendered) self.assertTrue('data-pat-textareamimetypeselector' in rendered) self.assertTrue( '<option value="text/html" selected="selected">' in rendered) self.assertTrue('pat-tinymce' not in rendered)
def test_extract(self): class IWithText(Interface): text = XMLText(title=u"Text") class Context(PortalContent): implements(IWithText) text = None widget = FieldWidget(IWithText['text'], XMLTextWidget(self.request)) widget.update() value = widget.extract() self.assertEquals(NOVALUE, value) self.request.form['%s' % widget.name] = u"<html><body>test</body></html>" self.request.form['%s.mimeType' % widget.name] = 'text/xml' value = widget.extract() self.assertEquals(u"<html><body>test</body></html>", value.raw)
def test_dx_tinymcewidget_multiple_mimetypes_edit(self): """A RichTextWidget with multiple available mimetypes should render a mimetype selection widget along with the textfield. When there is already a RichTextValue, it's mimetype should be preselected. """ if IMarkupSchema: # if not, don't run this test self._set_mimetypes(allowed=('text/html', 'text/plain')) from collective.patterns.widgets.dx import RichTextWidget from plone.app.textfield.value import RichTextValue widget = FieldWidget(self.field, RichTextWidget(self.request)) # set the context so we can get tinymce settings widget.context = self.portal widget.value = RichTextValue(u'Hello world', mimeType='text/plain') rendered = widget.render() self.assertTrue('<select' in rendered) self.assertTrue('pat-textareamimetypeselector' in rendered) self.assertTrue('data-pat-textareamimetypeselector' in rendered) self.assertTrue( '<option value="text/plain" selected="selected">' in rendered) self.assertTrue('pat-tinymce' not in rendered)
def test_converter(self): _marker = object() class IWithText(Interface): text = XMLText(title=u"Text", missing_value = _marker) widget = FieldWidget(IWithText['text'], XMLTextWidget(self.request)) widget.update() converter = XMLTextConverter(IWithText['text'], widget) self.assertTrue(converter.toFieldValue(u'') is _marker) self.assertTrue(converter.toFieldValue(XMLTextValue(u'')) is _marker) self.assertEquals(converter.toWidgetValue(None), None) self.assertTrue(IXMLTextValue.providedBy( converter.toWidgetValue(XMLTextValue(u'')))) self.request.form['%s' % widget.name] = u"<html><body>test</body></html>" self.request.form['%s.mimeType' % widget.name] = 'text/xml' widget.update() self.assertTrue(IXMLTextValue.providedBy( converter.toWidgetValue(u"<html><body>test</body></html>"))) with self.assertRaises(ValueError): converter.toWidgetValue(int)
def DueFieldWidget(field, request): widget = FieldWidget(field, PatternWidget(request)) widget.pattern = 'uutask-due' widget.pattern_options = dict() widget.pattern_options['date'] = dict() widget.pattern_options['computed'] = dict( field2=[i[:2] for i in TIME_UNITS], field3=[i[:2] for i in TIME_RELATIONS], field4=[i[:2] for i in SOURCE_DATE], ) widget.pattern_options['computed_dow'] = dict( field2=[i[:2] for i in DAYS_OF_WEEK], field3=[i[:2] for i in TIME_RELATIONS], field4=[i[:2] for i in SOURCE_DATE], ) alsoProvides(widget, IInheritParentValue) return widget
def ExpirationDateFieldWidget(field, request): widget = FieldWidget(field, TypeADatetimeWidget(request)) widget.pattern_options.setdefault("date", {}) widget.pattern_options["date"]["firstDay"] = first_weekday() return widget
def RelatedItemsFieldWidget(field, request): widget = FieldWidget(field, RelatedItemsWidget(request)) widget.vocabulary = 'plone.app.vocabularies.Catalog' return widget
def ButtonFieldWidget(field, request): button = FieldWidget(field, ButtonWidget(request)) button.value = field.title return button
def RelatedAttachmentsFieldWidget(field, request, extra=None): if extra is not None: request = extra return FieldWidget(field, RelatedAttachmentsWidget(request))
def ColorDictInputFieldWidget(field, request): """IFieldWidget factory for TextWidget.""" return FieldWidget(field, ColorDictInputWidget(request))
def ExperimentResultProjectionFieldWidget(field, request): return FieldWidget(field, ExperimentResultProjectionWidget(request))
def FunctionsFieldWidget(field, request): return FieldWidget(field, FunctionsWidget(request))
def FutureDatasetsFieldWidget(field, request): """ Widget to select datasets and layers """ return FieldWidget(field, FutureDatasetsWidget(request))
def ExperimentResultFieldWidget(field, request): return FieldWidget(field, ExperimentResultWidget(request))
def ExperimentSDMFieldWidget(field, request): return FieldWidget(field, ExperimentSDMWidget(request))
def DatasetDictFieldWidget(field, request): """ Widget to select datasets and layers """ return FieldWidget(field, DatasetDictWidget(request))
def DatasetFieldWidget(field, request): return FieldWidget(field, DatasetWidget(request))
def YearFieldWidget(field, request): """IFieldWidget factory for YearWidget.""" return FieldWidget(field, YearWidget(request))
def BoolRadioFieldWidget(field, request): return FieldWidget(field, BoolRadioWidget(request))
def ScheduleFieldWidget(field, request): """IFieldWidget factory for cheduleWidget.""" return FieldWidget(field, ScheduleWidget(request))
def PaymentProcessorFieldWidget(field, request): """IFieldWidget factory for PaymentProcessorWidget.""" return FieldWidget(field, PaymentProcessorWidget(request))
def RelatedImagesFieldWidget(field, request, extra=None): if extra is not None: request = extra return FieldWidget(field, RelatedImagesWidget(request))
def StreamNamedFileFieldWidget(field, request): return FieldWidget(field, StreamNamedFileWidget(request))
def RelatedMediaFieldWidget(field, request, extra=None): if extra is not None: request = extra return FieldWidget(field, RelatedMediaWidget(request))
def DateFieldWidget(field, request): widget = FieldWidget(field, DateWidget(request)) widget.years_range = (-100, 10) widget.update() return widget
def TableRadioFieldWidget(field, request): """IFieldWidget factory for TableRadioWidget.""" return FieldWidget(field, TableRadioWidget(request))
def RelatedItemsAddFieldWidget(field, request, extra=None): """An advanced related items widget for Plone.""" if extra is not None: request = extra return FieldWidget(field, RelatedItemsAddWidget(request))
def SubmitFieldWidget(field, request): submit = FieldWidget(field, SubmitWidget(request)) submit.value = field.title return submit
def SelectFieldWidget(field, request): return FieldWidget(field, SelectWidget(request))
def AssigneeFieldWidget(field, request): widget = FieldWidget(field, AjaxSelectWidget(request)) widget.vocabulary = 'uu.task.Users' widget.pattern_options['allowNewItems'] = False alsoProvides(widget, IInheritParentValue) return widget
def AjaxSelectFieldWidget(field, request, extra=None): if extra is not None: request = extra return FieldWidget(field, AjaxSelectWidget(request))
def DateFieldWidget(field, request): """IFieldWidget factory for DateWidget.""" return FieldWidget(field, DateWidget(request))
def RichTextFieldWidget(field, request): return FieldWidget(field, RichTextWidget(request))
def EndDateFieldWidget(field, request): widget = FieldWidget(field, DatetimeWidget(request)) widget.default_timezone = default_timezone return widget
def QueryStringFieldWidget(field, request, extra=None): if extra is not None: request = extra return FieldWidget(field, QueryStringWidget(request))
def MonthYearFieldWidget(field, request): """IFieldWidget factory for MonthYearWidget.""" return FieldWidget(field, MonthYearWidget(request))
def DataGridFieldObjectFactory(field, request): """IFieldWidget factory for DataGridField.""" return FieldWidget(field, DataGridFieldObject(request))
def FunctionsRadioFieldWidget(field, request): return FieldWidget(field, FunctionsRadioWidget(request))
def QueryStringFieldWidget(field, request): return FieldWidget(field, QueryStringWidget(request))
def _custom_field_widget(field, request): from plone.app.z3cform.widget import AjaxSelectWidget widget = FieldWidget(field, AjaxSelectWidget(request)) widget.vocabulary = 'plone.app.vocabularies.PortalTypes' return widget
def FunkyFieldWidget(field, request): """IFieldWidget factory for FunkyWidget.""" return FieldWidget(field, FunkyWidget(request))
def DataSubsetsFieldWidget(field, request): return FieldWidget(field, DataSubsetsWidget(request))
def DatetimeFieldWidget(field, request): return FieldWidget(field, DatetimeWidget(request))